Skip to main content

Posts

Showing posts from April, 2017

Decorator Pattern : Real Life Example and Java Code Example

Decorator Pattern The Decorator Pattern provides a mechanism to dynamically attach the additional responsibilities to an object at runtime. Inheritance also provides the same but it is not flexible and does statically. We will see further in detail why Inheritance is not a good option as compared to Decorator Pattern. One important thing about the Decorator Pattern, It does not affect the core functionality just attach some additional. Real Life Example: Take the example of a bicycle store. When someone comes to buy a bicycle, The distributor shows the basic bicycle with core functionalities. After selecting the bicycle, He asks for the accessories you want to attach. Suppose there are accessories like carrier,stand-leg,front-light,front-box,bike-bell. The service man decorates your bicycle with accessories(additional functionalities without affecting the core functionality) as per your requirement. Java Code Example: We will try to implement the above real life exam

Factory Pattern : Real Life Example and Java Code Example

Factory Pattern The Factory Pattern is actually a method(factory method) which is used to create objects on the basis of runtime requirements. Real Life Example: The factory method is same as the real world factory, It gives the same kind of object with different(based on the input) behavior. For example, There is a machine in a factory which takes wood and paints as input and gives a painted box as the final product. We can create different boxes by just changing the color of paint in the input. Java Code Example: Before going to codes, We need to see some key points about factory method implementation. We need an interface(Suppose A is an interface). Then We need various implementations of A (Suppose B, C, D are implementing A). Finally, We need a Factory class with a factory method. The factory method must have the return type of A. Find the below problem and its solution using the factory method. Suppose We need to build an application KNOW ABOUT ANIMALS. The a

Singleton Pattern : Real Life Example and Java Code Example

Singleton Pattern The Singleton pattern uses to prevent the instantiation of an Object or creating a new one. It is used when We need only one instance in the whole application. The singleton is similar to global variables and we can find many debates over singleton vs global variables on the internet. We can choose between singleton and global variables based on the application context, But global variables violate the encapsulation policy of OOP concepts. Real Life Example: The DataSource is very good example of a singleton pattern. We initialize DataSourcet object by some parameters like host,port,user and password. Every time We need the DataSource, the values of the parameters remains same So We need exactly one instance of DataSource. Java Code Example: There is the various implementation of the singleton, based on the initialization like eager, lazy, static block and thread safe. Below implementation called Bill Pugh Singleton Implementation is simplest and also threa