zoukankan      html  css  js  c++  java
  • 【设计模式】桥接模式

    使用频率:★★★☆☆

    一、什么是桥接模式

    将对象的行为抽象为接口,作为抽象类的成员属性在抽象层进行组合(个人理解,仅供参考);

    二、补充说明

    改变对象与其行为的强耦合关系,使之与行为解耦;

    使对象的行为以及对象本身都能独立变化;

    三、角色

    抽象类

    具体实现类

    行为接口

    具体行为实现类

    客户端

    四、例子,JAVA实现

    例子说明:

    对象:人(有中国人、印度人。。。。)

    行为:吃(用筷子吃、用手吃、用叉子吃。。。)

    假设一个场景:有一个中国人和欧洲人,中国人用筷子吃,欧洲人用叉子吃,如果我们要对这两个人进行封装,一般用以下方式:

    抽象出一个抽象人类,新建两个具体类继承抽象人类,其中一个实现筷子吃行为,另一个实现用叉子吃行为;

    缺点:具体的人对象与吃的行为变成了强耦合关系,修改和扩展都不方便;说不定哪天中国人去吃西餐,用叉子吃,这时候就需要改变具体中国人实现类代码;

    假设使用桥接模式,我们可以这样设计:

    对吃行为抽象成一个接口,并定义好各自的实现类(用筷子吃实现类,用手吃实现类,用叉子吃实现类);

    还是抽象出一个抽象人类,不过要在这个抽象人类中组合进一个吃行为接口;

    具体的中国人或欧洲人继承抽象人类,但其不需要实现具体吃行为,只需要对其成员属性(吃行为接口)赋一个具体实现对象即可;

    代码实现如下:

    吃行为接口:

    package com.pichen.dp.structuralpattern.bridge;
    
    /**
     * 吃行为接口
     * @author    pi chen
     * @version   V1.0.0, 2016年2月19日
     * @see       
     * @since     V1.0.0
     */
    public interface Eatable {
    
        public void eat();
        
    }

    吃行为实现类(用筷子吃实现类,用手吃实现类,用叉子吃实现类):

    package com.pichen.dp.structuralpattern.bridge;
    
    public class EatWithChopsticks implements Eatable{
    
        @Override
        public void eat() {
            System.out.println("Eat with Chopsticks");
        }
    
    }
    View Code
    package com.pichen.dp.structuralpattern.bridge;
    
    
    public class EatWithHand implements Eatable{
    
        @Override
        public void eat() {
            System.out.println("Eat with Hand");
        }
    
    }
    View Code
    package com.pichen.dp.structuralpattern.bridge;
    
    
    
    public class EatWithFork implements Eatable{
    
        @Override
        public void eat() {
            System.out.println("Eat with Fork");
        }
    
        
    }
    View Code

    抽象人类:

    package com.pichen.dp.structuralpattern.bridge;
    
    
    public abstract class Person {
    
        //吃行为抽象为接口,使之与具体对象解耦
        protected Eatable eatable;
    
        /**
         * @return the eatable
         */
        public Eatable getEatable() {
            return eatable;
        }
    
        /**
         * @param eatable the eatable to set
         */
        public void setEatable(Eatable eatable) {
            this.eatable = eatable;
        }
        
    
    }

    具体的不同种类的人(中国人、欧美人、印度人):

    package com.pichen.dp.structuralpattern.bridge;
    
    public class Chinese extends Person{
    
    
        public Chinese(Eatable eatable) {
            this.eatable = eatable;
        }
    
    
    }
    View Code
    package com.pichen.dp.structuralpattern.bridge;
    
    public class European extends Person{
        public European(Eatable eatable) {
            this.eatable = eatable;
        }
    
    }
    View Code
    package com.pichen.dp.structuralpattern.bridge;
    
    public class Indian extends Person{
    
        public Indian(Eatable eatable) {
            this.eatable = eatable;
        }
        
    
    }
    View Code

    客户端:

    package com.pichen.dp.structuralpattern.bridge;
    
    public class Main {
    
        public static void main(String[] args) {
            //定义三种具体行为
            Eatable eatWithChopsticks = new EatWithChopsticks();
            Eatable eatWithFork = new EatWithFork();
            Eatable eatWithHand = new EatWithHand();
            
            //定义三中具体人
            Person chinese = new Chinese(eatWithChopsticks);
            Person indian = new Indian(eatWithHand);
            Person european = new European(eatWithFork);
            
            //中国人用筷子吃,印度人用手,欧洲人用叉子
            chinese.getEatable().eat();
            indian.getEatable().eat();
            european.getEatable().eat();
            
            //当然,中国人有时候也用叉子吃西餐,或用手吃
            chinese.setEatable(eatWithFork);
            chinese.getEatable().eat();
            chinese.setEatable(eatWithHand);
            chinese.getEatable().eat();
            
            
        }
    }

    结果打印:

    Eat with Chopsticks
    Eat with Hand
    Eat with Fork
    Eat with Fork
    Eat with Hand
  • 相关阅读:
    BIND简易教程(2):BIND视图配置
    BIND简易教程(1):安装及基本配置
    大学本科计算机专业应该学点什么?
    Bukkit之yaml动态读取
    将指定世界中的指定位置的Block转化为箱子
    iframe中父页面与子页面的传值方法
    ajax提交数据
    a标签响应onclick事件,并且不执行href动作
    jsp重新打开一个新的页面
    java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException
  • 原文地址:https://www.cnblogs.com/chenpi/p/5201399.html
Copyright © 2011-2022 走看看