0% found this document useful (0 votes)
28 views

Abstract Factory Pattern: Jiaxin Wang CSPP 51023 Winter 2010

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It isolates clients from implementation classes by using abstract interfaces and avoids naming concrete classes directly. The pattern is applicable when clients need to operate with different families of products and when consistent groups of products must be used together. The pattern promotes loose coupling between products and clients.

Uploaded by

Sasi Darran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
28 views

Abstract Factory Pattern: Jiaxin Wang CSPP 51023 Winter 2010

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. It isolates clients from implementation classes by using abstract interfaces and avoids naming concrete classes directly. The pattern is applicable when clients need to operate with different families of products and when consistent groups of products must be used together. The pattern promotes loose coupling between products and clients.

Uploaded by

Sasi Darran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 10

Abstract Factory Pattern

Jiaxin Wang
CSPP 51023
Winter 2010
Intent

 “Provide an interface for creating families of related


or dependent objects without specifying their
concrete classes.”

– provide a simple creational interface for a complex family of


classes
• Client does not have to know any of those details.

– avoid naming concrete classes


• Clients use abstract creational interfaces and abstract product
interfaces. Concrete classes can be changed without affecting
clients.
Applicability

 Use the Abstract Factory Pattern if:

– clients need to be ignorant of how servers are created, composed,


and represented.

– clients need to operate with one of several families of products

– a family of products must be used together, not mixed with


products of other families

– provide a library and want to show just the interface, not


implementation of the library components.
Collaborators

 Usually only one ConcreteFactory instance is used for an


activation, matched to a specific application context. It builds a
specific product family for client use -- the client doesn’t care
which family is used -- it simply needs the services appropriate
for the current context.

 The client may use the AbstractFactory interface to initiate


creation, or some other agent may use the AbstractFactory on
the client’s behalf.

 The factory returns, to its clients, specific product instances


bound to the product interface. This is what clients use for all
access to the instances.
Consequences

 The Abstract Factory Pattern has the following benefits:

– It isolates concrete classes from the client.


• You use the Abstract Factory to control the classes of objects the client creates.
• Product names are isolated in the implementation of the Concrete Factory, clients
use the instances through their abstract interfaces.

– Exchanging defined product families is easy.


• None of the client code breaks because the abstract interfaces don’t change.
• Because the abstract factory creates a complete family of products, the whole
product family changes when the concrete factory is changed.

– It promotes consistency among products.


• It is the concrete factory’s job to make sure that the right products are used
together.

It is also easy to replace any implementation of the product interfaces. Just


rebuild the library and copy into the client’s directory.
Abstract Interface

Fact:
This client will be compile-time independent of the
concrete implementation if, and only if, it does not
directly create an instance of the concrete class

The purpose of an abstract


abstract interface interface is to provide a protocol
for clients to use to request
client (a class with at least one service from concrete objects
pure virtual function) without coupling to their
implementations

Client has a pointer statically


typed as pointer to interface,
but that pointer will refer to a
concrete implementation
object
concrete
implementation
Abstract Factory Structure
Implementation example

 Sample sample=new Sample();  Public class Factory


{
 Sample mysample=new MySample(); Public static Sample creator(int
which)
 Sample hissample=new HisSample(); {
 f if (which==1)
 F return new SampleA();
 Ff    else if (which==2)
 F    return new SampleB();
 F    }
 F  }
 f

Sample sampleA=Factory.creator(1);
Implementation example

 public abstract class Factory{


    public abstract Sample creator();
    public abstract Sample2 creator(String name); }
    public class SimpleFactory extends Factory{
    public Sample creator(){
    .........
    return new SampleA
    }
    public Sample2 creator(String name){
    .........
    return new Sample2A
    } }
    public class BombFactory extends Factory{
    public Sample creator(){
    ......
    return new SampleB
    }
    public Sample2 creator(String name){
    ......
    return new Sample2B
    } }

You might also like