设计模式之六桥接模式

案列介绍

 

 

uml类图

 

问题所在

 

 桥接模式基本介绍

 

 uml类图

 

 

 

 代码演示

package com.hy.bridge;


public interface Brand {
    void open();
    void close();
    void call();
}

package com.hy.bridge;

public abstract class Phone {

    private Brand brand;

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

    protected void open() {
        this.brand.open();
    }

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

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

}

package com.hy.bridge;


//折叠式手机类,继承 抽象类 Phone
public 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(" 折叠样式手机 ");
    }
}

package com.hy.bridge;

public class UpRightPhone extends Phone {
    
        //构造器
        public UpRightPhone(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(" 直立样式手机 ");
        }
}

package com.hy.bridge;

public class XiaoMi implements Brand {

    @Override
    public void open() {
        // TODO Auto-generated method stub
        System.out.println(" 小米手机开机 ");
    }

    @Override
    public void close() {
        // TODO Auto-generated method stub
        System.out.println(" 小米手机关机 ");
    }

    @Override
    public void call() {
        // TODO Auto-generated method stub
        System.out.println(" 小米手机打电话 ");
    }

}

package com.hy.bridge;

public class Vivo implements Brand {

    @Override
    public void open() {
        // TODO Auto-generated method stub
        System.out.println(" Vivo手机开机 ");
    }

    @Override
    public void close() {
        // TODO Auto-generated method stub
        System.out.println(" Vivo手机关机 ");
    }

    @Override
    public void call() {
        // TODO Auto-generated method stub
        System.out.println(" Vivo手机打电话 ");
    }

}

package com.hy.bridge;

public class Client {

    public static void main(String[] args) {
        

        
        Phone phone1 = new FoldedPhone(new XiaoMi());
        
        phone1.open();
        phone1.call();
        phone1.close();
        
        System.out.println("=======================");
        
        Phone phone2 = new FoldedPhone(new Vivo());
        
        phone2.open();
        phone2.call();
        phone2.close();
        
        System.out.println("==============");
        
        UpRightPhone phone3 = new UpRightPhone(new XiaoMi());
        
        phone3.open();
        phone3.call();
        phone3.close();
        
        System.out.println("==============");
        
        UpRightPhone phone4 = new UpRightPhone(new Vivo());
        
        phone4.open();
        phone4.call();
        phone4.close();
    }

}

 

posted @ 2020-11-18 01:21  yongzhewuwei  阅读(83)  评论(0编辑  收藏  举报