More OO Design Patterns CSC 335 ObjectOriented Programming
More OO Design Patterns CSC 335: Object-Oriented Programming and Design 1
Outline Three Creational Design Patterns Singleton Factory 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 Singleton Recurring Problem • Some classes have only one instance. For example, there may be many printers in a system, but there should be only one printer spooler • How do we ensure that a class has only one instance and that instance is easily accessible? Solution • Have constructor return the same instance when called multiple times • Takes responsibility of managing that instance away from the programmer • It is simply not possible to construct more instances 5
UML General form as UML (From http: //cvs. m 17 n. org/~akr/mj/design-pattern/en/design-pattern. html) 6
Java Code General Form // NOTE: This is not thread safe! public class Singleton { private static Singleton unique. Instance; // other useful instance variables here private Singleton() {} public static Singleton get. Instance() { if (unique. Instance == null) { unique. Instance = new Singleton(); } return unique. Instance; } // other useful methods here } 7
Example Used in a final project names changed to protect identity /** This class is a DECORATOR of Array. List. Its purpose is to make * sure there are no duplicate names anywhere in the universe. * That's why it's SINGLETON; because many classes use it but * there should be only one. */ public class Names. List implements Serializable { private Array. List<String> npc. Names; private static Names. List self; private Names. List() { npc. Names = new Array. List<String>(); } public static syncronized Names. List get. Instance() { if (self == null) { self = new Names. List(); } return self; } 8
OO Design Pattern Factory Method Name: Factory Method 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. 9
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 10
Example from Java Border is an interface Abstract. Border is an abstract class that implements Border return priority + ": " + my. Text; Border. Factory has a series of static methods returning different types that implement Border This hides the implementation details of the subclasses Factory methods such as create. Matte. Border create. Ethed. Border create. Title. Border directly call constructors of the subclasses of Abstract. Border 11
One type JFrame f = new JFrame(); f. set. Size(250, 100); f. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); 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); f. get. Content. Pane(). add(to. Be. Bordered); f. set. Visible(true); 12
Another type JFrame f = new JFrame(); f. set. Size(250, 100); f. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); 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); f. get. Content. Pane(). add(to. Be. Bordered); f. set. Visible(true); 13
Two others Border border = Border. Factory. create. Titled. Border("Title"); Border border = Border. Factory. create. Line. Border(Color. GREEN, 12); 14
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 15
Number. Format, a factory 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 Locale $12, 345. 12 Use computer setting to Germany Locale and we get this: Number. Format. get. Currency. Instance(Locale. GERMANY); 12. 345, 12 € 16
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 17
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 18
public class Two. Factories { public static void main(String[] args) { JFrame f = new JFrame(); f. set. Size(250, 100); f. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); JPanel to. Be. Bordered = new JPanel(); Border border = Border. Factory. create. Matte. Border(2, 1, 5, 9, Color. RED); // // // Border border = Border. Factory. create. Etched. Border(); Border border = Border. Factory. create. Titled. Border("Title"); Border border = Border. Factory. create. Line. Border(Color. GREEN, 12); to. Be. Bordered. add(new JLabel("" + border. get. Class())); to. Be. Bordered. set. Border(border); f. get. Content. Pane(). add(to. Be. Bordered); f. set. Visible(true); /* double amount = 12345. 1234656789457; Number. Format formatter = Number. Format. get. Currency. Instance(); // Number. Format formatter = Number. Format. get. Currency. Instance(Locale. UK); System. out. println(formatter. format(amount)); */ } } 19
- Slides: 19