设计模式-建造者模式,适配器模式,桥接模式

Posted yuec1998

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-建造者模式,适配器模式,桥接模式相关的知识,希望对你有一定的参考价值。

建造者模式

//建造者模式
public class BuilderTest {
    public static void main(String[] args) {
        //普通盖房
        CommonHouse commonHouse = new CommonHouse();
        //准备创建房子指挥者
        HouseDirector houseDirector=new HouseDirector(commonHouse);
        //完成盖房子,返回产品
        House getCommonHouse = houseDirector.constructHouse();
        //高楼
        HighBuilding highBuilding = new HighBuilding();
        houseDirector.setHouseBuilder(highBuilding);
        House getHighBuildingHouse = highBuilding.buildHouse();
    }
}

//指挥者
class HouseDirector{
    HouseBuilder houseBuilder = null;
    //构造器传入houseBuilder
    public HouseDirector(HouseBuilder houseBuilder){
        this.houseBuilder=houseBuilder;
    }
    //通过setter传入houseBuilder
    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }
    //处理建造房子的流程,交给指挥者
    public House constructHouse(){
        houseBuilder.buildBasic();
        houseBuilder.buildWall();
        houseBuilder.buildRoofed();
        return houseBuilder.buildHouse();
    }
}

@Setter
class House{
    private String basic;
    private String wall;
    private String roofed;
}

//抽象的建造者
abstract class HouseBuilder{
    protected House house = new House();
    //将建造的流程写好,抽象的方法
    public abstract void buildBasic();
    public abstract void buildWall();
    public abstract void buildRoofed();
    //建造房子好后,将房子交付
    public House buildHouse(){
        return house;
    }
}

//普通房子
class CommonHouse extends HouseBuilder{

    @Override
    public void buildBasic() {
        //this.house.setBasic("普通房子打地基"); 后面都是如此
        System.out.println("普通房子打地基");
    }

    @Override
    public void buildWall() {
        System.out.println("普通房子砌墙");
    }

    @Override
    public void buildRoofed() {
        System.out.println("普通房子封顶");
    }
}
//高楼
class HighBuilding extends HouseBuilder{

    @Override
    public void buildBasic() {
        System.out.println("高楼打地基");
    }

    @Override
    public void buildWall() {
        System.out.println("高楼砌墙");
    }

    @Override
    public void buildRoofed() {
        System.out.println("高楼封顶");
    }
}

适配者模式

适配器的实现方式有3种,对象适配器,类适配器,接口适配器
以下,用一个对象适配器的例子来体验一下适配器的解耦

import lombok.Getter;

public class AdapterTest {
    public static void main(String[] args) {
        activity(PosTypeEnum.POS1.getValue());
    }

    static public void activity(Integer posType){
        PrintManager printManager = new PrintManager(posType);
        printManager.printMoney();
    }

}

//打印接口
interface IPrint{
    //打印订单
    void printOrder();
    //打印金额
    void printMoney();
    //打印日期
    void printDate();
}

//第一种pos机
class PosPrinter1 implements IPrint{

    @Override
    public void printOrder() {
        System.out.println("第一种打印机:打印订单");
    }

    @Override
    public void printMoney() {
        System.out.println("第一种打印机:打印金额");
    }

    @Override
    public void printDate() {
        System.out.println("第一种打印机:打印日期");
    }
}

//第二种pos机
class PosPrinter2 implements IPrint{

    @Override
    public void printOrder() {
        System.out.println("第二种打印机:打印订单");
    }

    @Override
    public void printMoney() {
        System.out.println("第二种打印机:打印金额");
    }

    @Override
    public void printDate() {
        System.out.println("第二种打印机:打印日期");
    }
}

//第三种pos机
class PosPrinter3 implements IPrint{

    @Override
    public void printOrder() {
        System.out.println("第三种打印机:打印订单");
    }

    @Override
    public void printMoney() {
        System.out.println("第三种打印机:打印金额");
    }

    @Override
    public void printDate() {
        System.out.println("第三种打印机:打印日期");
    }
}

//打印管理类,相当于Adapter
class PrintManager implements IPrint{

    private IPrint printer;
    public PrintManager(Integer posType){
        if (PosTypeEnum.POS1.getValue().equals(posType)){
            printer= new PosPrinter1();
        }else if (PosTypeEnum.POS2.getValue().equals(posType)){
            printer= new PosPrinter2(); 
        }if (PosTypeEnum.POS3.getValue().equals(posType)){
            printer = new PosPrinter3();
        }
    }

    @Override
    public void printOrder() {
        printer.printOrder();
    }

    @Override
    public void printMoney() {
        printer.printMoney();
    }

    @Override
    public void printDate() {
        printer.printDate();
    }
}

@Getter
enum PosTypeEnum{
    POS1(1),
    POS2(2),
    POS3(3),
    ;
    private Integer value;
    PosTypeEnum(Integer value){
        this.value = value;
    }
}

桥接模式

//桥接模式
public class BridgeTest {
    public static void main(String[] args) {
        Phone phone=new FoldedPhone(new Xiaomi());
        phone.open();
        phone.call();
        phone.close();
    }
}

//品牌
interface Brand{
    void open();
    void close();
    void call();
}

//小米
class Xiaomi implements Brand{

    @Override
    public void open() {
        System.out.println("小米手机开机了");
    }

    @Override
    public void close() {
        System.out.println("小米手机关机了");

    }

    @Override
    public void call() {
        System.out.println("小米手机打电话");

    }
}


//华为
class Huawei implements Brand{

    @Override
    public void open() {
        System.out.println("华为手机开机了");
    }

    @Override
    public void close() {
        System.out.println("华为手机关机了");

    }

    @Override
    public void call() {
        System.out.println("华为手机打电话");

    }
}

//手机抽象类
abstract class Phone{
    private Brand brand;

    public Phone(Brand brand) {
        this.brand = brand;
    }

    public void open() {
        brand.open();
    }

    public void close() {
        brand.close();
    }

    public void call() {
        brand.call();
    }
}

//折叠手机
class FoldedPhone extends Phone{

    public FoldedPhone(Brand brand) {
        super(brand);
    }

    public void open() {
        super.open();
        System.out.println("折叠式手机");
    }

    public void close() {
        super.close();
        System.out.println("折叠式手机");
    }

    public void call() {
        super.call();
        System.out.println("折叠式手机");
    }
}

以上是关于设计模式-建造者模式,适配器模式,桥接模式的主要内容,如果未能解决你的问题,请参考以下文章

编程经常使用设计模式具体解释--(上篇)(工厂单例建造者原型)

设计模式基础

Java设计模式之四 ----- 适配器模式和桥接模式

设计模式

设计模式—— 建造者模式/适配器模式

设计模式—— 建造者模式/适配器模式