Factory Design Pa0ern COMP 401 Spring 2013 Lecture 16 3 5 2013 Factory Design Pa0ern When direct construcCon of an object is harmful or at least undesired By direct construcCon I mean by using the new keyword Several di erent contexts when factory design pa0ern is appropriate Singleton When there should only be one instance of a parCcular class in the whole system MulCton When there should only be one instance of of an object associated with some idenCfying property Dynamic subclass binding When the speci c subclass of an object can only be determined at the Cme that it is created Complex construcCon When the construcCon of the object requires complex validaCon and or side e ects that must be handled When null should be a valid result The Basic Factory Pa0ern Make the constructor private This prevents direct use of the new keyword for creaCng new objects outside of the class Provide staCc class methods that construct new instances and return them These are the factories Return type of the factory method is the same as the class class FPClass private FPClass Private constructor public staCc FPClass factoryMethod return new FPClass FPClass o FPClass factoryMethod Singleton One Object To Rule Them All When there should only be one instance of a parCcular class in the whole system at any given Cme Generally the object represents some sort of system wide resource that may be needed by many objects in di erent parts of the sobware Modi es basic factory pa0ern by maintaining a single instance Created on demand when rst requested Lazy iniCaCon Stored in a private staCc variable and retrieved by a staCc ge0er StaCc ge0er is the factory method lec16 v1 Simple logging mechanism lec16 v2 A singleton variant that employs delegaCon MulCton Maintains a single instance of the object with regard to a uniquely idenCfying characterisCc of object MulCton factory method StaCc as per the general factory pa0ern Provided all info needed to create a new object if necessary Determines if corresponding object already exists If so returns the exisCng object If not creates the new object and inserts it into a staCc structure Usually implemented using a map Map K V and HashMap K V Map is a collecCon of key value pairs SomeCmes called a dicConary Java CollecCons Framework Interface Map K V ImplementaCon HashMap K V K and V are placeholders for type names K for the type of the key and V for the type of the value Must be reference types Basic operaCons CreaCng a new map Map K V m new HashMap K V InserCng a value associated with a key m put key value Retrieving a value associated with a key V value m get key Checking to see if key already in the map m containsKey key Returns boolean Removing a key value pair from the map m remove key MulCton Example lec16 v3 Student is a class that models students Uniquely idenC ed by PID property getStudent is factory method NoCce that all info needed to create student is provided lookupStudent is a factory method variant that only retrieves exisCng objects Does not create object if it doesn t exist but must signal that fact In example this is done by returning null What would another opCon have been lec16 v4 Integer reference type Map K V can only work with reference types but the key we want to use is simple integer Java provides reference type versions of basic value types for these kinds of situaCons Dynamic Subclass Binding Useful when choice needs to be made between several di erent subclasses Delegates decision about which to use to factory Factory method given any all informaCon that is relevant to decision and for creaCng new object if necessary lec16 v5 A rose by any other name Some design pa0ern textbooks frameworks associate the name Factory speci cally and only for dynamic subclass binding use case Other cases described as separate pa0erns In my presentaCon I ve grouped all of these use cases under the name Factory more generally Just something to be aware of if when you encounter the term in tutorials documentaCon etc
View Full Document
Unlocking...