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();
        }
    }
    
  • 相关阅读:
    自动填写数据与自动点击锭钮提交数据
    序列化(Serialization)据为JSONP远端请求
    使用iframe实现同域跨站提交数据
    使用JSONP跨域请求数据
    程序自动化需要一个Windows服务
    SPC-Light显示正常的日期与时间
    使用DDE传输数据至SQL Server
    C# console application executing macro function
    Transfer data to SQL Server from SPC-Light with Excel macros
    MVC应用程序请求密码的功能(二)
  • 原文地址:https://www.cnblogs.com/ghlz/p/13583777.html
Copyright © 2011-2022 走看看