You are on page 1of 8

Creational Design Patterns

Software Engineering Seminar Report Cristian Voicu May 19, 2013

Introduction

The motivation for the Creational Patterns comes with the development of the software eld. Creational Patterns permits to an object to be created from another object without knowing the implementation of the last one. This is based on object composition and dier from class inheritance. Object composition it means to compose simple objects in order to create a complex object. A real world example of object composition is a car that is composed of wheels, an engine, doors. Class inheritance it means that a classes inherit some commonly characteristics and behavior of another class. A real world example is a hierarchy of bicycle [3] :

Figure 1: Example of Class inheritance.

This paper summarize the following patterns: Factory Method, Abstract Factory, Builder, Prototype and Singleton. These are creational design patterns. The report is based on the presentation of Michiels Sbastien and Orban Pierre-Yves and on the book Design Patterns Elements of Reusable Object-Oriented Software [1]

Factory Method

Denition: Factory Method is a creational pattern that emphasizes interfaces for creating classes and it let the subclasses to decide which classes to be instantiated. In this way we separate the creation and the representation of classes in order to hide the complex process of the creation. Confusion: There exists another pattern called Factory pattern that is not listed in the book: Design Patterns Elements of Reusable Object-Oriented Software. The Factory pattern dene an interface for creating an object. We can say that it does the same thing like the Factory Method Pattern. But not. The Factory Method has one propriety more than the Factory pattern. The Factory Method does what the Factory pattern does and in addition it let the subclasses to decide about instantiation. Factory Method is used to isolate classes from the client. That means the client is not concerned about how the systems work inside. In addition is used because of easiness of changing the whole product family by changing the interface.

Figure 2: Factory Method example class diagram.[2]

The picture above illustrate an example of the Factory Method pattern. Basically the EnemyShipFactoy is link between the user and the creation of the dierent type of Ship. The user specify what kind of Ship he wants and the EnemyShipFactoy will create that specic Ship object.

Abstract Factory

Denition: The abstract factory is a creational design pattern. It is a factory of factories, or better, is a factory of families of objects (products). As we can see in the next picture, the members of a family of objects are designed to be used together. The abstraction allow a system to be independent of how its products are created, composed and represented. [1]

Figure 3: Abstract Factory class diagram.

Benets: It isolates concrete classes: Is more easy to control the objects because of encapsulation. Easy to exchange product families because the factory appears only where is instantiated. Constancy among products. Only objects from one family are used at a time. Drawbacks: Dicult to add new of families because to add a new family, we need to change the AbstracFactory class and all its subclasses. Example: The example of the presentation illustrate well the Abstract factory pattern. Homer wants to move from the Sony world (TV and Stereo) to the Samsung world (TV and Stereo). By using Abstract Factory he is able to use the object Remote for both TV and Stereo without interesting for which mark is used. Here, Abstract Factory generate two factories for Sony and for Samsung. For each of this, Factory Method return the concret object.

Figure 4: Abstract Factory Homer example.[5]

Comparison of patterns: Factory Method and Abstract Factory The Factory method we can produce implementation of a particular interface (e.g IFruits) that can result in a specic object (e.g. Apple, Banana, ...). The Abstract Factory is more generic, we can produce dierent factories, e.g IFruitFactory which is an interface that permits to implement dierent kinds of fruits. Also we can produce ILegumsFactory that can result in some specic objects (e.g. Tomato, ...). Abstract Factory is one level more of abstraction than the Factory Method. [4]

Builder

The main goal of the builder is to separate the construction of a complex object form his representation. So, with the same construction process we can create dierent representations. Suppose the case where the class of the object has many constructors and the constructors has many parameters. In this situation is very dicult to handle all constructors and the order of all parameters. Take the example of Pizza product: The design in the naive way can result in a class with many parameters like: Pizza(cheese) Pizza(cheese, tomato) Pizza(cheese, tomato, bacon) ...

With the builder pattern a solution of this problem can look like: Builder() ... Pizza(Builder) ...... Pizza pizza = new Pizza(new Builder().cheese().tomato().bacon()) The methods cheese, tomato, bacon, returns a builder so they can be written like in the above example.

Figure 5: Builder pattern, Pizza example.

In the picture above, the phases (1), (2), (3), (4), (5) are construction phases. Dierent representations are noted by (a), (b), (c).

Prototype

The prototype pattern is a creational design pattern that allows to create objects from another object without interesting about their construction. The key word

here is cloning. Rather than creating from scratch the object, this is cloned from another object. This pattern is used when the construction of an object is costly in terms of time for example. By cloning it, we reduce the time of creation. A real world example is the photo-copy of a paper.

Figure 6: Prototype pattern, photo-copy example.

In the above image, User1 writes a paper (1) and for writing, it took some time. Next he wants to photo-copy (2) that paper. The paper is photo-copied (3) in less time than the time it takes for writing. Now User1 has two identical paper and gives the copy (4) to User2. User2 can modify the paper (correct the paper for example) and send back the User1. This example illustrates the interest of the Prototype pattern. Is more easy to create a copy of an object and to modify some proprieties and use this copy like a stand alone object.

Singleton

The goal is to have only an instance of an object that is perpetuated through the whole system. The interest to have an instance of an object that is accessible through all system is when such an object should coordinate actions in the system. 6

The question is how to be sure that is only one instance of an object? Singleton makes the class itself responsible for keeping track of self instance by intercepting requests to create new objects. This intercepting mechanism check every time when an object wants to be created if one instance of that object already exists. If no instance exist, a new one will be created, but if an instance already exists then this instance of the object that already exists will be returned. There is one more issue. What about using threads? The next picture shows three cases:

Figure 7: Singleton pattern working with threads.

The picture above illustrate three situations of a program that use threads. The thread T initialize with null the variable inst. Then, the thread T splits in thread T1 and thread T2. The rst case (a) is a normal one when T1 nish the instantiation of inst and 7

T2 will not execute the code inside if. Therefore, no problem for the number of instances. There is only one instance created by T1 and the same instance is used by T2. In the second case (b) is an issue. The two threads are simultaneous in the if statement. The thread T1 creates an instance and T2 creates an instance also. Therefore, each thread will have his one instance of inst and the singleton pattern is not respected. This issue is solved in the third case (c). In the last case (c) we see how Singleton should deal with threads. To ensure that there is no more than one instance of an object in the program, only one thread at a time should be allowed to enter in the if statement. So, when T1 is in the if statement, T2 must wait for T1 to nish the if statement and then to execute the if. This can be done in Java with synchronize statement. In this way we can be sure that even with threads the Singleton pattern can be used with the insurance that in the program there is only one instance of the object.

References
[1] R. Johnson E. Gamma, R. Helm and J. Vlissides. Design Patterns Elements of Reusable Object-Oriented Software. 1995. [2] Newthinktank. Factory design pattern tutorial. http://www.newthinktank. com/2012/09/factory-design-pattern-tutorial/. [3] Oracle. What is inheritance? http://docs.oracle.com/javase/tutorial/ java/concepts/inheritance.html. [4] Stackoverow. Dierence between factory and abstract factory patterns. http://stackoverflow.com/questions/1001767/ what-is-the-basic-difference-between-factory-and-abstract-factory-patterns. [5] Michiels Sbastien and Orban Pierre-Yves. Creational patterns. icampus. uclouvain.be/claroline/work/user_work.php?cmd=exDownload&authId= 12&assigId=1&workId=60&cidReset=true&cidReq=INGI2359.