Docsity

DESIGN PATTERNS INTRODUCTION, Lecture notes of Design Patterns

Partial preview of the text

Download DESIGN PATTERNS INTRODUCTION and more Lecture notes Design Patterns in PDF only on Docsity!

Chapter 6 Design Patterns

Creational Design Patterns

Structural Design Patterns

Behavioral Design Patterns

Applying Design Patterns

GoF Design Patterns

GoF Design pattern relationships

6.1 Creational Design Patterns

Factory Method

Abstract Factory

Builder

Prototype

Singleton

Abstract Factory - Motivation

Abstract Factory Motivation

1) Implement a user interface toolkit that supports multiple looks

and feel standards such as Motif, Windows 95 or the finder in

MacOS.

 How can you write a single user interface and make it portable

across the different look and feel standards for these window

managers?

2) Implement a facility management system for an intelligent

house that supports different control systems such as Siemens’

Instabus, Johnson & Control Metasys or Zumtobe’s proprietary

standard.

 How can you write a single control system that is independent

from the manufacturer?

Abstract Factory

The Client remains blissfully unaware of the various concrete classes in this

example. Client code deals with the simpler, abstract, general case.

Abstract Factory - Example

Solution: ShapeFactory interface.

Shape

<>

Square Circle

ShapeFactory

Implementation

<>

SomeApp

ShapeFactory

<>

makeSquare()

makeCircle()

Abstract Factory - Example

Problem: Every time we add a new Shape derivative, we have to add

a method to the ShapeFactory.

Solution:

public interface ShapeFactory {

public Shape make (String shapeName) throws Exception

}

public class ShapeFactoryImplementation implements ShapeFactory {

public Shape make(String shapeName) throws Exception {

if (shapeName.equals("Circle")) return new Circle();

else if (shapeName.equals("Square")) return new Square();

else

throw new Exception("ShapeFactory cannot create " + shapeName);

}

}

………………………

private ShapeFactory factory;

factory = new ShapeFactoryImplementation();

Shape s = factory.make("Circle");

Abstract Factory - Structure

Factory Method - example

For example, a framework for a windowing application
has a class Application which must create an object of
class Document
But the actual applications and documents are not
written yet!
Solution:
Let subclasses decide which objects to instantiate
Application subclasses redefine an abstract
CreateDocument operation on Application to return the
appropriate Document subclass.
Once an Application subclass is instantiated, it can then
instantiate application-specific Documents without
knowing their class.
We call CreateDocument a factory method because it's
responsible for "manufacturing" an object.

Factory Method

Intent:

Define an interface for creating an object, but let subclasses decide which class to

instantiate.

Factory Method lets a class defer instantiation to subclasses.

Structure

Builder – Motivation (GoF)

Problem:

A reader for the RTF (Rich Text Format) document exchange format should be able

to convert RTF to many text formats.

The problem is that the number of possible conversions is open-ended. So it

should be easy to add a new conversion without modifying the reader.

Solution:

to configure the RTFReader class with a TextConverter object that converts RTF to

another textual representation.

Subclasses of TextConverter specialize in different conversions and formats.

Builder

Intent:

Separate the construction of a complex object from its

representation so that the same construction process can create

different representations.

Structure

Builder

Collaborations :

The client creates the Director object and configures it with the desired Builder object.

Director notifies the builder whenever a part of the product should be built.

Builder handles requests from the director and adds parts to the product.

The client retrieves the product from the builder.