Design Patterns - Abstract Factory Pattern

Preview:

Citation preview

Mudasir Qazi - mudasirqazi00@gmail.com 1

Abstract Factory Pattern

From Definition to Implementation

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 2

Contents / Agenda

• Definition• Advantages and Usage• Implementation diagram• Implementation steps 1 to 5• Test and results• Difference between Factory and Abstract Factory Patterns.

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 3

Definition

• Abstract Factory Pattern says that just define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.• That means Abstract Factory lets a class returns a

factory of classes. So, this is the reason that Abstract Factory Pattern is one level higher than the Factory Pattern. This factory is also called as Factory of factories.• An Abstract Factory Pattern is also known as Kit.• It comes under “Creational Design Patterns”.17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 4

Advantages and Usage

• Advantages:1. Abstract Factory Pattern isolates the client code from concrete

(implementation) classes.2. It eases the exchanging of object families.3. It promotes consistency among objects.

• Usage1. When the system needs to be independent of how its object are created,

composed, and represented.2. When the family of related objects has to be used together, then this

constraint needs to be enforced.3. When you want to provide a library of objects that does not show

implementations and only reveals interfaces.4. When the system needs to be configured with one of a multiple family of

objects.

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 5

Implementation - Overview

• We're going to create a Shape and Color interfaces and concrete classes implementing these interfaces. We creates an abstract factory class AbstractFactory as next step. Factory classes ShapeFactory and ColorFactory are defined where each factory extends AbstractFactory. A factory creator/generator class FactoryProducer is created.• AbstractFactoryPatternDemo, our demo class uses FactoryProducer to

get a AbstractFactory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to AbstractFactory to get the type of object it needs.

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 6

Class Diagram

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 7

Step 1 : Interfaces

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 8

Step 2 : Concrete classes

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 9

Step 3 : Create Abstract Factory

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 10

Step 4 : Factory Classes

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 11

Step 5 : Factory Producer

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 12

Step 6 : Test

You will create factories from Factory Producer using getFactory method. Then by using each factory you will create objects from getShape / getColor methods defined in each factory.

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 13

Step 7 : Results

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 14

Factory Method vs Abstract Factory• The methods of an Abstract Factory are implemented as Factory Methods.

Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance where the Abstract Factory creates objects through composition.• With the Factory pattern, you produce implementations (Apple, Banana,

Cherry, etc.) of a particular interface -- say, IFruit. With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- say, IFruitFactory. Each of those knows how to create different kinds of fruit.• Factory method: You have a factory that creates objects that derive from a

particular base class.Abstract factory: You have a factory that creates other factories, and these factories in turn create objects derived from base classes. You do this because you often don't just want to create a single object (as with Factory method) - rather, you want to create a collection of related objects.

17-Dec-14

Mudasir Qazi - mudasirqazi00@gmail.com 15

Example

• Imagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the Factory.• Now, consider the same example of the door. You can

go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory.

17-Dec-14

Recommended