The construction process must allow different

This preview shows page 25 - 31 out of 68 pages.

The construction process must allow different representations for the object that is constructed. Complex objects need to be created that have a common overall structure and interface, even though their internal behavior and detailed structure may be different. Builder pattern: context
Concordia Department of Computer Science and Software Joey Paquet, 2006-2014 26 SOEN 6441 - Advanced Programming Practices A Builder lets you vary the internal representation of the product it builds. It also hides the details of how the product is assembled. It provides construction abstraction . Each specific builder is independent of the others and of the rest of the program. This improves modularity and makes the addition of other builders relatively simple. Because each builder constructs the final product step-by- step, you have more control over each final product that a Builder constructs. Builder pattern: consequences
Concordia Department of Computer Science and Software Singleton pattern Joey Paquet, 2006-2014 27 SOEN 6441 - Advanced Programming Practices
Concordia Department of Computer Science and Software Joey Paquet, 2006-2014 28 SOEN 6441 - Advanced Programming Practices Sometimes it is appropriate to have exactly one instance of a class: window managers, print spoolers, filesystems. Typically, those types of objects known as singletons, are accessed by disparate objects throughout a software system, and therefore require a global point of access. The Singleton pattern addresses all the concerns above. With the Singleton design pattern you can: Ensure that only one instance of a class is created. Provide a global point of access to the object. Allow multiple instances in the future without affecting a singleton class' clients. Singleton pattern
Concordia Department of Computer Science and Software Joey Paquet, 2006-2014 29 SOEN 6441 - Advanced Programming Practices The Singleton pattern ensures a class has only one instance, and provides a global point of access to it. The class itself is responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. Singletons maintain a static reference to the sole singleton instance and return a reference to that instance from a static instance() method. Singleton pattern
Concordia Department of Computer Science and Software Singleton pattern Joey Paquet, 2006-2014 30 SOEN 6441 - Advanced Programming Practices public class SingleObject { /** * create an object of SingleObject embedded as a static member of the class itself */ private static SingleObject instance = new SingleObject(); /** * Make the constructor private so that this class cannot be instantiated */ private SingleObject(){} /** * If the instance was not previously created, create it. Then return the instance */ public static SingleObject getInstance(){ if ( instance == null ) instance = new SingleObject(); return instance ; } public void showMessage(){ System. out .println( "Hello World!" ); } } public class SingletonDriver { public static void

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture