939f4fff825a42dcade68a79d9d51cd7.ppt
- Количество слайдов: 23
Creational Design Patterns CSC 335: Object-Oriented Programming and Design 1
Outline Three Creational Design Patterns Singleton Factory Abstract 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 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
Participant Singleton • Defines a constructor that returns the (single) instance of this class. Store this instance in the class (unique. Instance). • Defines operations (Singleton. Operation()) and data (get. Singleton. Data()) for this instance. • Optionally, may also define a static method (Instance()) to return the instance, instead of a constructor (in this case, the constructor would be a private method). 8
Implementing Singleton in Java Make constructor(s) private so that they can not be called from outside. Declare a single static private instance of the class. Write a public get. Instance() or similar method that allows access to the single instance. 9
Use Example: A Phone. State class Phone. State { private static Phone. State p. S = new Phone. State(); public static Phone. State get. Instance() { return p. S; } private Phone. State() {} public int get. Num. People() { … } } Patterns I P-10 10
An Alternative Approach class Phone. State { private static Phone. State p. S; public static Phone. State get. Instance() { if (p. S == null) { p. S = new Phone. State(); } return p. S; } private Phone. State() {} public int get. Num. People() { … } } Patterns I P-11 11
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. 12
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 13
General Form http: //www. dofactory. com/Patterns/Pattern. Factory. aspx 14
• Product (Page) • defines the interface of objects the factory method creates • Concrete. Product • implements the Product interface • Creator • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default Concrete. Product object. • may call the factory method to create a Product object. • Concrete. Creator (Report, Resume) 15 • overrides the factory method to return an instance of a
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 16
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); 17
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); 18
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 19
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>(); 20
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 € 21
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 22
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 23
939f4fff825a42dcade68a79d9d51cd7.ppt