



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Abstract Factory Motivation
1) Implement a user interface toolkit that supports multiple looks
and feel standards such as Motif, Windows 95 or the finder in
MacOS.
How can you write a single user interface and make it portable
across the different look and feel standards for these window
managers?
2) Implement a facility management system for an intelligent
house that supports different control systems such as Siemens’
Instabus, Johnson & Control Metasys or Zumtobe’s proprietary
standard.
How can you write a single control system that is independent
from the manufacturer?
The Client remains blissfully unaware of the various concrete classes in this
example. Client code deals with the simpler, abstract, general case.
Solution: ShapeFactory interface.
Shape
<
Square Circle
ShapeFactory
Implementation
<
SomeApp
ShapeFactory
<
makeSquare()
makeCircle()
Problem: Every time we add a new Shape derivative, we have to add
a method to the ShapeFactory.
Solution:
public interface ShapeFactory {
public Shape make (String shapeName) throws Exception
}
public class ShapeFactoryImplementation implements ShapeFactory {
public Shape make(String shapeName) throws Exception {
if (shapeName.equals("Circle")) return new Circle();
else if (shapeName.equals("Square")) return new Square();
else
throw new Exception("ShapeFactory cannot create " + shapeName);
}
}
………………………
private ShapeFactory factory;
factory = new ShapeFactoryImplementation();
Shape s = factory.make("Circle");
Intent:
Define an interface for creating an object, but let subclasses decide which class to
instantiate.
Factory Method lets a class defer instantiation to subclasses.
Structure
Problem:
A reader for the RTF (Rich Text Format) document exchange format should be able
to convert RTF to many text formats.
The problem is that the number of possible conversions is open-ended. So it
should be easy to add a new conversion without modifying the reader.
Solution:
to configure the RTFReader class with a TextConverter object that converts RTF to
another textual representation.
Subclasses of TextConverter specialize in different conversions and formats.
Intent:
Separate the construction of a complex object from its
representation so that the same construction process can create
different representations.
Structure
Collaborations :
The client creates the Director object and configures it with the desired Builder object.
Director notifies the builder whenever a part of the product should be built.
Builder handles requests from the director and adds parts to the product.
The client retrieves the product from the builder.