Design Pattern
Design Pattern
Builder design pattern in Java is a creational pattern i.e. used to create objects, similar to factory method
design pattern which is also creational design pattern. Before learning any design pattern I suggest find out
the problem a particular design pattern solves. Its been well said necessity is mother on invention.
Learning design pattern without facing problem is not that effective, Instead if you have already faced issues
than its much easier to understand design pattern and learn how its solve the issue. In this Java
design pattern tutorial we will first see what problem Builder design pattern solves which will give
some insight on when to use builder design pattern in Java, which is also a popular design pattern
interview question and then we will see example of Builder design pattern and pros and cons of using
Builder pattern in Java.
final
final
final
final
final
final
final
final
double sugar;
//cup
double butter; //cup
int eggs;
int vanila;
//spoon
double flour;
//cup
double bakingpowder; //spoon
double milk; //cup
int cherry;
double sugar;
//cup
double butter; //cup
int eggs;
int vanila;
//spoon
double flour;
//cup
double bakingpowder; //spoon
double milk; //cup
int cherry;
this.cherry = builder.cherry;
}
@Override
public String toString() {
return "Cake{" + "sugar=" + sugar + ", butter=" + butter + ", eggs=" +
eggs + ", vanila=" + vanila + ", flour=" + flour + ", bakingpowder=" +
bakingpowder + ", milk=" + milk + ", cherry=" + cherry + '}';
}
}
Output:
Cake{sugar=0.75, butter=0.5, eggs=2, vanila=2, flour=1.5, bakingpowder=0.0,
milk=0.5, cherry=0}
When sub classing is become impractical and we need large number of different possibilities
to make independent object or we can say we have number of combination for an object.
Secondly when we want to add functionality to individual object not to all object at run-time
we use decorator design pattern.
Code Example of decorator design pattern:
To better understand concept of decorator design pattern let see a code example
using Decorator Pattern in Java. You can also look inside JDK and find what are classes and
packages which are using decorator pattern.
// Component on Decorator design pattern
public abstract class Currency {
String description = "Unknown currency";
public String getCurrencyDescription() {
return description;
}
public abstract double cost(double value);
}
// Concrete Component
public class Rupee extends Currency {
double value;
public Rupee() {
description = "indian rupees";
}
public double cost(double v){
value=v;
return value;
}
}
//Another Concrete Component
public class Dollar extends Currency{
double value;
public Dollar () {
description = "Dollar;
}
public double cost(double v){
value=v;
return value;
}
}
// Decorator
public abstract class Decorator extends Currency{
// Concrete Decorator
public class USDDecorator extends Decorator{
Currency currency;
//adding decorators
1.
2.
3.
4.
1.
2.
In brief we see what the main advantages of using decorator design patterns are.
Decorator Pattern is flexible than inheritance because inheritance add responsibilities at
compile time and it will add at run-time.
Decorator pattern enhance or modify the object functionality
Disadvantage
Main disadvantage of using Decorator Pattern in Java is that the code maintenance can be a
problem as it provides a lot of similar kind of small objects (each decorator).
Observer design pattern in Java is a fundamental core Java pattern where Observe watch
for any change in state or property of Subject. For Example Company updates all its
shareholders for any decision they make here Company is Subject and Shareholders are
Observers, any change in policy of company and Company notifies all its Shareholders or
Observer. This was simple real world explanation of Observer pattern. In this article we will in
detail what is Observer Design pattern, what is benefit of Observer design Pattern, Example or
Observer pattern in Java and few other points. Just like Decorator design Pattern and Factory
Pattern in Java, Observer pattern is also used in JDK.
}
public class ObserverTest {
public static void main(String args[]) {
// this will maintain all loans information
Newspaper printMedia = new Newspaper();
Internet onlineMedia = new Internet();
Loan personalLoan = new Loan("Personal Loan", 12.5f,
"Standard Charterd");
personalLoan.registerObserver(printMedia);
personalLoan.registerObserver(onlineMedia);
personalLoan.setInterest(3.5f);
}
}
The disadvantage is that the sometime if any problem comes, debugging becomes very
difficult because flow of control is implicitly between observers and observable we can
predict that now observer is going to fire and if there is chain between observers then
debugging become more complex.
Another issue is Memory management because subject will hold all the reference of all the
observers if we not unregister the object it can create the memory issue.
Whenever we create object using new() we violate principle of programming for interface rather than
implementation which eventually result in inflexible code and difficult to change in maintenance. By using
Factory design pattern in Java we get rid of this problem.
Another problem we can face is class needs to contain objects of other classes or class hierarchies within it;
this can be very easily achieved by just using the new keyword and the class constructor. The problem with
this approach is that it is a very hard coded approach to create objects as this creates dependency between
the two classes.
So factory pattern solve this problem very easily by model an interface for creating an object which at
creation time can let its subclasses decide which class to instantiate, Factory Pattern promotes loose
coupling by eliminating the need to bind application-specific classes into the code. The factory methods are
typically implemented as virtual methods, so this pattern is also referred to as the Virtual Constructor.
These methods create the objects of the products or target classes.
Static Factory methods are common in frameworks where library code needs to create objects of
types which may be sub classed by applications using the framework.
Some or all concrete products can be created in multiple ways, or we want to leave open the option
that in the future there may be new ways to create the concrete product.
Factory method is used when Products don't need to know how they are created.
We can use factory pattern where we have to create an object of any one of sub-classes
depending on the data provided