桥接模式

1.桥接模式简介

作用:将抽象部分与它的实现部分相分离,使他们都可以独立地变化。

实现:抽象类依赖实现类。抽象出来一个基类,这个基类里面定义共有的一些行为,形成接口函数,聚合一个基类的指针(说白了就是在一个类中放一个指针来指向另一个对象),通过它来使用另一个类的实现函数。

使用场景:
(1)如果一个系统需要在构建的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。
(2)对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。
(3)一个类存在两个独立变化的维度,且这两个维度都需要进行扩展,对于两个独立变化的维度,使用桥接模式再适合不过了。


优点:
(1)抽象和实现的分离。 (2)优秀的扩展能力。 (3)实现细节对客户透明。

2.相关书籍:《大话设计模式》和《设计模式:可复用面向对象软件的基础》

3.抽象类虽然不能用来实例化对象,但是可以用来定义指针,如下OS是个抽象类。
OS *o_l1 = new Linux();

4.以后阅读Android源代码时一旦看到impl就可以认为使用了桥接模式

5.参考:
http://www.runoob.com/w3cnote/bridge-pattern2.html
https://blog.csdn.net/wuzhekai1985/article/details/6670473
https://www.cnblogs.com/jiese/p/3164940.html

6.Demo C++实现

#include <iostream>

using namespace std;

class Computer {
public:
    virtual void computer_name() = 0;
    virtual void installInfo() = 0;
};

class OS {
public:
    virtual void install_os() = 0;
};



class Mac : public Computer {
    OS *os_impl;
public:
    Mac() { this->os_impl = NULL; }
    Mac(OS *os) {
        os_impl = os;
    }
    void computer_name() {
        cout << "Computer Mac: ";
    }

    void installInfo() {
        computer_name();
        if (os_impl) {
            os_impl->install_os();
        }
    }
};

class Lenovo : public Computer {
    OS *os_impl;
public:
    Lenovo() { this->os_impl = NULL; }
    Lenovo(OS *os) {
        os_impl = os;
    }
    void computer_name() {
        cout << "Computer Lenovo: ";
    }

    void installInfo() {
        computer_name();
        if (os_impl) {
            os_impl->install_os();
        }
    }
};


class Linux : public OS {
public:
    void install_os() {
        cout << "install Linux" << endl;
    }
};

class Windows : public OS {
public:
    void install_os() {
        cout << "install Windows" << endl;
    }
};

int main() {
    OS *o_l1 = new Linux();
    OS *o_w1 = new Windows();

    Computer *c_m1 = new Mac(o_l1);
    Computer *c_l1 = new Lenovo(o_w1);

    c_m1->installInfo();
    c_l1->installInfo();

    return 0;
}

#if 0
int main() {
    Linux *o_l1 = new Linux();
    Windows *o_w1 = new Windows();

    Mac *c_m1 = new Mac(o_l1);
    Lenovo *c_l1 = new Lenovo(o_w1);

    c_m1->installInfo();
    c_l1->installInfo();

    return 0;
}
#endif

7.Demo Java实现

abstract class Shape {
    Color color;
 
    public void setColor(Color color) {
        this.color = color;
    }
    
    public abstract void draw();
}

class Rectangle extends Shape{
 
    public void draw() {
        color.bepaint("长方形");
    }
 
}

class Square extends Shape{
 
    public void draw() {
        color.bepaint("正方形");
    }
 
}


interface Color {
    public void bepaint(String shape);
}

class White implements Color{
 
    public void bepaint(String shape) {
        System.out.println("白色的" + shape);
    }
 
}

class Black implements Color{
 
    public void bepaint(String shape) {
        System.out.println("黑色的" + shape);
    }
}


public class Client {
    public static void main(String[] args) {

        Color white = new White();
        Color Black = new Black();

        Shape square = new Square();
        Shape rectange = new Rectangle();

        //白色的正方形
        square.setColor(white);
        square.draw();
 
        rectange.setColor(Black);
        rectange.draw();
    }
}
原文地址:https://www.cnblogs.com/hellokitty2/p/10661440.html