Posts

Showing posts with the label diagram

Singleton Design Pattern by example

Image
Singleton design pattern is most commonly used among all available java design patterns. It is a Creational patterns. Basic purpose of this design pattern is to maintain only one instance of a class throughout the application. This class may represent a common shared resource, functionality etc. When to Use ? You should consider singleton design pattern in following scenario A resource is shared across all application component and only one instance of resource is possible. Global variables are being passed across all part of application Multiple instances representing same kind of information. How to Use ? It is very simple to implement this design pattern. Following are he key requirements and possible solution for implementing singleton design pattern. Only one instance should be available at the time – restrict the class instantiation using private scoped constructor. Everybody should get same instance – maintain a private static instance variable ...

Composite Design Pattern by example

Image
This post explains Composite design pattern with an example. When to use ? – Use Composite pattern when you have treat a collection of objects as they were once one thing. With Composite design pattern it is very easy to simplify operations over collection data structure. It reduces complexity of the code required if you were going to handle collection as special case. How to Use? - The Composite pattern consist of Component, Leaf and Composite Classes as shown below class diagram Component – Component declares the common interface that both the single and composite nodes will implements Leaf – The leaf class represent the singular atomic data type implementing he component interface.     Composite – Composite is an abstract entity which declares common functionality which are needed by concrete implementation for handling collection data. An Example -   Item.java public interface Item {     public double getPrice(); ...