Daily Patterns #05

download Daily Patterns #05

of 3

Transcript of Daily Patterns #05

  • 7/31/2019 Daily Patterns #05

    1/3

    Factory methodDESIGN pattern

    Intent

    Define an interface for creating an object,but let subclasses decide which class toinstantiate. Factory Method lets a class

    defer instantiation to subclasses.

    Defining a virtual constructor.

    The new operator considered harmful.

    PROBLEM

    A framework needs to standardize thearchitectural model for a range of

    applications, but allow for individualapplications to define their own domain

    objects and provide for their instantiation.

    Factory Method is to creating objects asTemplate Method is to implementing an

    algorithm. A superclass specifies allstandard and generic behavior (using pure

    virtual placeholders for creation steps),and then delegates the creation details tosubclasses that are supplied by the client.

    Factory Method makes a designmore customizable and only alittle more complicated. Other

    design patterns require newclasses, whereas Factory

    Method only requires a newoperation.

    People often useFactory Method as the

    standard way to createobjects; but it isnt

    necessary if: the classthats instantiatednever changes, orinstantiation takes

    place in an operationthat subclasses can

    easily override (such asan initialization

    operation).Factory Methods

    are routinelyspecified by an

    architecturalframework, and

    then implementedby the user of the

    framework.

    Factory Method is similar toAbstract Factory but without

    the emphasis on families.

    seven nations -"the factory song"

    http://www.youtube.com/watch?v=kH21Nr9gGEU

    http://www.youtube.com/watch?v=kH21Nr9gGEUhttp://www.youtube.com/watch?v=kH21Nr9gGEU
  • 7/31/2019 Daily Patterns #05

    2/3

  • 7/31/2019 Daily Patterns #05

    3/3

    Example

    The Factory Method defines aninterface for creating objects, but

    lets subclasses decide whichclasses to instantiate. Injection

    molding presses demonstrate thispattern. Manufacturers of plastic

    toys process plastic moldingpowder, and inject the plastic intomolds of the desired shapes. The

    class of toy (car, action figure, etc.)is determined by the mold.

    Factory Methods are usually calledwithin Template Methods.

    Factory Method: creation throughinheritance. Prototype: creation through

    delegation.

    Prototype doesnt require subclassing,but it does require an Initialize

    operation. Factory Method requiressubclassing, but doesnt require

    Initialize.

    Some GUYS recommend that as matter of

    language design absolutely allconstructors should be private or

    protected. Its no one elses businesswhether a class manufactures a new

    object or recycles an old one.

    Often, designs start out usingFactory Method (less complicated,more customizable, subclassesproliferate) and evolve towardAbstract Factory, Prototype, or

    Builder (more flexible, morecomplex) as the designer discovers

    where more flexibility is needed.

    The advantage of a Factory Method isthat it can return the same instance

    multiple times, or can return asubclass rather than an object of

    that exact type.

    Abstract Factory classes are oftenimplemented with Factory Methods,but they can be implemented using

    Prototype.

    The new operator consideredharmful. There is a difference

    between requesting an object andcreating one. The new operator

    always creates an object, and failsto encapsulate object creation. A

    Factory Method enforces thatencapsulation, and allows an objectto be requested without

    inextricable coupling to the act ofcreation.