Minggu, 27 November 2016

Design Patterns - Strategy Pattern

dalam pola strategi, perilaku kelas atau algoritma dapat diubah berdasarkan berjalannya waktu. 
dalam pola strategi, objek yang kita buat mewakili berbagai strategi dan objek konteks yg perilakunya bervariasi sesuai objek strateginya. objek strategi perubahan algoritma mengeksekusi objek konteks.

IMPLEMENTASI


Step 1 : create an interface (strategy.java)
public interface Strategy {
   public int doOperation(int num1, int num2);
}
Step 2 : create concrete classes implementing  (operationadd.java)
public class OperationAdd implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 + num2;
   }
}
(operationsubstract.java)
public class OperationSubstract implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 - num2;
   }
}
(operationmultiply.java)
public class OperationMultiply implements Strategy{
   @Override
   public int doOperation(int num1, int num2) {
      return num1 * num2;
   }
}
Step 3 : Create Context Class (context.java)
public class Context {
   private Strategy strategy;

   public Context(Strategy strategy){
      this.strategy = strategy;
   }

   public int executeStrategy(int num1, int num2){
      return strategy.doOperation(num1, num2);
   }
}
Step 4 : Use the Context to see change in behaviour when it changes its Strategy(strategypatterndemo.java)
public class StrategyPatternDemo {
   public static void main(String[] args) {
      Context context = new Context(new OperationAdd());  
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationSubstract());  
      System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationMultiply());  
      System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
   }
}
step 5 : verify the output
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50

Revisi Builder Design pattern - Implementation [Animal Toy]

Builder Design pattern - Implementation [Animal Toy] 

Design Pattern merupakan sebuah istilah di dalam rekayasa perangkat lunak yang mengacu pada solusi umum yang bisa dipergunakan kembali atau berulang-ulang untuk menyelesaikan masalah-masalah yang umum terjadi dalam konteks tertentu atau khusus yang ditemui pada desain perangkat lunak. Sebuah pola desain yang sudah terbentuk bukan berarti desain tersebut dapat langsung digunakan untuk menulis program.

Builder Pattern adalah sebuah software design pattern.
Tujuannya adalah sebagai adalah sebagai langkah-langkah abstract kerangka (kontruksi) object ,jika suatu implementasi menggunakan langkah-langkah yang berbeda, maka akan membuat representasi object yang berbeda juga.
Builder Pattern sering digunakan untuk membangun produk sesuai dengan sebuah pola struktur Composite Pattern, dengan cara memisahkan pembangunan kompleks object dari representation, sehingga proses konstruksi yang sama dapat membuat representation yang berbeda. 

Class Diagram :
 

 Design Pattern Diagram :

AnimalToy.java 
 
 public class AnimalToy  
 {  
  private String animalToyName;  
  private String animalToyHead;  
  private String animalToyBody;  
  private String animalToyArms;  
  private String animalToyLegs;  
  private String animalToyTail;  
  public String getAnimalToyName()  
  {  
   return animalToyName;  
  }  
  public void setAnimalToyName( String animalToyName )  
  {  
   this.animalToyName = animalToyName;  
  }  
  public String getAnimalToyHead()  
  {  
   return animalToyHead;  
  }  
  public void setAnimalToyHead( String animalToyHead )  
  {  
   this.animalToyHead = animalToyHead;  
  }  
  public String getAnimalToyBody()  
  {  
   return animalToyBody;  
  }  
  public void setAnimalToyBody( String animalToyBody )  
  {  
   this.animalToyBody = animalToyBody;  
  }  
  public String getAnimalToyArms()  
  {  
   return animalToyArms;  
  }  
  public void setAnimalToyArms( String animalToyArms )  
  {  
   this.animalToyArms = animalToyArms;  
  }  
  public String getAnimalToyLegs()  
  {  
   return animalToyLegs;  
  }  
  public void setAnimalToyLegs( String animalToyLegs )  
  {  
   this.animalToyLegs = animalToyLegs;  
  }  
  public String getAnimalToyTail()  
  {  
   return animalToyTail;  
  }  
  public void setAnimalToyTail( String animalToyTail )  
  {  
   this.animalToyTail = animalToyTail;  
  }  
  @Override  
  public String toString()  
  {  
   return "AnimalToy [animalToyName=" + animalToyName + ", animalToyHead=" + animalToyHead  
           + ", animalToyBody=" + animalToyBody + ", animalToyArms=" + animalToyArms  
           + ", animalToyLegs=" + animalToyLegs + ", animalToyTail=" + animalToyTail + "]\n";  
  }  
 } 
 


AnimalToyBuilder.java

public abstract class AnimalToyBuilder  
 {  
  private AnimalToy animalToy;  
  public AnimalToy getAnimalToy()  
  {  
   return animalToy;  
  }  
  public void setAnimalToy( AnimalToy animalToy )  
  {  
   this.animalToy = animalToy;  
  }  
  public final AnimalToy buildAnimalToy()  
  {  
   AnimalToy animalToy = createAnimalToy();  
   setAnimalToy(animalToy);  
   buildAnimalHead();  
   buildAnimalBody();  
   buildAnimalLegs();  
   buildAnimalArms();  
   buildAnimalTail();  
   setAnimalToyName();  
   return animalToy;  
  }  
  public abstract void buildAnimalHead();  
  public abstract void buildAnimalBody();  
  public abstract void buildAnimalLegs();  
  public abstract void buildAnimalArms();  
  public abstract void buildAnimalTail();  
  public abstract void setAnimalToyName();  
  public abstract AnimalToy createAnimalToy();  
 }  

