Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
-
Upload
lynn-holmes -
Category
Documents
-
view
213 -
download
1
Transcript of Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
![Page 1: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/1.jpg)
Creational Patterns
CSE301
University of Sunderland
Harry R Erwin, PhD
![Page 2: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/2.jpg)
Pattern Languages
• Alexander (1977) invented the idea of a pattern language as a practical tool for describing architectural expertise in some domain.
• The elements of a pattern language are patterns. Each pattern describes a problem that occurs over and over again, and then describes the core of the solution to that problem in such a way that it can be reused many times, never once doing it the same way.
• A pattern isn’t considered proven until it has been used at least three times in real applications.
![Page 3: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/3.jpg)
Design Patterns
• The four essential elements (Gamma, et al) of a design pattern are:– A descriptive name – A problem description that shows when to apply the
pattern and to what contexts. The description explains how it helps to complete larger patterns.
– A solution that abstractly describes the constituent elements, their relationships, responsibilities, and collaborations.
– The results and trade-offs that should be taken into account when applying the pattern.
![Page 4: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/4.jpg)
Resources
• Gamma, Helm, Johnson, and Vlissides, 1995, Design Patterns, Addison-Wesley.
• Cooper, 1998, The Design Patterns Java Companion, Addison-Wesley, available free from http://www.patterndepot.com/put/8/JavaPatterns.htm,the sample code in the book is available as http://www.patterndepot.com/put/8/JavaPatterns.ZIP.
• The Portland Pattern Repository: http://c2.com/ppr/• Alexander, 1977, A Pattern Language: Towns/Buildings/
Construction, Oxford University Press. (For historical interest.)
![Page 5: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/5.jpg)
Some Creational Patterns
• Builder
• Factory Method
• Abstract Factory
• Prototype
• Singleton
![Page 6: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/6.jpg)
Builder• Ever need to build a complex object in a specified way from
different concrete classes? E.g., an airplane has wings, tail, fuselage, engines, landing gear, etc., all selected from families of components.
• The Builder class is an abstract interface for a construction process that puts together a system of a specific type.
• Positives:– Encapsulation– Code isolation– Fine control over the build process
• Negatives:– Only certain types of composite objects lend themselves to a build
process. And don’t build a whale using the build process for a bird.
![Page 7: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/7.jpg)
Factory Method
• You need an object, but which subtype of the object needs to be decided later. This can be a particularly problem during initialization, as it means the file/object loader has to know every possible subclass.
• The solution to this is called the ‘factory method’ or ‘object factory’.
• Positives:– Flexibility– Can connect parallel class hierarchies
• Negatives:– Forces subclassing of the base class
![Page 8: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/8.jpg)
Abstract Factory• Ever need to provide a family of related or dependent objects,
where the underlying concrete classes need to be specified based on implementation details not of concern to the using program?
• The pattern that does this is called an ‘abstract factory’. We will discuss Modified Visual Proxy in Lecture 19.
• Positives: – Enforces encapsulation– Makes exchanging families easy– Produces consistency
• Negatives:– Adding new families is difficult. Each family needs a concrete factory
implementing the AbstractFactory. If many factors must be considered, there can be an explosion in the amount of code.
![Page 9: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/9.jpg)
Prototype• Uses clone() rather than new to create objects. You can specialize a
prototype object at program start-up to address implementation issues that cannot be specified at compile time.
• Positives:– Allows you to bind design details at run-time.– Allows you to define a pseudo-subclass at run-time parameterized or indexed by a
numeric value.– Allows you to duplicate object data if a large set of objects need to be defined with
common characteristics.– Avoids excessive subclassing. – Allows you to do dynamic loading of classes.
• Negatives:– Everything needs to implement clone().– Designing this approach into a class is highly non-trivial.
![Page 10: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/10.jpg)
Singleton• Ensures a class has a globally accessible unique instance.• Sometimes you need a class with only one instance, and sometimes
you need global access to a class. • Positives:
– Avoids the need to link objects at system initialization.– Sometimes you need it badly.– Supports persistence. The local copy can be a proxy for a copy kept on disk.– Reduced name space pollution.– Can be modified to support multiple instances using a Map.
• Negatives:– Has to be carefully implemented.– Sometimes it’s just not useful.
![Page 11: Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649eb35503460f94bb9c02/html5/thumbnails/11.jpg)
Examples from Book