Comp435 Object-Oriented Design Week 11 Computer Science PSU HBG 1 Define an interface for creating an object Let subclasses decide which class to instantiate Defer instantiation to subclasses Avoid the creation of specific objects from framework High-level framework Application-specific objects to create is not known Use abstract classes» Define and maintain relationship between objects Application Application-specific subclasses of the abstract class» Override the factory method 2 Cannot anticipate the objects to be created Want to let subclasses to specify details of objects to be created Localize the creation of various objects Product Creator FactoryMethod() AnOperation() product = FactoryMethod() ConcreteProduct ConcreteCreator FactoryMethod() return new ConcreteProduct 3 4 Product / ConcreteProduct Creator / ConcreteCreator Creator relies on ConcreteCreator ConcreteCreator Override the factory method of Creator Returns an instance of ConcreteProduct Provide hooks for subclasses Flexibility on creating objects Connect parallel class hierarchies 5 6 1
Parallel Class Hierarchies Figure Manipulator LineFigure TextFigure LineManipulator TextManipulator 7 8 Without violating encapsulation, capture and externalize an object s internal state so that the object can be restored to this state later Want to store private state information of object Ex: support UNDO operation Want to avoid violating encapsulation Object s internal state should be available Object s internal state should not be visible to other objects Solution Use an object that stores snapshot of another object 9 A snapshot of whole/part of an object s state must be saved A direct interface to obtaining the state may expose internal implementation 10 Originator Set( m) Create() state return new (state) GetState() SetState() state state = m GetState() memento Caretaker 11, Originator, Caretaker Caretaker Requests a memento from an originator Holds it for a time Passes it back to the originator Originator Creates a memento upon Caretaker s request Sets the memento s state with its current state Retrieves the memento s state to restore its state Passive object 12 2
Preserves encapsulation boundaries Simplifies Originator May be expensive Hidden cost to Caretaker 13 14 Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Want to reduce number of classes Ex: Building car engines Have common structures (material, shape, etc) Some attributes are different (4 valves vs 6 valves, etc)» Use inheritance: leads to class cluttering 15 Classes to instantiate are specified at run time Want to avoid parallel class hierarchy of factories Instances of a class can have one of only a few different combinations of state 16 Operation() p=prototype Concrete1 Concrete2 / Concrete asks a prototype to clone itself return copy of self return copy of self 17 18 3
Adding / removing products at run-time Specifying new objects by varying values Specifying new objects by varying structures Reduced subclassing Shallow copying Default copy constructor Deep copying Define your own copy constructor 19 20 Provide a surrogate or placeholder for another object to control access to it Need a more versatile or sophisticated reference than a simple pointer Control access to the actual objects by authentication Cache results from actual object for faster access Lazy initialization of expensive objects 21 Remote proxy Local representative for an object in a different address space Virtual proxy Lazy creation of expensive objects on demand Protection proxy Controlled access to the original object for protection Smart reference Additional actions when an object is accessed 22 RealSubject realsubject Subject realsubject Subject / RealSubject forward requests to RealSubject when appropriate 23 24 4
Introduces a level of indirection Remote proxy Disguise remote objects as local objects Virtual proxy Perform optimization such as lazy creation Protection proxy / smart reference Allow additional housekeeping when object is accessed 25 26 5