Berner Fachhochschule Engineering and Information Technology
The Abstract Factory Pattern Prof. Dr. Eric Dubuis Berner Fachhochschule, Engineering an...
Berner Fachhochschule Engineering and Information Technology
The Abstract Factory Pattern Prof. Dr. Eric Dubuis Berner Fachhochschule, Engineering and Information Technology @ Biel Course "UML and Design Patterns" of module "Software Engineering and Design", version May 2008
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 1 of 9
Berner Fachhochschule Engineering and Information Technology
The Problem Given a framework, and the framework itself uses a set of different, but similar Command objects. You want, however, allow your framework users to provide their concrete implementations of these commands.
Client
Command
??
How can Client instantiate these classes?
CutCommand {abstract}
MoveCommand {abstract}
ResizeCommand {abstract}
...
...
...
CutCommand(...)
MoveCommand(...)
ResizeCommand(...)
MyCutCommand
MyMoveCommand
MyResizeCommand
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 2 of 9
Berner Fachhochschule Engineering and Information Technology
General Ideas for a Solution • Class Client does not know the concrete Command classes. • In stead, Client uses a (known) factory class. • A concrete instance of a factory knows how to instantiate the concrete Command objects.
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 3 of 9
Berner Fachhochschule Engineering and Information Technology
Adding a Command Factory to the Graphical Editor
JdtEditor
MoveCommand {abstract} MoveCommand(...)
+createMoveCommand(): Command
MyMoveCommand
YourMoveCommand ...
...
CutCommand {abstract} CutCommand(...)
+createMoveCommmand(): Command
...
MyCutCommand
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
YourCutCommand
...
Page 4 of 9
Berner Fachhochschule Engineering and Information Technology
The Classes We first start with the abstract Command classes: 1 2 3 4 5
public abstract class MoveCommand implements Command { protected MoveCommand(...) { ... } public void execute() { ... } ... }
1 2 3 4 5
public abstract class CutCommand implements Command { protected CutCommand(...) { ... } public void execute() { ... } ... }
et cetera.
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 5 of 9
Berner Fachhochschule Engineering and Information Technology
Class MyCommandFactory implements a set of factory methods that create Command objects: 1 public class MyCommandFactory implements CommandFactory { 2 3 public MoveCommand createMoveCommand(Shape s, Point origin, Point end) { 4 return 5 } 6 7 public CutCommand createCutCommand(View v, Shape s) { 8 return 9 } 10 } // MyCommandFactory Each factory method returns an concrete XxCommand object which the editor can use via the common XxCommand abstraction.
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 6 of 9
Berner Fachhochschule Engineering and Information Technology
Remarks • The methods of the factory class that create (here: Command) objects are so-called factory methods (as with the factory method pattern). • However, the only responsibility of the factory class is to create objects. No other business methods are supported by this class. This is in contrast to the factory method pattern: There, the class with the factory method has other responsibilities, too.
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 7 of 9
Berner Fachhochschule Engineering and Information Technology
The Structure of the Abstract Factory Pattern
Client
AbstractProduct1 {abstract} ...could be an inteface, too...
...
...
Concrete Factory2
AbstractProduct2 {abstract}
createProduct1() createproduct2() ...
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis
Page 8 of 9
Berner Fachhochschule Engineering and Information Technology
References • [GoF]
E. Gamma et al, "Design Patterns -- Elements of Reusable Object-Orient Software", Addison-Wesley, 1995, ISBN 0-201-63361-2.
• Vince Huston's illustration of the command pattern: http://home.earthlink.net/%7Ehuston2/dp/abstract_factory.html
BFH/TI/Software Engineering and Design/UML and Design Patterns/Abstract Factory/May 2008/Prof. Dr. E. Dubuis