Java Design Patterns are in essence is a true understanding of the problem and applied methodologies to achieve generic solution. I will be showcasing my understanding of the Java Design Pattern[s] along with collaborative UML diagram[s] and JUnit Test Case. |
1. Factory Pattern (FP) |
In the Factory pattern, objects are created based on a condition. Now, which object to return, is decided at run time based on the condition[s]. It is best described by an example. For instance, we have a Juice factory i.e. JuiceFactory . JuiceFactory manufactures juice[s] based on the type. Type could be a ‘fruit’ or a ‘vegetable’. |
The juice is prepared in Juicer , is measured and then packed. These are standard operations. They will be same for all type of juices. Hence, they will be captured using the JuiceInterface [code can be fetched from the Resources]. |
Figure 1 Blueprint of Juice
So, as per the Figure 1, we have Juice class which implements
JuiceInterface
. We also have a VegetableJuice
and a FruitJuice
classes which inherit the properties of Juice class to prepare the juice, measure it and then pack the juice.Figure 2 Blueprint of Juice Store Implementation
Now, as per the Figure 2, we will create the JuiceFactory , whose main task is to give the JuiceStore , the appropriate Juice based on the customer’s choice i.e. fruit or vegetable juice. |
Implementation: Let us frame our juice factory |
// Create JuiceFactory, which manufactures juice object |
public class JuiceFactory { public static Juice typeOfJuice (String type) { Juice juice = null; if (type == null) return juice; if (type.equalsIgnoreCase("vegetable")) juice = new VegetableJuice(); else if (type.equalsIgnoreCase("fruit")) juice = new FruitJuice(); return juice; } } } // end of JuiceFactory |
Now we will create the JuiceStore class, to see how the implementation of Factory Pattern is progressing. Below you may see [in the shaded], that all the logic of comparing the type of juice to choose is taken care by JuiceFactory . |
// Order Juice |
public class JuiceStore {
JuiceFactory juiceFactory;
public JuiceStore() {}
public JuiceStore (JuiceFactory juiceFactory) {
this.juiceFactory = juiceFactory;
}
public Juice orderJuice (String type) {
Juice juice;
if (type == null)
return null;
juice = JuiceFactory.typeOfJuice (type);
juice.prepare();
juice.measure();
juice.packed();
return juice;
}
} |
The rest of the implementation you may find in the code attached [Resources]. |
Try to test your implementation using JUnit. You may find plenty of reasons in Resources or when you search on the net. Below you find a small test of the above implementation. |
public void testJuiceStore () { JuiceStore myStore = new JuiceStore (); assertEquals(new JuiceStore().orderJuice("vegetable").prepare(), myStore.orderJuice("vegetable").prepare()); assertEquals(new JuiceStore().orderJuice("vegetable").measure(), myStore.orderJuice("vegetable").measure()); assertEquals(new JuiceStore().orderJuice("vegetable").packed(), myStore.orderJuice("vegetable").packed()); assertEquals(new JuiceStore().orderJuice("fruit").prepare(), myStore.orderJuice("fruit").prepare()); assertEquals(new JuiceStore().orderJuice("fruit").measure(), myStore.orderJuice("fruit").measure()); assertEquals(new JuiceStore().orderJuice("fruit").packed(), myStore.orderJuice("fruit").packed()); } |
Resources:
1. Source Code for the above implementation. Soon the files could be downloaded from Google Docs.
2. Why to Test programs
3. One stop shop for all design pattern can be found at Gang of Four book Design Patterns, Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides (Addison-Wesley, 1995)
4. Learn Design Pattern from AllAppLabs
No comments:
Post a Comment