Factory Abstract Factory Builder Prototype Creational Patterns Making
Factory, Abstract Factory, Builder, Prototype Creational Patterns Making Objects The Smart Way
What are creational patterns? n n n Design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation Make a system independent of the way in which objects are created, composed and represented Recurring themes: ¨ Encapsulate knowledge about which concrete classes the system uses (so we can change them easily later) ¨ Hide how instances of these classes are created and put together (so we can change it easily later)
Benefits of creational patterns n n n Creational patterns let you program to an interface defined by an abstract class That lets you configure a system with “product” objects that vary widely in structure and functionality Example: GUI systems ¨ Inter. Views GUI class library ¨ Multiple look-and-feels ¨ Abstract Factories for different screen components
Benefits of creational patterns n n n Generic instantiation – Objects are instantiated without having to identify a specific class type in client code (Abstract Factory, Factory) Simplicity – Make instantiation easier: callers do not have to write long complex code to instantiate and set up an object (Builder, Prototype pattern) Creation constraints – Creational patterns can put bounds on who can create objects, how they are created, and when they are created
4 Factory n creating objects without specifying the exact class of object that will be created n Solution: define a separate method for creating the objects, whose subclasses can then override to specify the derived type of product that will be created
Abstract Factory: An Example n Manage addresses and phone numbers ¨ You hard-coded it for US data ¨ At some point, you wanted to extend it to incorporate any address / phone number ¨ So you subclassed n Dutch. Address, Japanese. Phone. Number, etc. ¨ But now, how do you create them?
Abstract Factory: Overview n Intent ¨ Provide an interface for creating families of related or dependent objects without specifying their concrete classes n Analogous to a pasta maker Your code is the pasta maker Different disks create different pasta shapes: these are the factories All disks have certain properties in common so that they will work with the pasta maker All pastas have certain characteristics in common that are inherited from the generic “Pasta” object
7 Example n http: //en. wikipedia. org/wiki/Abstract_factory_patt ern#Java
Abstract Factory: Participants n Abstract. Factory Declares an interface for operations that create abstract products n Concrete. Factory Implements the operations to create concrete product objects: usually instantiated as a Singleton n Abstract. Product Declares an interface for a type of product object; Concrete Factories produce the concrete products n Concrete. Product Defines a product object to be created by the corresponding concrete factory
Abstract Factory: Applicability n Use Abstract Factory when: ¨A system should be independent of how its products are created, composed, and represented ¨ A system should be configured with one of multiple families of products ¨ You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
Abstract Factory: Consequences n Good: ¨ Isolates n All manipulation on client-side done through abstract interfaces ¨ Makes n concrete classes exchanging product families easy Just change the Concrete. Factory ¨ Enforces n consistency among products Bad ¨ Supporting new kinds of products is difficult ¨ Have to reprogram Abstract Factory and all subclasses ¨ But it’s not so bad in dynamically typed languages
Abstract Factory: Implementation n n Usually should be a Singleton Define a Factory Method (Go. F) in Abstract. Factory – Concrete. Factory then specifies its products by overriding the factory for each public class USAddress. Factory implements Address. Factory{ public Address create. Address(){ return new USAddress(); } public Phone. Number create. Phone. Number(){ return new USPhone. Number(); } }
Builder: Overview n Intent ¨ abstract steps of construction of object so that different implementations of these steps can construct different representations of objects. n Think of a car factory ¨ Boss tells workers (or robots) to build each part of a car ¨ Workers build each part and add them to the car being constructed
13 Example n http: //en. wikipedia. org/wiki/Builder_pattern#Java
Builder: Participants n Builder Specifies an abstract interface for creating parts of a Product object n Concrete. Builder Constructs and assembles parts of the product by implementing the Builder interface n Director Constructs an object using the Builder interface n Product Represents the complex object under construction Includes classes that define the constituent parts Gives interfaces for assembling the parts
Builder: Collaborations n n Client creates Director object and configures it with a Builder Director notifies Builder to build each part of the product Builder handles requests from Director and adds parts to the product Client retrieves product from the Builder
Builder: Applicability n Use Builder when: ¨ The algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled ¨ The construction process must allow different representations for the object being constructed ¨ The building process can be broken down into discrete steps (difference between Builder and Abstract Factory)
Builder: Consequences n n n Lets you vary a product’s internal representation by using different Builders Isolates code for construction and representation Gives finer-grain control over the construction process
Builder: Implementation n Issues to consider: ¨ Assembly and construction interface: generality ¨ Is an abstract class for all Products necessary? n Usually products don’t have a common interface ¨ Usually there’s an abstract Builder class that defines an operation for each component that a director may ask it to create. n n These operations do nothing by default (empty, non-virtual methods in C++) The Concrete. Builder overrides operations selectively
19 Prototype n n n avoid subclasses of an object creator in the client application, like the abstract factory pattern does. avoid the inherent cost of creating a new object in the standard way (e. g. , using the 'new' keyword). Solution: 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.
20 Example n http: //en. wikipedia. org/wiki/Prototype_pattern#Ja va
Conclusions n n Creational design patterns are beneficial in that they allow your software to tightly control the way in which it constructs objects Separate users of the code from the messy details of creating and building objects
Conclusions n Abstract Factory: ¨ Lets you choose what family of products to create at runtime ¨ Isolates the implementation from clients, since clients only use the interfaces ¨ Makes exchanging product families simple
Conclusions n Builder: ¨ Lets you vary how a product gets assembled. ¨ Can define a new kind of builder for a product to assemble it in a different way ¨ Client doesn’t need to know anything about the construction process, nor the parts that make up a product.
- Slides: 24