設計模式-橋樑模式

栗子

定義抽象公司函數

public abstract class Corp{
    // 公司生產內容
    protected abstract void produce();
    // 銷售
    protected abstract void sell();
    // 賺錢
    public void makeMoney(){
        // 生產
        this.produce();
        // 銷售
        this.sell();
    }
}

上方是模板方法
下面是房地產公司this

public class HouseCorp extends Corp{
    // 蓋房子
    protected void produce(){
        // 蓋房子
    }
    // 賣房子
    protected void sell(){
        // 賣房子
    }
    // 賺錢
    public void makeMoney(){
        super.makeMoney();
        // 賺錢
    }
}

服裝公司spa

public class ClothesCorp extends Corp{
    // 生產衣服
    protected void produce(){
        // 生產衣服
    }
    protected void sell(){
        // 出售衣服
    }
    public void makeMoney(){
        super.makeMoney();
        // 賺錢
    }
}

最後編寫場景code

public class Client{
    public static void main(String[] args){
        HouseCorp houseCorp = new HouseCorp();
        houseCorp.makeMoney();
        ClothesCorp clothesCorp = new ClothesCorp();
        clothesCorp.makeMoney();
    }
}

更改

企業改頭換面,生產另外的產品
山寨公司對象

public class IPodCorp extends Corp{
    // 生產
    protected void produce(){
        // 生產
    }
    // 暢銷
    protected void sell(){
        // 暢銷
    }
    // 賺錢
    public void makeMoney(){
        super.makeMoney();
        // 賺錢
    }
}

賺錢繼承

public class Client{
    public static void main(String[] args){
        // 房地產
        HouseCorp houseCorp = new HouseCorp();
        // 掙錢
        houseCorp.makeMoney();
        // 山寨公司
        IPodCorp ipodCorp = new IPodCorp();
        ipodCorp.makeMoney();
    }
}

繼續更改

公司和產品分離,讓其之間創建關係圖片

// 抽象產品類
public abstract class Product{
    // 生產
    public abstract void beProducted();
    // 銷售
    public abstract void beSelled();
}
// 房子
public class House extends Product{
    // 豆腐渣房子
    public void beProducted(){
        // 生產
    }
    // 銷售
    public void beSelled(){
        // 銷售
    }
}

繼續ip

public class IPod extends Product{
    public void beProducted(){
        // 生產
    }
    public void beSelled(){
        // 銷售
    }
}

下面是抽象公司類產品

public abstract class Corp{
    // 定義抽象產品對象
    private Product product;
    // 構造函數
    public Corp(Product product){
        this.product = product;
    }
    // 公司賺錢
    public void makeMoney(){
        // 生產
        this.product.beProducted();
        // 銷售
        this.product.beSelled();
    }
}

定義房地產公司it

public class HouseCorp extends Corp{
    // 定義House產品
    public HouseCorp(House house){
        super(house);
    }
    // 賺錢
    public void makeMoney(){
        super.makeMoney();
        // 賺錢
    }
}

山寨公司

public class ShanZhaiCorp extends Corp{
    // 產品
    public ShanZhaiCorp(Product product){
        super(product);
    }
    // 賺錢
    public void makeMoney(){
        super.makeMoney();
        // 賺錢
    }
}

最後書寫場景

public class Client{
    public static void main(String[] args){
        House house = new House();
        // 房地產公司
        HouseCorp houseCorp = new HouseCorp(house);
        // 賺錢
        houseCorp.makeMoney();
        // 生產產品
        ShanZhaiCorp shanZhaiCorp = new ShanZhaiCorp(new IPod());
        ShanZhaiCorp.makeMoney();
    }
}

此時在目前狀態下,若要生產服裝,只須要繼承Product類,定義一個服裝類便可

public class Clothes extends Product{
    public void beProducted(){
        // 生產服裝
    }
    public void beSelled(){
        // 賣衣服
    }
}

最後書寫場景類

public class Client{
    public static void main(String[] args){
        House house = new House();
        // 房地產公司
        HouseCorp houseCorp = new HouseCorp(house);
        // 掙錢
        houseCorp.makeMoney();
        // 山寨公司生產
        ShanZhaiCorp.shanZhaiCorp = new ShanZhaiCorp(new Clothes());
        ShanZhai.makeMoney();
    }
}

總結

橋樑模式,抽象和實現解耦,須要的時候,將實現注入抽象。

圖片描述

相關文章
相關標籤/搜索