Creational Design Patterns CSC 335 ObjectOriented Programming and
Creational Design Patterns CSC 335: Object-Oriented Programming and Design 1
Outline Three Creational Design Patterns Singleton Factory Prototype 2
To use new or to not use new? That is the question Since most object-oriented languages provide object instantiation with new and initialization with constructors There may be a tendency to simply use these facilities directly without forethought to future consequences The overuse of this functionality often introduces inflexibility in the system 3
Creational Patterns Creational patterns describe object-creation mechanisms that enable greater levels of reuse in evolving systems: Builder, Singleton, Prototype The most widely used is Factory This pattern calls for the use of a specialized object solely to create other objects 4
OO Design Pattern Factory Design Pattern Name: Factory Problem: A Client needs an object and it doesn't know which of several objects to instantiate Solution: Let an object instantiate the correct object from several choices. The return type is an abstract class or an interface type. 5
Characteristics A method returns an object The return type is an abstract class or interface The interface is implemented by two or more classes or the class is extended by two or more classes 6
Example from Java Border is an interface Abstract. Border is an abstract class Border. Factory has a series of static methods returning different types that implement Border This hides the implementation details of the subclasses The factory methods directly call the constructors of the subclasses of Abstract. Border 7
One type set. Size(250, 100); JPanel to. Be. Bordered = new JPanel(); Border border = Border. Factory. create. Matte. Border(2, 1, 5, 9, Color. RED); to. Be. Bordered. add(new JLabel("" + border. get. Class())); to. Be. Bordered. set. Border(border); get. Content. Pane(). add(to. Be. Bordered, Border. Layout. CENTER); 8
Another type set. Size(250, 100); JPanel to. Be. Bordered = new JPanel(); Border border = Border. Factory. create. Etched. Border(); to. Be. Bordered. add(new JLabel("" + border. get. Class())); to. Be. Bordered. set. Border(border); get. Content. Pane(). add(to. Be. Bordered, Border. Layout. CENTER); 9
Lots of Subclasses javax. swing. border. Abstract. Border java. lang. Object javax. swing. border. Abstract. Border All Implemented Interfaces: Serializable, Border Direct Known Subclasses: Basic. Borders. Button. Border, Basic. Borders. Field. Border, Basic. Borders. Margin. Border, Basic. Borders. Menu. Bar. Border, Bevel. Border, Compound. Border, Empty. Border, Etched. Border, Line. Border, Metal. Borders. Button. Border, Metal. Borders. Flush 3 DBorder, Metal. Borders. Internal. Frame. Border, Metal. Borders. Menu. Bar. Border, Metal. Borders. Menu. Item. Border, Metal. Borders. Option. Dialog. Border, Metal. Borders. Palette. Border, Metal. Borders. Popup. Menu. Border, Metal. Borders. Scroll. Pane. Border, Metal. Borders. Table. Header. Border, Metal. Borders. Tool. Bar. Border, Titled. Border 10
Iterators? The iterator methods isolate the client from knowing the class to instantiate List<String> list = new Array. List<String>(); Iterator<String> itr = list. iterator(); System. out. println(itr. get. Class(). to. String()); What type is itr? class java. util. Abstract. List$Itr What type is itr with this change? List<String> list = new Linked. List<String>(); 11
Do we need new? Objects can be returned without directly using new double amount = 12345. 1234656789457; Number. Format formatter = Number. Format. get. Currency. Instance(); System. out. println(formatter. format(amount)); Output if the computer is set to US $12, 345. 12 Change the computer setting to Germany and we get this: 12. 345, 12 € 12
What Happened? get. Currency. Instance returns an instance of Decimal. Format where methods like set. Currency help build the appropriate object It encapsulates the creation of objects Can be useful if the creation process is complex, for example if it depends on settings in configuration files or the jre or the OS 13
Behind the scenes Client: main method Factory Method: get. Currency. Instance Product: a properly configured instance of Decimal. Format This is another example of Factory in use 14
- Slides: 14