Posts

Showing posts with the label by

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 ...

Understanding Observer Designing Pattern by Example

Image
  “The Observer pattern define a one to many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.” With the observer pattern, the source is the object that contain the state and control it. The Observer on the other hand use the state, even if they don’t own it. The observer pattern provides an object design where the subject and observer are loosely coupled. Here is the Class Diagram explaining Benefit of loosely coupling The only thing the subject knows about an Observer is that it implements a certain interface We can add new Observer at any time We never need to modify subject to add more observer. We can use subject or observer independently of each other. Change in either observer or Source will not affect the other. Here is an example : This example explains Observer pattern, here source generates random characters and observer, Java Swing component listen this eve...