Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "Overview of Creational Patterns ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 Overview of Creational Patterns ©SoftMoore ConsultingSlide 1

2 Creational Patterns Creational design patterns are concerned with the process of object creation. Typically they defer some part of object creation to another class or object. Details of classes that are instantiated are encapsulated and hidden from the client class, which knows only about an abstract class or the interface it implements. Creational patterns go beyond simply instantiating a class, and they help to make parts of a system independent of how its objects are created. ©SoftMoore ConsultingSlide 2

3 Why not new() ? The simplest way to create an object (instance) in Java (and lots of other programming languages) is by using the new() operator to allocate and initialize memory. Student s = new Student(); Using the new() operator explicitly is generally acceptable, but it hard codes the class of the object into the creation process, and in many cases the exact class being created varies with the need of the program. Example: You have a list of Shape objects, which could be circles, rectangles, triangles, etc. How would you write the code to duplicate that list? Note: You are required to duplicate every shape on the list. ©SoftMoore ConsultingSlide 3

4 Recurring Themes Creational patterns encapsulate knowledge about which concrete classes the system uses. Creational patterns hide how instances of those classes are created and put together. All the system knows about the objects is their interfaces. ©SoftMoore ConsultingSlide 4

5 Common Example in GoF Book Building a maze for a computer game –set of rooms with walls and doors –each room knows its neighbors Classes –MapSite (common abstract class for all maze components) –Room –Wall –Door –Maze (represents a collection of rooms) The maze and game vary slightly from pattern to pattern. ©SoftMoore ConsultingSlide 5

6 Creational Design Patterns Abstract Factory – Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Builder – Separate the construction of a complex object from its representation so that the same construction process can create different representations. Factory Method (a.k.a. Virtual Constructor) – Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses. ©SoftMoore ConsultingSlide 6

7 Creational Design Patterns (continued) Singleton – Ensure a class has only one instance, and provide a global point of access to it. Prototype – Specify the kinds of objects to create using a prototypical instance, and create new objects by cloning this prototype. Lazy Initialization (not a GoF pattern) – Delay creation of an object, calculation of a value, or some other expensive process until the first time it is needed. Object Pool (not a GoF pattern) – Avoid expensive acquisition and release of resources by recycling objects that are no longer in use. ©SoftMoore ConsultingSlide 7

8 Using Creational Patterns Sometimes creational patterns are competitors. –You might be able to chose effectively from either Prototype or Abstract Factory At other times they are complementary –Abstract Factory might store a set of Prototypes from which to clone and return product objects –Builder can use one of the other patterns to implement which components get built. –Abstract Factory, Builder, and Prototype can use Singleton in their implementation. ©SoftMoore ConsultingSlide 8

9 References Creational pattern (Wikipedia) –http://en.wikipedia.org/wiki/Creational_patternhttp://en.wikipedia.org/wiki/Creational_pattern Creational patterns (Source Making) –https://sourcemaking.com/design_patterns/creational_patternshttps://sourcemaking.com/design_patterns/creational_patterns Effective Java (Second Edition) by Joshua Bloch, Addison-Wesley, 2008. ISBN: 978-0321356680 (Second chapter is devoted to creating and destroying objects.) ©SoftMoore ConsultingSlide 9


Download ppt "Overview of Creational Patterns ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google