重要的Java模式——策略模式

策略模式允許在允許中替換算法。要實現該解決方案,需要將每個算法表示爲Strategy(策略)類。然後應用程序委託當前的Strategy類來執行特定於策略的算法。

 

1、Strategy

         下面示例使用Role(充當Strategy)接口來聲明策略行爲和倆個具體來——Buyer和Seller來實現不同的行爲:

Role接口:

package strategy;
public interface Role {
	public boolean siSatisfied(Product product, double price);
}

Seller類:希望銷售的產品都設置了20%的利潤率

package strategy;

public class Seller implements Role{
	public boolean siSatisfied(Product product, double price) {
		//具有20%的利潤
		if(price - product.getCost() > product.getCost() * .2){
			return true;
		}else{
			return false;
		}
	}
}

對於Buyer來說,他要買下此種商品,必須有一個限制額。

package strategy;

public class Buyer implements Role{
	private double limit;
	public Buyer(double limit){
		this.limit = limit;
	}
	public boolean siSatisfied(Product product, double price) {
		if(price < limit && price < product.getCost() * 2){
			return true;
		}
		return false;
	}
}

2、context

         Context這裏是指:管理Role的Person類。Person類與Role接口又一個關聯,另外值得注意的是,Role又一個setter和getter方法,它允許一個人的角色在程序執行時發生變化。以後,其它的Role實現對象(如broker(中間)等)都可以被添加。因爲它們對特定的子類沒有依賴。下面時Person類:

package strategy;

public class Person {
	private String name;
	private Role role;
	public Person(String name){
		this.name = name;
	}
	public Role getRole(){
		return role;
	}
	public void setRole(Role role){
		this.role = role;
	}
	public boolean statisfied(Product product, double price){
		//statisfied方法將特定於角色的行爲委託給其Role接口。這裏多態會選擇正確的底層對象。
		return role.isSatisfied(product, price);
	}
}

其它類:本示例的Product類及執行的Main類:

1Product

package strategy;

public class Product {
	private String name;
	private String description;
	private double cost;
	public Product(String name, String description, double cost){
		this.name = name;
		this.description = description;
		this.cost = cost;
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public double getCost() {
		return cost;
	}

	public void setCost(double cost) {
		this.cost = cost;
	}

}

2、Main類

package strategy;

public class Main {
	public static void main(String[] args){
		Product house = new Product("house", "Three room and a parlor", 200000);
		Product laptop = new Product("laptop", "The latest style of lenovo laptop computer", 10000);
		Person liLei = new Person("李雷");
		Person hanMeimei = new Person("韓梅梅");
		liLei.setRole(new Buyer(500000));
		hanMeimei.setRole(new Seller());
		
		if(!hanMeimei.statisfied(house, 200000)){
			System.out.println("韓梅梅房子不會賣200000。");
		}
		if(!liLei.statisfied(house, 6000000)){
			System.out.println("李雷,覺得如果要花6000000買下那房子,太貴了!");
		}
		if(liLei.statisfied(house, 390000) && hanMeimei.statisfied(house, 390000)){
			System.out.println("他們最終已390000的價格成交了!");
			//允許時修改一個對象的行爲而不影響它的實現
			hanMeimei.setRole(new Buyer(390000));
			if(hanMeimei.statisfied(laptop, 11000)){
				System.out.println("得到390000,韓梅梅現在花了11000去買進了一臺最新款的聯繫筆記本!");
			}
		}
	}
}

         通過實現策略模式,可以在運行時修改一個對象的行爲而不影響它的實現。這是軟件設計中非常強大的工具。

發佈了62 篇原創文章 · 獲贊 29 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章