2015年9月14日 星期一

策略模式

1、定義共同介面 2、編寫策略類別 implements這個介面 , 每個策略都有獨特的實現 3、編寫角色類別,有一個對共同介面的引用,使set/get或者建構子賦予值
  //Strategy->Interface , DiscountStrategy implements Strategy
  Strategy strategy = new DiscountStrategy();
  Price price = new Price(strategy);  
  double price = price.calculate(200);  ->會使用DiscountStrategy的策略方法打折

沒有留言:

張貼留言