You are on page 1of 22

Design

Design Patterns
Patterns
CE00362-3
CT070-3-3

Creational Pattern
Introduction
Abstract & Overview
Factory Method

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 12)


Learning Outcomes

At the end of this session, you should be


able to;

Describe benefits of Abstract Factory


Method pattern
Recognise and apply Abstract Factory
Method pattern given a scenario

CT070-3-3 - Design Patterns Abstract Factory Method Slide 2 (of 12)


Abstract Factory Pattern

Name : Abstract Factory

Problem :
Provide an interface for creating families of related
dependent objects without specifying their concrete
classes.

A hierarchy that encapsulates: many possible


"platforms", and the construction of a suite of
"products".
CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)
Abstract Factory Pattern
Context:
Provide a level of indirection that abstracts the
creation of families of related or dependent
objects without directly specifying their concrete
classes.

The "factory" object has the responsibility for


providing creation services for the entire platform
family.

Clients never create platform objects directly, they


ask the factory to do that for them.

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Abstract Factory Pattern

Forces:

Static Factory
The static factory hides the choice of implementation
and instance management of a certain component,
from the objects using this component.

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Description Factory Method
Solution:
Decide if "platform independence" and creation
services are the current source of pain.
Map out a matrix of "platforms" versus "products".
Define a factory interface that consists of a factory
method per product.
Define a factory derived class for each platform that
encapsulates all references to the new operator.
The client should retire all references to new, and
use the factory methods to create the product
objects.

CT070-3-3 - Design Patterns Abstract Factory Method Slide 5 (of 12)


Class Design Abstract Factory
Method

CT070-3-3 - Design Patterns Abstract Factory Method Slide 6 (of 12)


Abstract Factory Pattern-
Demo

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Class:AbstractFactory

abstract class AbstractFactory{


abstract AbstractProductA createProductA();
abstract AbstractProductB createProductB();
}

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Class -AbstractProductA
abstract class AbstractProductA{
public abstract void operationA1();
public abstract void operationA2();
}
Class AbstractProductB
abstract class AbstractProductB{
//public abstract void operationB1();
//public abstract void operationB2();
}
CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)
Class product B1,ProductB2

class ProductB1 extends AbstractProductB{


ProductB1(String arg){
System.out.println("Hello "+arg);
} // Implement the code here
}
class ProductB2 extends AbstractProductB{
ProductB2(String arg){
System.out.println("Hello "+arg);
} // Implement the code here
}
CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)
Class Product A1,Product A2
class ProductA1 extends AbstractProductA{
ProductA1(String arg){
System.out.println("Hello "+arg);
} // Implement the code here
}
class ProductA2 extends AbstractProductA{
ProductA2(String arg){
System.out.println("Hello "+arg);
} // Implement the code here
CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)
Concrete Classes
class ConcreteFactory1 extends AbstractFactory
{
AbstractProductA createProductA(){
return new ProductA1("ProductA1");
}
AbstractProductB createProductB(){
return new ProductB1("ProductB1");
}
}
CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)
Concrete Classes

class ConcreteFactory2 extends AbstractFactory{


AbstractProductA createProductA(){
return new ProductA2("ProductA2");
}
AbstractProductB createProductB(){
return new ProductB2("ProductB2");
}
}

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Class Design -Description
AbstractFactory :
Declares a interface for operations that create abstract products.
ConcreteFactory :
Implements operations to create concrete products.
AbstractProduct :
Declares an interface for a type of product objects.
Product :
Defines a product to be created by the corresponding
ConcreteFactory; it implements the AbstractProduct interface.
Client :
Uses the interfaces declared by the AbstractFactory and
AbstractProduct classes.

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Example-1 Abstract Factory Pattern

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Example-2 Abstract Factory
Method

One classic application of the abstract factory is the case


where your system needs to support multiple look-and-
feel user interfaces, such as Windows-9x, Windows XP,
Motif or Macintosh. You tell the factory that you want your
program to look like Windows and it returns a GUI factory
which returns Windows-like objects. Then when you
request specific objects such as buttons, check boxes
and windows, the GUI factory returns Windows instances
of these visual interface components.

CT070-3-3 - Design Patterns Abstract Factory Method Slide 7 (of 12)


Abstract Factory vs. Factory
Method

Abstract Factory pattern is one level of abstraction


higher than the Factory pattern.
Factory Method is to create one product only
but Abstract Factory is about creating families of
related or dependent products.
Abstract factory is a factory of factories.
Factory Method hides the construction of single
object where as Abstract Factory hides the
construction of a family of related objects. Abstract
factories are usually implemented using (a set of)
factory methods.
CT070-3-3 - Design Patterns Abstract Factory Method Slide 9 (of 12)
Abstract Factory vs. Factory
Method (Example)

CT070-3-3 - Design Patterns Abstract Factory Method Slide 1 (of 18)


Summary

In this lecture we have:


Introduced the Abstract Factory Method
pattern
Discussed examples that take advantage
of the Abstract Factory Method pattern

CT070-3-3 - Design Patterns Abstract Factory Method Slide 10 (of 12)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Abstract Factory Method Slide 11 (of 12)


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Abstract Factory Method Slide 12 (of 12)

You might also like