Download presentation
Presentation is loading. Please wait.
1
Factory Patterns 1
2
Overview Factory: a class whose sole job is to easily create and return instances of other classes A creational pattern; makes it easier to construct complex objects Instead of calling a constructor, use a static method in a “factory” class to set up the object Saves lines and complexity to quickly construct/initialize objects
3
Motivation All OO languages have an idiom for object creation. In C# this idiom is the new operator. Creational patterns allow us to write methods that create new objects without explicitly using the new operator. This allows us to write methods that can instantiate different objects and that can be extended to instantiate other newly-developed objects, all without modifying the method’s code.
4
Simple Factory Intent The Simple Factory isn’t actually a Design Pattern; it’s more of a programming idiom Pull the code that builds the instances of classes out and put it into a separate class Identify the aspects of your application that vary and separate them from what stays the same A Simple Factory pattern is one that returns an instance of one of several possible classes, depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods but each perform a task differently
5
<< abstract >>
Class Diagram << abstract >> Product Client SimpleFactory orderProduct () createProduct () productMethod () The create method is often declared statically This is the Factory where we create Product; it should be the only part of our application that refers to Concrete Product classes ConcreteProductA ConcreteProductB productMethod () productMethod ()
6
Factory Method Pattern Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate Lets a class defer instantiation to subclasses
7
Factory Method Pattern Applicability
Use the Factory Method pattern in any of the following situations A class can’t anticipate the class of objects it must create A class wants its subclasses to specify the objects it creates
8
Class Diagram A class used to create other objects Factory Product
product = FactoryMethod() FactoryMethod() AnOperation() abstract IS-A IS-A ConcreteFactory return new ConcreteProduct() ConcreteProduct FactoryMethod() HAS-A A class used to create other objects
9
Participants Product ConcreteProduct Factory ConcreteFactory
Defines the interface for the type of objects the factory method creates ConcreteProduct Implements the Product interface Factory Declares the factory method which returns an object of type Product ConcreteFactory Factory relies on its subclasses to implement the factory method so that it returns an instance of the appropriate ConcreteProduct
10
Collaborations Factory relies on its subclasses to implement the Factory method so that it returns an instance of the appropriate ConcreteProduct Say What??? It means that Factory class is written without knowing what actual ConcreteProduct class will be instantiated. The ConcreteProduct class which is instantiated is determined solely by which ConcreteFactory subclass is instantiated and used by the application It does not mean that somehow the subclass decides at runtime which ConcreteProduct class to create
11
Sequence Diagram A Client A ConcreteFactory A ConcreteProduct
CreateProduct (args) Product product = new ConcreteProduct () return product
12
Consequences Benefits Liabilities
Code is made more flexible and reusable by the elimination of instantiation of application-specific classes Code deals with the interface of the Product class and can work with any ConcreteProduct class that supports this interface Liabilities Clients might have to subclass the Factory class just to instantiate a particular ConcreteProduct
13
Implementation Issues
Factory can be abstract or concrete Should the Factory method be able to create multiple kinds of products? If so, then the factory method has a parameter (possibly used in an if-else) to decide what object to create
14
Abstract Factory Pattern Intent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes The Abstract Factory pattern is very similar to the Factory Method pattern. One difference between the two is that the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation Actually, the delegated object frequently uses factory methods to perform the instantiation!
15
Abstract Factory Pattern Applicability
Use the Abstract Factory pattern in any of the following situations A system should be independent of how its products are created, composed, and represented A class can’t anticipate the class of objects it must create A system must use just one of a set of families of products A family of related product objects is designed to be used together, and you need to enforce this constraint
16
Class Diagram <<abstract>> Product
Factory CreateProductA() CreateProductB() ConcreteProductA1 ConcreteProductA2 ConcreteFactory1 ConcreteFactory2 <<abstract>> Product CreateProductA() CreateProductB() CreateProductA() CreateProductB() ConcreteProductB1 ConcreteProductB2
17
Participants AbstractFactory ConcreteFactory AbstractProduct
Declares an interface for operations that create abstract Product objects ConcreteFactory Implements the operations to create ConcreteProduct objects AbstractProduct Declares an interface for a type of Product object ConcreteProduct Defines a product object to be created by the corresponding ConcreteFactory Implements the AbstractProduct interface Client Uses only interfaces declared by AbstractFactory and AbstractProduct classes
18
Collaborations Normally a single instance of a ConcreteFactory class is created at runtime (This is an example of the Singleton Pattern). This concrete factory creates Product objects having a particular implementation. To create different Product objects, Clients should use a different ConcreteFactory.
19
Consequences Benefits Liabilities
Isolates Clients from concrete implementation classes Makes exchanging product families easy, since a particular ConcreteFactory can support a complete family of products Enforces the use of products only from one family Liabilities Supporting new kinds of products requires changing the AbstractFactory interface
20
Implementation Issues
How can the factories create the products? Factory Methods Factories How can new Products be added to the AbstractFactory interface? AbstractFactory defines a different method for the creation of each Product it can produce We could change the interface to support only a Create(String kindOfProduct) method
21
Summary Simple Factory Factory Method Abstract Factory
Use when you have only one family of objects Factory Method Use when you have multiple families of objects Abstract Factory Use when you have multiple families of object components
22
Factory Method vs Abstract Factory
Factory Method pattern A Factory that can be constructed and has an overridable method to create its objects Abstract Factory pattern When the topmost Factory class and its creational method are abstract
23
?
24
References
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.