java设计模式--[结构模式]--门面模式[decorator pattern]
一. 門面模式
門面模式,又叫外觀模式,一個大的系統由若干個子系統構成,每個子系統中包含多個類,這些類協同合作為用戶提供所需要的功能,若直接和子類進行交互,則就形成了依賴關係,門面模式就是用來解決此問題的一個成熟模式.
二. 門面模式的UML類圖如下所示:
三. 現在用一個郵局的物流系統來模擬這個門面模式的實現,完整示例代碼如下:
1. 物流接口類
package structuralPattern.facade.facade1;
/**
* @ClassName: ILogistics
* @Description:物流接口
* @author Liyongbin
* @date 2011-2-9 上午11:20:48
* @version V1.0
*/
public interface ILogistics {
//發貨
public ILogistics sub_send();
//收貨
//public ILogistics sub_receive();
}
2. 郵局類
package structuralPattern.facade.facade1;
/**
* @ClassName: Postoffice
* @Description:郵局類
* @author Liyongbin
* @date 2011-2-9 上午11:21:31
* @version V1.0
*/
public class Postoffice implements ILogistics{
//
private Sub_measurement measurement;
private Sub_package packaging;
private Sub_weighIn weighIn;
private Sub_countPrice countprice;
private Sub_mailing miling;
//無參構造器
public Postoffice() {
super();
}
//構造器
public Postoffice(Sub_measurement measurement, Sub_weighIn weighIn, Sub_package packaging,
Sub_countPrice countprice, Sub_mailing amiling) {
super();
this.measurement = measurement;
this.weighIn = weighIn;
this.packaging = packaging;
this.countprice = countprice;
this.miling = amiling;
}
//運送物品
@Override
public ILogistics sub_send() {
packaging.packaging();
measurement.measurement();
weighIn.weighIn();
countprice.countPrice();
miling.mailing();
return null;
}
}
3. 物品打包類
package structuralPattern.facade.facade1;
/**
* @ClassName: Sub_package
* @Description:物品打包
* @author Liyongbin
* @date 2011-2-9 上午11:34:16
* @version V1.0
*/
public class Sub_package {
public void packaging(){
System.out.println("流程開始");
System.out.println("包裝物品...");
}
}
4. 量物品體積
package structuralPattern.facade.facade1;
/**
* @ClassName: Sub_measurement
* @Description:量物品體積
* @author Liyongbin
* @date 2011-2-9 上午11:32:56
* @version V1.0
*/
public class Sub_measurement {
public void measurement(){
System.out.println("測量物品體積...");
}
}
5. 物品稱重
package structuralPattern.facade.facade1;
/**
* @ClassName: Sub_weighIn
* @Description:物品稱重
* @author Liyongbin
* @date 2011-2-9 上午11:31:29
* @version V1.0
*/
public class Sub_weighIn {
public void weighIn(){
System.out.println("稱物品重量...");
}
}
6. 投遞物品
package structuralPattern.facade.facade1;
/**
* @ClassName: Sub_mailing
* @Description:投遞物品
* @author Liyongbin
* @date 2011-2-9 上午11:38:43
* @version V1.0
*/
public class Sub_mailing {
public void mailing(){
System.out.println("投遞物品...");
System.out.println("流程結束");
}
}
7. 測試類
package structuralPattern.facade.facade1;
/**
* @ClassName: FacadeTest
* @Description: 門面測試類
* @author Liyongbin
* @date 2011-2-9 上午11:55:25
* @version V1.0
*/
public class FacadeTest {
public static void main(String[] args) {
//實例化組件
Sub_measurement measurement = new Sub_measurement();
Sub_package packaging=new Sub_package();
Sub_weighIn weighIn= new Sub_weighIn();
Sub_countPrice countprice =new Sub_countPrice();
Sub_mailing miling =new Sub_mailing();
//實例化郵局
ILogistics lt= new Postoffice(measurement,weighIn,packaging,countprice,miling);
//郵寄物品
lt.sub_send();
}
}
8. 運行結果如下: