15708968 GoF Design Pattern an Overview

Embed Size (px)

Citation preview

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    1/33

    GOF - Design Patterns

    Creational patterns Abstract factory Builder Factory method Prototype Singleton

    Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Proxy

    Behavioral patterns Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template method Visitor

    http://www.tml.tkk.fi/~pnr/GoF-models/html/Abstract-factory.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Builder.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Factory-method.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Prototype.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Singleton.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Adapter.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Bridge.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Composite.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Decorator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Facade.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Flyweight.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Proxy.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Chain-of-responsibility.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Command.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Interpreter.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Iterator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Mediator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Memento.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Observer.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/State.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Strategy.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Template-method.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Visitor.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Chain-of-responsibility.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Command.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Interpreter.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Iterator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Mediator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Memento.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Observer.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/State.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Strategy.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Template-method.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Visitor.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Singleton.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Builder.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Factory-method.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Prototype.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Abstract-factory.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Proxy.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Adapter.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Bridge.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Composite.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Decorator.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Facade.htmlhttp://www.tml.tkk.fi/~pnr/GoF-models/html/Flyweight.html
  • 8/8/2019 15708968 GoF Design Pattern an Overview

    2/33

    Creational patterns

    Creational design patterns are design patterns that deal with object creation mechanisms, trying tocreate objects in a manner suitable to the situation. The basic form of object creation could result indesign problems or added complexity to the design. Creational design patterns solve this problem bysomehow controlling this object creation. This often involves isolating the details of object creation soyour code isnt dependent on what types of objects there are and thus doesnt have to be changed whenyou add a new type of object.

    1.1 Factory method

    The Factory pattern provides a way to use an instance as a object factory. The factory can return aninstance of one of several possible classes (in a subclass hierarchy), depending on the data provided toit. The essence of the Factory Pattern is to "Define an interface for creating an object, but let thesubclasses decide which class to instantiate. The Factory method lets a class defer instantiation tosubclasses. The Factory Method defines an interface for creating objects, but lets subclasses decidewhich classes to instantiate.

    e.g.#1

    public interface ImageReader {public DecodedImage getDecodedImage();

    }

    public class GifReader implements ImageReader {public DecodedImage getDecodedImage() {return decodedImage;

    }} public class JpegReader implements ImageReader {

    // ....}

    public class ImageReaderFactory {public static ImageReader getImageReader(InputStream is) {

    int imageType = figureOutImageType(is);

    switch(imageType) {case ImageReaderFactory.GIF:

    return new GifReader(is);case ImageReaderFactory.JPEG:

    return new JpegReader(is);// etc.

    }}

    }

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    3/33

    e.g.#2

    class Complex{

    public static Complex fromCartesian(double real, double imag) {return new Complex(real, imag);

    }

    public static Complex fromPolar(double modulus, double angle) {return new Complex(modulus * cos(angle), modulus * sin(angle));

    }

    private Complex(double a, double b) {//...

    }}

    Complex c = Complex.fromPolar(1, pi); // Same as fromCartesian(-1, 0)

    Ref: http://en.wikipedia.org/wiki/Factory_method_pattern

    Where to useWhen a class can't anticipate which kind of class of object it must create.

    You want to localize the knowledge of which class gets created.

    When you have classes that is derived from the same subclasses, or they may in fact beunrelated classes that just share the same interface. Either way, the methods in these classinstances are the same and can be used interchangeably.

    When you want to insulate the client from the actual type that is being instantiated.

    e.g. Figure

    http://en.wikipedia.org/wiki/Factory_method_patternhttp://en.wikipedia.org/wiki/Factory_method_patternhttp://en.wikipedia.org/wiki/Factory_method_pattern
  • 8/8/2019 15708968 GoF Design Pattern an Overview

    4/33

    1.2 Abstract Factory

    Abstract Factory provide an interface for creating families of related or dependent objects without

    specifying their concrete classes.Thus it provides a way to encapsulate a group of individual factoriesthat have a common theme. The purpose of the Abstract Factory is to provide an interface for creatingfamilies of related objects without specifying concrete classes.The Abstract Factory pattern looks likethe factory objects weve seen previously, with not one but several factory methods. Each of the factorymethods creates a different kind of object. The idea is that at the point of creation of the factory object,you decide how all the objects created by that factory will be used.The Abstract Factory pattern is a creational pattern which is related to the Factory Method pattern, butit adds another level of abstraction. What this means is that the pattern encapsulates a group of individual concrete factory classes (as opposed to concrete factory methods which are derived insubclasses) which share common interfaces. The client software uses the Abstract Factory which

    provides an interface for creating families of related or dependent objects without specifying their

    concrete classes. This patternseparates the implementation details of a set of objects from its general usage.

    Where to useThe pattern can be used where we need to create sets of objects that share a common theme and wherethe client only needs to know how to handle the abstract equivalence of these objects, i.e. theimplementation is not important for the client. The Abstract Factory is often employed when there is aneed to use different sets of objects and where the objects could be added or changed some time duringthe lifetime of an application. Use of this pattern makes it possible to interchange concrete classeswithout changing the code that uses them, even at runtime.

    e.g./* GUIFactory example -- */ interface GUIFactory {

    public Button createButton();}

    class WinFactory implements GUIFactory {public Button createButton() {

    return new WinButton();}

    }

    class OSXFactory implements GUIFactory {public Button createButton() {

    return new OSXButton();}

    }

    interface Button {public void paint();

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    5/33

    }

    class WinButton implements Button {public void paint() {

    System.out.println("I'm a WinButton");}

    }

    class OSXButton implements Button {public void paint() {

    System.out.println("I'm an OSXButton");}

    }

    class Application {public Application(GUIFactory factory){

    Button button = factory.createButton();button.paint();

    }} public class ApplicationRunner {

    public static void main(String[] args) {new Application(createOsSpecificFactory());

    }

    public static GUIFactory createOsSpecificFactory() {int sys = readFromConfigFile("OS_TYPE");if (sys == 0) {

    return new WinFactory();} else {

    return new OSXFactory();}

    }}

    ouptput:The output should be either "I'm a WinButton" or "I'm an OSXButton" depending on which kind of factory was used. Note that the Application has no idea what kind of GUIFactory it is given or evenwhat kind of Button that factory creates.

    Ref:http://en.wikipedia.org/wiki/Abstract_factory_pattern

    http://www.dofactory.com/Patterns/PatternAbstract.aspx#_self1

    http://en.wikipedia.org/wiki/Abstract_factory_patternhttp://www.dofactory.com/Patterns/PatternAbstract.aspx#_self1http://en.wikipedia.org/wiki/Abstract_factory_patternhttp://www.dofactory.com/Patterns/PatternAbstract.aspx#_self1
  • 8/8/2019 15708968 GoF Design Pattern an Overview

    6/33

    Figure:1

    1.3 Builder

    The Builder pattern can be used to ease the construction of a complex object from simple objects. TheBuilder pattern also separates the construction of a complex object from its representation so that thesame construction process can be used to create another composition of objects.

    Separate the construction of a complex object from its representation so that the same construction process can create different representations.

    Abstract factory is similar to builder in that it too may construct complex objects. The primarydifference is that the Builder pattern focuses on constructing a complex object step by step. Abstractfactor's emphasis is on families of product objects(either simple or complex). Builder returns the

    product as the final step, but as far as the Abstract Factory is concerned, the product gets returnedimmediately. The factor pattern defers the choice of what concrete type of object to make until runtime. The builder pattern encapsulates the logic of how to put together a complex object so that theclient just requests a configuration and the builder directs the logic of building it.

    e.g The main contractor(builder) in building a house knows, given a floor plan, knows how to executethe sequence of operations (i,e. by delegating to subcontractors) needed to build the complex object. If that logic was not encapsulated in a builder, then the buyers would have to organize the subcontractingthemselves ("Dear, shouldn't we have asked for the foundation to be laid before the roofers showedup?")

    The factory is concerned with what is made, the builder with how it is made.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    7/33

    Where to useWhen the algorithm for creating a complex object should be independent of the parts that makeup the object and how they are assembled.When the construction process must allow different representations for the object that isconstructed.When you want to insulate clients from the knowledge of the actual creation process and/or

    resulting product.Figure:

    In the class diagram above:The Builder specifies an abstract interface for creating parts of a Product.The ConcreteBuilder constructs and assembles parts of the product byimplementing the Builder interface.The Director constructs an object using the Builder interface.The Product represents the object under construction.

    e.g.

    public abstract class HouseBuilder{

    protected House house;protected Floor floor;protected Walls walls;protected Roof roof;public abstract House createHouse();

    public abstract Floor createFloor();public abstract Walls createWalls();public abstract Roof createRoof();

    }

    public class WoodBuilder extends HouseBuilder{

    public Floor createFloor() {floor = new WoodFloor();return floor;

    }

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    8/33

    public House createHouse() {house = new WoodHouse();return house;

    }public Roof createRoof() {

    roof = new WoodRoof();return roof;

    }

    public Walls createWalls() {walls = new WoodWalls();return walls;

    }}

    public class BrickBuilder extends HouseBuilder {

    //similar to WoodBuilder }

    public class HouseDirector {public House construcHouse(HouseBuilder builder) {

    House house = builder.createHouse();

    System.out.println(house.getRepresentation());house.setFloor(builder.createFloor());System.out.println(house.getFloor().getRepresentation());house.setWalls(builder.createWalls());System.out.println(house.getWalls().getRepresentation());house.setRoof(builder.createRoof());System.out.println(house.getRoof().getRepresentation());return house;

    }}

    public abstract class House {protected Floor floor;protected Walls walls;protected Roof roof;

    public Floor getFloor() {return floor;}public void setFloor(Floor floor) {

    this.floor = floor;}public Walls getWalls() {

    return walls;}public void setWalls(Walls walls) {

    this.walls = walls;

    }public Roof getRoof() {

    return roof;}public void setRoof(Roof roof) {

    this.roof = roof;}public abstract String getRepresentation();

    }

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    9/33

    public interface Floor {public String getRepresentation();

    }public interface Walls {

    public String getRepresentation();}public interface Roof {

    public String getRepresentation();}public class WoodHouse extends House {

    public String getRepresentation() {return "Building a wood house";

    }}public class WoodFloor implements Floor {

    public String getRepresentation() {return "Finished building wood floor";

    }}

    public class WoodWalls implements Walls {//similar to WoodFloor

    }public class WoodRoof implements Roof {

    //similar to WoodFloor}

    // Similar structure for Brick familypublic class BrickHouse extends House

    public class BrickFloor implements Floor

    public class BrickWalls implements Walls

    public class BrickRoof implements Roof

    public class HouseClient {public static void main(String[] args) {

    HouseDirector director = new HouseDirector();HouseBuilder woodBuilder = new WoodBuilder();BrickBuilder brickBuilder = new BrickBuilder();// Build a wooden houseHouse woodHouse = director.construcHouse(woodBuilder);System.out.println();// Build a brick houseHouse brickHouse = director.construcHouse(brickBuilder);

    }

    }Output:c:>Building a wood housec:>Finished building wood floorc:>Finished building wood wallsc:>Finished building wooden roofc:>c:>Building a brick housec:>Finished building brick floorc:>Finished building brick wallsc:>Finished building brick roof

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    10/33

    1.4 Prototype

    The Prototype pattern is basically the creation of new instances through cloning existing instances.

    By creating a prototype, new objects are created by copying this prototype.

    To implement the pattern, declare an abstract base class that specifies a pure virtual clone() method.Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class,and implements the clone() operation.The client, instead of writing code that invokes the "new" operator on a hard-wired class name, callsthe clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided byanother design pattern.Figure:

    1.5 Singleton

    Ensure a class has only one instance, and provide a global point of access to it. The Singleton pattern provides the possibility to control the number of instances (mostly one) that are allowed to be made.We also receive a global point of access to it (them). When only one instance or a specific number of instances of a class are allowed. Facade objects are often Singletons because only one Facade objectis required.

    e.g.#1 public class ClassicSingleton{

    private static ClassicSingleton instance = null;private ClassicSingleton(){

    // Exists only to defeat instantiation}

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    11/33

    public static ClassicSingleton getInstance(){

    if(instance == null){

    instance = new ClassicSingleton();}

    return instance;}}

    Figure:

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    12/33

    Structural patterns

    Designing objects to satisfy particular project constraints. These work with the way objects are

    connected with other objects to ensure that changes in the system dont require changes to thoseconnections. In software engineering, structural design patterns are design patterns that ease thedesign by identifying a simple way to realize relationships between entities.

    Structural Patterns describe how objects and classes can be combined to form structures. Wedistinguish between object patterns and class patterns. The difference is that class patterns describerelationships and structureswith the help of inheritance. Object patterns, on other hand, describe howobjects can be associated and aggregated to form larger, more complex structures.

    2.1 Adapter (a.k.a) Wrapper.

    The Adapter pattern is used to translate the interface of one class into another interface. This means thatwe can make classes work together that couldn't otherwise because of incompatible interfaces. A classadapter uses multiple inheritance (by extending one class and/or implementing one or more classes) toadapt one interface to another. An object adapter relies on object aggregation.

    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces

    Where to useWhen you want to use an existing class, and its interface does not match the one you need.

    When you want to create a reusable class that cooperates with unrelated or unforeseen classes,that is, classes that don't necessarily have compatible interfaces.When you want to increase transparency of classes.When you want to make a pluggable kit.

    Figure:

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    13/33

    2.2 Bridge

    Decouple an abstraction or interface from its implementation so that the two can vary independently.Bridge makes a clear-cut between abstraction and implementation.Figure:

    Abstraction; defines the abstract interface, maintains the Implementor referenceRefined Abstraction; extends the interface defined by AbstractionImplementor defines the interface for implementation classesConcreteImplementor implements the Implementor interface

    2.3 Composite

    Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treatindividual objects and compositions of objects uniformly. The Composite pattern helps you to createtree structures of objects without the need to force clients to differentiate between branches and leavesregarding usage. The Composite pattern lets clients treat individual objects and compositions of objectsuniformly.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    14/33

    Component;is the abstraction for all components, including composite ones,declares the interface for objects in the composition,

    implements default behavior for the interface common to all classes, as appropriate,declares an interface for accessing and managing its child components,(optional) defines an interface for accessing a component's parent in the recursive structure, and

    implements it if that's appropriate,Leaf;

    represents leaf objects in the composition,implements all Component methodsComposite;represents a composite Component (component having children),implements methods to manipulate children,implements all Component methods, generally by delegating them to its children

    e.g.

    The following example, written in Java , implements a graphic class, which can be either an ellipse or acomposition of several graphics. Every graphic can be printed. In algebraic form,

    Graphic = ellipse | GraphicListGraphicList = empty | ellipse GraphicList

    It could be extended to implement several other shapes (rectangle, etc.) and methods ( translate , etc.).

    List = empty_list | atom List | List List

    import java.util.List;import java.util.ArrayList; /** "Component" */interface Graphic {

    //Prints the graphic.public void print();

    http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Translation_(geometry)http://en.wikipedia.org/wiki/Translation_(geometry)http://en.wikipedia.org/wiki/Java_(programming_language)http://en.wikipedia.org/wiki/Translation_(geometry)
  • 8/8/2019 15708968 GoF Design Pattern an Overview

    15/33

    } /** "Composite" */class CompositeGraphic implements Graphic {

    //Collection of child graphics.private List mChildGraphics = new ArrayList();

    //Prints the graphic.public void print() {

    for (Graphic graphic : mChildGraphics) {graphic.print();

    }}

    //Adds the graphic to the composition.public void add(Graphic graphic) {

    mChildGraphics.add(graphic);}

    //Removes the graphic from the composition.public void remove(Graphic graphic) {mChildGraphics.remove(graphic);

    } }

    /** "Leaf" */class Ellipse implements Graphic {

    //Prints the graphic.public void print() {

    System.out.println("Ellipse");}

    }

    /** Client */public class Program {

    public static void main(String[] args) {

    //Initialize four ellipsesEllipse ellipse1 = new Ellipse();Ellipse ellipse2 = new Ellipse();Ellipse ellipse3 = new Ellipse();Ellipse ellipse4 = new Ellipse();

    //Initialize three composite graphicsCompositeGraphic graphic = new CompositeGraphic();CompositeGraphic graphic1 = new CompositeGraphic();CompositeGraphic graphic2 = new CompositeGraphic();

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    16/33

    //Composes the graphicsgraphic1.add(ellipse1);graphic1.add(ellipse2);graphic1.add(ellipse3);

    graphic2.add(ellipse4);

    graphic.add(graphic1);graphic.add(graphic2);

    //Prints the complete graphic (four times the string "Ellipse").graphic.print();

    }}

    2.4 Decorator

    The Decorator pattern lets you attach additional responsibilities and modify an instance functionalitydynamically. Decorators provide a flexible alternative to subclassing for extending functionality, usingcomposition instead of inheritance. The use of layered objects to dynamically and transparently addresponsibilities to individual objects is referred to as the decorator pattern. Attach additionalresponsibilities to an object dynamically keeping the same interface. Decorators provide a flexiblealternative to subclassing for extending functionality.

    As an example, consider a window in a windowing system. To allow scrolling of the window's

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    17/33

    contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windowsare represented by instances of the Window class, and assume this class has no functionality for addingscrollbars. We could create a subclass ScrollingWindow that provides them, or we could create aScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine.

    Now let's assume we also wish the option to add borders to our windows. Again, our original Windowclass has no support. The ScrollingWindow subclass now poses a problem, because it has effectivelycreated a new kind of window. If we wish to add border support to all windows, we must createsubclasses WindowWithBorder and ScrollingWindowWithBorder. Obviously, this problem gets worsewith every new feature to be added. For the decorator solution, we simply create a newBorderedWindowDecoratorat runtime, we can decorate existing windows with theScrollingWindowDecorator or the BorderedWindowDecorator or both, as we see fit.

    The following example illustrates the use of decorators using the window/scrolling scenario.

    // the Window interfaceinterface Window {

    public void draw(); // draws the Windowpublic String getDescription(); // returns a description of the Window

    }

    // implementation of a simple Window without any scrollbarsclass SimpleWindow implements Window {

    public void draw() {// draw window

    }

    public String getDescription() {

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    18/33

    return "simple window";}

    }

    The following classes contain the decorators for all Window classes, including the decorator classesthemselves..

    // abstract decorator class - note that it implements Windowabstract class WindowDecorator implements Window {

    protected Window decoratedWindow; // the Window being decorated

    public WindowDecorator (Window decoratedWindow) {this.decoratedWindow = decoratedWindow;

    }}

    // the first concrete decorator which adds vertical scrollbar functionalityclass VerticalScrollBarDecorator extends WindowDecorator {

    public VerticalScrollBarDecorator (Window decoratedWindow) {

    super(decoratedWindow);}

    public void draw() {drawVerticalScrollBar();decoratedWindow.draw();

    }

    private void drawVerticalScrollBar() {// draw the vertical scrollbar

    }

    public String getDescription() {return decoratedWindow.getDescription() + ", including vertical

    scrollbars";}

    }

    // the second concrete decorator which adds horizontal scrollbar functionalityclass HorizontalScrollBarDecorator extends WindowDecorator {

    public HorizontalScrollBarDecorator (Window decoratedWindow) {super(decoratedWindow);

    }

    public void draw() {drawHorizontalScrollBar();

    decoratedWindow.draw();}

    private void drawHorizontalScrollBar() {// draw the horizontal scrollbar

    }

    public String getDescription() {return decoratedWindow.getDescription() + ", including horizontal

    scrollbars";}

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    19/33

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    20/33

    Facade; The facade class abstracts Packages 1, 2, and 3 from the rest of the application.Clients; The objects using the Facade Pattern to access resources from the Packages.Packages; Software library / API collection accessed through the Facade Class.

    e.g.#2/* Complex parts */ class CPU {

    public void freeze() { ... }public void jump(long position) { ... }public void execute() { ... }

    } class Memory {

    public void load(long position, byte[] data) { ... }} class HardDrive { public byte[] read(long lba, int size) { ... }} /* Faade */ class Computer {

    public void startComputer() {cpu.freeze();memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR,

    SECTOR_SIZE));cpu.jump(BOOT_ADDRESS);cpu.execute();

    }}

    /* Client */

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    21/33

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    22/33

    characters in a word processor . It would be nice to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but it would amount tohundreds or thousands of bytes for each character. Instead, for every character there might be areference to a flyweight glyph object shared by every instance of the same character in the document;only the position of each character (in the document and/or the page) would need to be storedexternally.

    The following programs illustrate the document example given above: the flyweights are calledFontData in the Java example.

    The examples illustrate the flyweight pattern used to reduce memory by loading only the datanecessary to perform some immediate task from a large Font object into a much smaller FontData(flyweight) object.

    public enum FontEffect {BOLD, ITALIC, SUPERSCRIPT, SUBSCRIPT, STRIKETHROUGH

    }public final class FontData {

    /*** A weak hash map will drop unused references to FontData.* Values have to be wrapped in WeakReferences,* because value objects in weak hash map are held by strong references.*/

    private static final WeakHashMapFLY_WEIGHT_DATA =

    new WeakHashMap();private final int pointSize;private final String fontFace;private final Color color;private final Set effects;

    private FontData(int pointSize, String fontFace, Color color,

    EnumSet effects) {this.pointSize = pointSize;this.fontFace = fontFace;this.color = color;this.effects = Collections.unmodifiableSet(effects);

    }

    public static FontData create(int pointSize, String fontFace, Color color,FontEffect... effects) {EnumSet effectsSet = EnumSet.noneOf(FontEffect.class);for (FontEffect fontEffect : effects) {

    effectsSet.add(fontEffect);}// We are unconcerned with object creation cost, we are reducing overall

    memory consumption

    http://en.wikipedia.org/wiki/Word_processorhttp://en.wikipedia.org/wiki/Glyphhttp://en.wikipedia.org/wiki/Glyphhttp://en.wikipedia.org/wiki/Reference_(computer_science)http://en.wikipedia.org/wiki/Word_processorhttp://en.wikipedia.org/wiki/Glyphhttp://en.wikipedia.org/wiki/Reference_(computer_science)
  • 8/8/2019 15708968 GoF Design Pattern an Overview

    23/33

    FontData data = new FontData(pointSize, fontFace, color, effectsSet);if (!FLY_WEIGHT_DATA.containsKey(data)) {

    FLY_WEIGHT_DATA.put(data, new WeakReference (data));}// return the single immutable copy with the given valuesreturn FLY_WEIGHT_DATA.get(data).get();

    }

    public boolean equals(Object obj) {if (obj instanceof FontData) {

    if (obj == this) {return true;

    }FontData other = (FontData) obj;return other.pointSize == pointSize && other.fontFace.equals(fontFace)

    && other.color.equals(color) && other.effects.equals(effects);}return false;

    }

    @Override

    public int hashCode() {return (pointSize * 37 + effects.hashCode() * 13) * fontFace.hashCode();}

    // Getters for the font data, but no setters. FontData is immutable.}

    Figure:

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    24/33

    2.7 Proxy

    A Proxy is a structural pattern that provides a stand-in for another object in order to control access to it.

    A proxy, in its most general form, is a class functioning as an interface to something else. The proxycould interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. A well-known example of the proxy pattern is areference counting pointer object.

    Where to use1) When the creation of one object is relatively expensive it can be a good idea to replace it with a

    proxy that can make sure that instantiation of the expensive object is kept to a minimum.2) Proxy pattern implementation allows for login and authority checking before one reaches the actualobject that's requested.3)Can provide a local representation for an object in a remote location.

    The following Java example illustrates the "virtual proxy" pattern. The ProxyImage class is used todelay the expensive operation of loading a file from disk until the result of that operation is actuallyneeded. If the file is never needed, then the expensive load has been totally eliminated.

    import java.util.*; interface Image {

    public void displayImage();} class RealImage implements Image {

    private String filename;

    public RealImage(String filename) {this.filename = filename;loadImageFromDisk();

    }

    private void loadImageFromDisk() {System.out.println("Loading "+filename);// Potentially expensive operation// ...

    }

    public void displayImage() {System.out.println("Displaying "+filename);

    }} class ProxyImage implements Image {

    private String filename;private Image image;

    public ProxyImage(String filename) {

    this.filename = filename;}public void displayImage() {

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    25/33

    if (image == null) {image = new RealImage(filename); // load only on demand

    }image.displayImage();

    }} class ProxyExample {

    public static void main(String[] args) {Image image1 = new ProxyImage("HiRes_10MB_Photo1");Image image2 = new ProxyImage("HiRes_10MB_Photo2");Image image3 = new ProxyImage("HiRes_10MB_Photo3");

    image1.displayImage(); // loading necessaryimage2.displayImage(); // loading necessaryimage1.displayImage(); // no loading necessary; already done// the third image will never be loaded - time saved!

    }}

    The program's output is:

    Loading HiRes_10MB_Photo1Displaying HiRes_10MB_Photo1Loading HiRes_10MB_Photo2Displaying HiRes_10MB_Photo2Displaying HiRes_10MB_Photo1

    Figure:

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    26/33

    Behavioral patterns

    Objects that handle particular types of actions within a program. These encapsulate processes that youwant to perform, such as interpreting a language, fulfilling a request, moving through a sequence (as inan iterator), or implementing an algorithm.

    Chain of responsibility

    Avoid coupling the sender of a request to its receiver by giving more than one object a chance tohandle the request. Chain the receiving objects and pass the request along the chain until an objecthandles it.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    27/33

    Command

    Encapsulate a request as an object, thereby letting you parameterize clients with different requests,queue or log requests, and support undoable operations.

    Interpreter

    Given a language, define a representation for its grammar along with an interpreter that uses therepresentation to interpret sentences in the language.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    28/33

    Iterator

    Provide a way to access the elements of an aggregate object sequentially without exposing itsunderlying representation.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    29/33

    Mediator

    Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling bykeeping objects from referring to each other explicitly, and it lets you vary their interactionindependently.

    Memento

    Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    30/33

    Observer

    Define a one-to-many dependency between objects so that when one object changes state, all itsdependents are notified and updated automatically.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    31/33

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    32/33

    Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. TemplateMethod lets subclasses redefine certain steps of an algorithm without changing the algorithm'sstructure.

    Visitor

    Represent an operation to be performed on the elements of an object structure. Visitor lets you define anew operation without changing the classes of the elements on which it operates.

  • 8/8/2019 15708968 GoF Design Pattern an Overview

    33/33

    References

    http://en.wikipedia.org/wiki/Design_pattern_(computer_science ) http://www.dofactory.com/Patterns/Patterns.aspxhttp://www.javaworld.com/http://www.tml.tkk.fi/~pnr/GoF-models/html/

    http://en.wikipedia.org/wiki/Design_pattern_(computer_sciencehttp://www.dofactory.com/Patterns/Patterns.aspxhttp://www.javaworld.com/http://www.tml.tkk.fi/~pnr/GoF-models/html/http://en.wikipedia.org/wiki/Design_pattern_(computer_sciencehttp://www.dofactory.com/Patterns/Patterns.aspxhttp://www.javaworld.com/http://www.tml.tkk.fi/~pnr/GoF-models/html/