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();
        }
    }
    
  • 相关阅读:
    Dotnet全平台下APM-Trace探索
    既生瑜何生亮?ASP.NET MVC VS ASP.NET Web API
    Dapper.NET——轻量ORM
    什么?字符串为空?
    Vue模板语法
    邂逅Vue.js
    Zookeeper是什么&怎么用
    虚拟机间实现免密登录
    十大排序算法最详细讲解
    JS将数字格式化成金融数字样式(千位分隔符,三位一个逗号间隔)
  • 原文地址:https://www.cnblogs.com/ghlz/p/13583777.html
Copyright © 2011-2022 走看看