标签:
1 模板方法(TEMPLATE METHOD)模式:
1 public abstract class Car { 2 3 public abstract void makeHead(); 4 5 public abstract void makeBody(); 6 7 8 public void makeCar(){ 9 this.makeHead(); 10 this.makeBody(); 11 } 12 13 14 15 }
具体类:
1 public class Bus extends Car { 2 3 @Override 4 public void makeHead() { 5 6 System.out.println("产生bus车头"); 7 8 } 9 10 @Override 11 public void makeBody() { 12 13 System.out.println("产生bus车身"); 14 15 } 16 17 }
测试:
1 public class Test { 2 3 public static void main(String[] args) { 4 5 Car car = new Jeep(); 6 car.makeCar(); 7 8 9 } 10 11 }
1 package ooad.design.Strategy; 2 /** 3 * 报价接口 4 * @author hellokitty燕 5 * 6 */ 7 public interface Quoteprice { 8 public double quoteprice(double price); 9 }
1 package ooad.design.Strategy; 2 3 public class Content { 4 private Quoteprice quoteprice; 5 6 public Content(Quoteprice quoteprice) { 7 super(); 8 this.quoteprice = quoteprice; 9 } 10 /** 11 * 折扣 12 * @param price 13 * @return 14 */ 15 public double discounting(double price){ 16 return quoteprice.quoteprice(price); 17 18 } 19 20 }
1 package ooad.design.Strategy; 2 /** 3 * 新用户 4 * @author hellokitty燕 5 * 6 */ 7 public class NewUser implements Quoteprice{ 8 9 @Override 10 public double quoteprice(double price) { 11 if(price>10000){ 12 return price*0.95; 13 } 14 15 return price; 16 } 17 18 }
测试类:
1 package ooad.design.Strategy; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 Quoteprice q=new NewUser(); 7 Content c=new Content(q); 8 double ne= q.quoteprice(20000); 9 System.out.println(ne); 10 } 11 12 }
行为型设计模式之模板方法(TEMPLATE METHOD)模式 ,策略(Strategy )模式
标签:
原文地址:http://www.cnblogs.com/hellokitty1/p/4656655.html