Monday, 13 January 2014

How to...

In this blog I am going to write about decorator pattern of OO designing.
examples :
InputStream:
concreate classes:FileInputStream,StringBufferInputStream,ByteArrayInputStream.
decorator:FilterInputStream:
Concreate decorators:
PushBackInputStream,BufferedInputStream,DataInputStream,
LineNumberInputStream
Design Principle :Classes should be closed for modifications but open for extensions. 

Decorator : "Attach additional responsibilities to an object dynamically.Decorators provide a flexible alternative to subclassing for extending functionality" - The book Head First Design Patterns.

 For this Pattern I made up my mind that I should think like an Indian Lady.  Let us consider a family bussiness in a street of Bombay,as it started as a small saree shop 20 years ago.  The bussiness got boomed since then ,so that now they are the owner of small enterprice and they have shops from this end  to the other end of that street. One corner  they have saree shops of various types of sarees like silk, nylon,polyester, cotten etc. On the other corner they have shops they sell matching falls, matching blouse, matching skirts,matching slippers, matching necklace and earings, matching bangles and at last matching Bindi(to put on the forehead); Once you buy a saree from them that is it, they will take you  to other shops to get all the matching items and you can then pay the bill at the end. (It is a family bussiness). 
Now we can  consider "WearableProducts" as a abstract class. All  sareePoducts such as "NylonSarees","SilkSarees " .... are thier concreate implementations.  There is an abstrct class "MatchingProducts"  class extands "WearableProducts"  is our decorator which takes a WearableProduct in its constructor.        
Here is our Component class(WearableProducts)
public abstract class WearableProducts{
string itemName = "";
public String getItemName (){
return itemName();
}
public abstrct double getPrice();
public abstract  void createBill();
public abstract Color getColor();
public abstract void setColor(Color color);
//more methods if you want to
}
This is our decorator class:
public abstract class MatchingProducts extends WearableProducts{
   public abstract String getItemName();
   public abstract boolean findMaching();
//more methods if you want to
}
concreate implemetation of WearableProducts:
public class NylonSarees extends WearableProducts{
Color color;
 public NylonSarees (){
     itemName = "nylon saree";
     
}
public double getPrice(){
return 400.00;
}
   public Color getColor(){
       return color;
}
public  void createBill(){
 System.out.println( " "+getItemName()+" "+getPrice());
}
public void setColor(Color color){
this.color = color;
}
}
My contreate decorator:-
public class  BlouseMatchers extends MatchingProducts{
WearableProduct saree;
String itemName;
Color color;
//define an array of colors;
public BlowseMatchers(WearableProducts saree){
this.saree = saree;
}
public boolean findMatching(){
//gothrough the array of colors
if(a[i] == saree.getColor()){
this.setColor((Color)a[i]);
}
return true;
else
return false;
}
public string getItemName(){
return itemName;
}
public double getPrice(){
  return(saree.getPrice()+100.00);
}
public void ceateBill(){
    saree.createBill();
     System.out.print(" "+getItemName()+" "100.00");
     System.out.Println(" "+getPrice());
}
   public Color getColor(){
       return color;
}
public void setColor(Color color){
this.color = color;
}

}
You can create more decorators like this. FallMatchers,BangleMatchers,NecklaceMatchers .....And you can create SilkSarees,CottonSarees are WearableProduct concreate classes.
while testing
WearableProduct saree = new NylonSarees();
MatchinProducts blouse = new BlouseMatchers(saree);
if (blouse.findMatching()) &&(shoppingisOver)

blouse.createBill();
else ...... you are allowed to go to further decorative stores wrap it with decorators.
MatchingProducts bangles = new BangleMatchers(blouse); (do this only if you get the matching blouse)
MatchingProducts slipper = new SlipperMatchers(bangles);
.......
bindi. createBill(); (this method calls others like recursive method, don't you think it is very powerful?)

I hope I did not confuse Indian Women.

Title:

How To explain "Decorator Pattern" for Indian Women.


No comments:

Post a Comment