zoukankan      html  css  js  c++  java
  • 设计模式8——桥接模式

    设计模式8——桥接模式




    代码实现:

    package com.ghl.bridge;
    
    /**
     * @ProjectName DesignPattern
     * @ClassName brand
     * @Date 2020/8/27 22:10
     * @Author gaohengli
     * @Version 1.0
     */
    //桥接模式
        //品牌
    public interface Brand {
    
        void info();
    }
    
    package com.ghl.bridge;
    
    /**
     * @ProjectName DesignPattern
     * @ClassName Lenovo
     * @Date 2020/8/27 22:11
     * @Author gaohengli
     * @Version 1.0
     */
    //苹果品牌
    public class Apple implements Brand {
        @Override
        public void info() {
            System.out.println("苹果");
        }
    }
    
    package com.ghl.bridge;
    
    /**
     * @ProjectName DesignPattern
     * @ClassName Lenovo
     * @Date 2020/8/27 22:11
     * @Author gaohengli
     * @Version 1.0
     */
    //联想品牌
    public class Lenovo implements Brand {
        @Override
        public void info() {
            System.out.println("联想");
        }
    }
    
    package com.ghl.bridge;
    
    /**
     * @ProjectName DesignPattern
     * @ClassName Computer
     * @Date 2020/8/27 22:16
     * @Author gaohengli
     * @Version 1.0
     */
    //抽象的电脑类型类
    public abstract class Computer {
    
        //组合:品牌
        protected Brand brand;
    
        public Computer(Brand brand) {
            this.brand = brand;
        }
    
        public void info(){
            brand.info();//自带品牌
        }
    }
    
    //台式机
    class Desktop extends Computer{
    
        public Desktop(Brand brand) {
            super(brand);
        }
    
        @Override
        public void info() {
            super.info();
            System.out.println("台式机");
        }
    }
    
    //笔记本
    class Laptop extends Computer{
    
        public Laptop(Brand brand) {
            super(brand);
        }
    
        @Override
        public void info() {
            super.info();
            System.out.println("笔记本");
        }
    }
    
    package com.ghl.bridge;
    
    /**
     * @ProjectName DesignPattern
     * @ClassName Test
     * @Date 2020/8/27 22:29
     * @Author gaohengli
     * @Version 1.0
     */
    //测试
    public class Test {
    
        public static void main(String[] args) {
            //苹果笔记本
            Computer laptop = new Laptop(new Apple());
            laptop.info();
    
            //联想台式机
            Computer desktop = new Desktop(new Lenovo());
            desktop.info();
        }
    }
    
  • 相关阅读:
    高级软件工程第四次作业(C++)
    248&258--高级软件工程第三次作业
    高级软件工程第二次作业--四则运算生成器
    2017282110258--高级软件工程--齐爽爽第一次作业
    一元多项式求导_9
    成绩排名_8
    写这个数 _7
    我要通过_6
    月饼_5
    Hadoop的读写类调用关系_图示
  • 原文地址:https://www.cnblogs.com/ghlz/p/13583777.html
Copyright © 2011-2022 走看看