The document discusses design patterns in software engineering, focusing on the Singleton and Factory design patterns. The Singleton pattern ensures a class has only one instance and provides global access to it, while the Factory pattern allows subclasses to decide which class to instantiate. Both patterns promote code reusability, encapsulation, and flexibility in object creation, with specific advantages and use cases outlined for each.
The document discusses design patterns in software engineering, focusing on the Singleton and Factory design patterns. The Singleton pattern ensures a class has only one instance and provides global access to it, while the Factory pattern allows subclasses to decide which class to instantiate. Both patterns promote code reusability, encapsulation, and flexibility in object creation, with specific advantages and use cases outlined for each.
SW Engineering By : Shereen Ali Design Patterns • The design patterns are communicating objects and classes that are customized to solve a general design problem in a particular context.
• A design pattern names, abstracts, and identifies the key aspects of a
common design structure that make it useful for creating a reusable object-oriented design. • The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities. Types of Design Pattern Types Design Patterns Singleton Design Pattern • Singleton Pattern says that just "define a class that has only one instance and provides a global point of access to it". • In other words, a class must ensure that only single instance should be created and single object can be used by all other classes. • There are two forms of singleton design pattern: Early Instantiation: creation of instance at load time. Lazy Instantiation: creation of instance when required Advantage and Usage of Singleton design pattern
Advantages: Saves memory because object is not created at each
request. Only single instance is reused again and again.
Usage : Singleton pattern is mostly used in multi-threaded and
database applications. It is used in logging, caching, thread pools, configuration settings etc. UML of Singleton How to create Singleton design pattern • To create the singleton class, we need to have static member of class, private constructor and static factory method. • Static member: It gets memory only once because of static, it contains the instance of the Singleton class. • Private constructor: It will prevent to instantiate the Singleton class from outside the class. • Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller. Understanding early Instantiation of Singleton Pattern • In such case, we create the instance of the class at the time of declaring the static data member, so instance of the class is created at the time of class loading. Java Code Understanding Lazy Instantiation of Singleton Pattern • In such case, we create the instance of the class in synchronized method or synchronized block, so instance of the class is created when required. Java Code Example 1 • In a software application for managing user preferences, there is a requirement to maintain a single instance of a user settings object throughout the application. The user settings object contains various user-specific preferences such as language settings, theme preferences, and notification preferences. These preferences need to be accessed and modified from different parts of the application. Solution • To ensure that there is only one instance of the user settings object throughout the application, we can use the Singleton pattern. By implementing the user settings object as a Singleton, we guarantee that all parts of the application interact with the same instance of the object, avoiding inconsistencies and synchronization issues that may arise from multiple instances. • Using the Singleton pattern, the user settings object class will have a private constructor to prevent external instantiation and a static method to provide access to the single instance. The first call to this method initializes the instance, and subsequent calls return the same instance. • By implementing the user settings object as a Singleton, we ensure that any changes made to the user preferences are reflected consistently across the entire application, providing a centralized and efficient solution to manage user preferences. Example 2 • In a gaming application, there is a need to track the score and progress of the player throughout the game. The score and progress data need to be accessed and updated from various game components such as levels, achievements, and user interface elements. However, it's essential to ensure that there is only one instance of the player's score and progress tracker to maintain consistency and avoid conflicts. Solution • To ensure that there is only one instance of the player's score and progress tracker throughout the game, we can use the Singleton pattern. By implementing the score and progress tracker as a Singleton, we guarantee that all parts of the game interact with the same instance of the tracker, ensuring consistency and avoiding synchronization issues that may arise from multiple instances. • Using the Singleton pattern, the score and progress tracker class will have a private constructor to prevent external instantiation and a static method to provide access to the single instance. The first call to this method initializes the instance, and subsequent calls return the same instance. • By implementing the score and progress tracker as a Singleton, we ensure that any changes made to the player's score and progress are reflected consistently throughout the game, providing a centralized and efficient solution to track the player's performance. Factory Design Pattern • It defines an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. • In other words, subclasses are responsible to create the instance of the class. • The Factory Method Pattern is also known as Virtual Constructor. Why Naming “Factory” • The word "factory" in the name emphasizes the role of the pattern in creating objects in a centralized and consistent manner, similar to how a manufacturing factory produces goods according to predefined specifications. • This abstraction helps in promoting code reusability, encapsulation, and flexibility in object creation. Advantages and Usage of Factory Design • Factory Method Pattern allows the sub-classes to choose the type of objects to create. • It promotes the loose-coupling by eliminating the need to bind application- specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class. Usage of Factory Design Pattern • When a class doesn't know what sub-classes will be required to create • When a class wants that its sub-classes specify the objects to be created. • When the parent classes choose the creation of objects to its sub-classes. UML of Factory Design Pattern