DogToyBuilder.java
 public class DogToyBuilder extends AnimalToyBuilder  
 {  
  public AnimalToy createAnimalToy()  
  {  
   return new AnimalToy();  
  }  
  public void buildAnimalHead()  
  {  
   getAnimalToy().setAnimalToyHead("Dog head");  
   System.out.println("Step 1 : Dog Head has been built");  
  }  
  public void buildAnimalBody()  
  {  
   getAnimalToy().setAnimalToyBody("Dog Body");  
   System.out.println("Step 2 : Dog Body has been built");  
  }  
  public void buildAnimalLegs()  
  {  
   getAnimalToy().setAnimalToyLegs("Dog 4 legs");  
   System.out.println("Step 3 : Dog Legs has been built");  
  }  
  public void buildAnimalArms()  
  {  
   // No implementation because dog doesn't have Arms  
   getAnimalToy().setAnimalToyLegs("No arms");  
   System.out.println("Step 4 : is skiped because dog doesn't have Arms");  
  }  
  public void setAnimalToyName()  
  {  
   getAnimalToy().setAnimalToyName("Dog");  
  }  
  public void buildAnimalTail()  
  {  
   getAnimalToy().setAnimalToyTail("Dog Tail");  
   System.out.println("Step 5 : Dog Tail has been built");  
  }  
 } 
 
 
 LionToyBuilder.java  
 public class LionToyBuilder extends AnimalToyBuilder  
 {  
  public AnimalToy createAnimalToy()  
  {  
   return new AnimalToy();  
  }  
  public void buildAnimalHead()  
  {  
   getAnimalToy().setAnimalToyHead("Lion head");  
   System.out.println("Step 1 : Lion Head has been built");  
  }  
  public void buildAnimalBody()  
  {  
   getAnimalToy().setAnimalToyBody("Lion Body");  
   System.out.println("Step 2 : Lion Body has been built");  
  }  
  public void buildAnimalLegs()  
  {  
   getAnimalToy().setAnimalToyLegs("Lion 4 legs");  
   System.out.println("Step 3 : Lion Legs has been built");  
  }  
  public void buildAnimalArms()  
  {  
   // No implementation because Lion doesn't have Arms  
   getAnimalToy().setAnimalToyLegs("No arms");  
   System.out.println("Step 4 : is skiped because Lion doesn't have Arms");  
  }  
  public void setAnimalToyName()  
  {  
   getAnimalToy().setAnimalToyName("Lion");  
  }  
  public void buildAnimalTail()  
  {  
   getAnimalToy().setAnimalToyTail("Lion Tail");  
   System.out.println("Step 5 : Lion Tail has been built");  
  }  
 }


MonkeyToyBuilder.java 
public class MonkeyToyBuilder extends AnimalToyBuilder  
 {  
  public AnimalToy createAnimalToy()  
  {  
   return new AnimalToy();  
  }  
  public void buildAnimalHead()  
  {  
   getAnimalToy().setAnimalToyHead("Monkey head");  
   System.out.println("Step 1 : Monkey Head has been built");  
  }  
  public void buildAnimalBody()  
  {  
   getAnimalToy().setAnimalToyBody("Monkey Body");  
   System.out.println("Step 2 : Monkey Body has been built");  
  }  
  public void buildAnimalLegs()  
  {  
   getAnimalToy().setAnimalToyLegs("Monkey 2 legs");  
   System.out.println("Step 3 : Monkey Legs has been built");  
  }  
  public void buildAnimalArms()  
  {  
   getAnimalToy().setAnimalToyArms("Monkey 2 Arms");  
   System.out.println("Step 4 : Monkey Arms has been built");  
  }  
  public void setAnimalToyName()  
  {  
   getAnimalToy().setAnimalToyName("Monkey");  
  }  
  public void buildAnimalTail()  
  {  
   getAnimalToy().setAnimalToyTail("Monkey Tail");  
   System.out.println("Step 5 : Monkey Tail has been built");  
  }  
 } 


ToyMaker.java 
 public class ToyMaker  
 {  
  public final static String LION_TOY  = "Lion";  
  public final static String DOG_TOY  = "Dog";  
  public final static String MONKEY_TOY = "Monkey";  
  public static AnimalToy makeToy( String toyType ) throws Exception  
  {  
   AnimalToyBuilder animalToyBuilder = null;  
   if( toyType.equalsIgnoreCase(LION_TOY) )  
   {  
    animalToyBuilder = new LionToyBuilder();  
   }  
   else if( toyType.equalsIgnoreCase(DOG_TOY) )  
   {  
    animalToyBuilder = new DogToyBuilder();  
   }  
   else if( toyType.equalsIgnoreCase(MONKEY_TOY) )  
   {  
    animalToyBuilder = new MonkeyToyBuilder();  
   }  
   else  
   {  
    System.out.println("Sorry we cann't make : " + toyType);  
    throw new Exception("Sorry we cann't make : " + toyType);  
   }  
   return animalToyBuilder.buildAnimalToy();  
  }  
 }  


BuilderClient.java

public class BuilderClient  
 {  
  public static void main( String[] args ) throws Exception  
  {  
   AnimalToy lionToy = ToyMaker.makeToy(ToyMaker.LION_TOY);  
   System.out.println(lionToy);  
   AnimalToy dogToy = ToyMaker.makeToy(ToyMaker.DOG_TOY);  
   System.out.println(dogToy);  
   AnimalToy monkeyToy = ToyMaker.makeToy(ToyMaker.MONKEY_TOY);  
   System.out.println(monkeyToy);  
  }  
 }