• 设计模式(18)桥接模式


    一、介绍:

    1、定义:桥接(Bridge)模式属于结构型设计模式。通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。简而言之,实现系统可能有多角度分类,每一种分类都有可能变化,那么就把这种多角度分离出来让他们独立变化,减少他们之间的耦合。

    2、组成结构:UML结构图如下:

      (1) Abstraction(抽象类):它是用于定义抽象类的接口,通常是抽象类而不是接口,其中定义了Implementor(实现类接口)类型的对象并可以维护该对象,它与Implementor具有关联关系,它既可以包含抽象业务方法,也可以包含具体业务方法。

    1. public abstract class Abstraction{
    2. private Implementor impl;//定义实现类接口对象
    3. public void setImpl(Implementor impl){
    4. this.impl = impl;
    5. }
    6. public abstract void operation();//声明抽象业务方法
    7. }

     (2) RefinedAbstraction(扩充抽象类):它扩充由Abstraction定义的接口,通常情况下它不再是抽象类而是具体类,实现了在Abstraction中的抽象业务方法,并且可以调用在Implementor中定义的业务方法。

    1. public class RefinedAbstraction extends Abstraction{
    2. public void operation(){
    3. //业务代码
    4. impl.operationImpl();//调用实现类的方法
    5. //业务代码
    6. }
    7. }

     (3) Implementor(实现类接口):它是定义实现类的接口,不一定要与Abstraction的接口完全一致,事实上这两个接口可以完全不同。一般而言,Implementor接口仅提供基本操作,而Abstraction定义的接口可能会做更多更复杂的操作。Implementor对这些基本操作进行了声明,而具体实现交给其子类。通过关联关系,在Abstraction中不仅拥有自己的方法,还可以调用Implementor中定义的方法,使用关联关系替代继承关系。

    1. public interface Implementor{
    2. public void operationImpl();
    3. }

     (4) ConcreteImplementor(具体实现类):它具体实现了Implementor接口,在不同的ConcreteImplementor中提供基本操作的不同实现,在程序运行时ConcreteImplementor对象将替换父类对象,提供给抽象类具体的业务操作方法。

    1. public class ConcreteImplementor implements Implementor{
    2. public void operationImpl(){
    3. //具体业务方法的实现
    4. }
    5. }

    对于客户端而言,可以针对两个维度的抽象层编程,在程序运行时再动态确定两个维度的子类,动态组合对象,将两个独立变化的维度完全解耦,以便能够灵活地扩充任一维度而对另一维度不造成任何影响。 

    二、demo:

    1、颜色画板:

    1. //画笔
    2. interface Printer {
    3. public void print(int radius, int x, int y);
    4. }
    5. class ColorPrinter implements Printer {
    6. @Override
    7. public void print(int radius, int x, int y) {
    8. System.out.println("Color: " + radius +", x: " +x+", "+ y +"]");
    9. }
    10. }
    11. class BlackPrinter implements Printer {
    12. @Override
    13. public void print(int radius, int x, int y) {
    14. System.out.println("Black: " + radius +", x: " +x+", "+ y +"]");
    15. }
    16. }
    17. //形状
    18. abstract class Shape {
    19. protected Printer print;
    20. protected Shape(Printer p){
    21. this.print = p;
    22. }
    23. public abstract void draw();
    24. }
    25. class Circle extends Shape {
    26. private int x, y, radius;
    27. public Circle(int x, int y, int radius, Printer draw) {
    28. super(draw);
    29. this.x = x;
    30. this.y = y;
    31. this.radius = radius;
    32. }
    33. public void draw() {
    34. print.print(radius,x,y);
    35. }
    36. }
    37. public class Main {
    38. public static void main(String[] args) {
    39. Shape redCircle = new Circle(100,100, 10, new ColorPrinter());
    40. Shape blackCircle = new Circle(100,100, 10, new BlackPrinter());
    41. redCircle.draw();
    42. blackCircle.draw();
    43. }
    44. }

  • 相关阅读:
    Postman常见问题及解决方法
    JSON文件读写
    R语言计算时间序列数据的移动平均值(滚动平均值、例如5日均线、10日均线等):使用zoo包中的rollmean函数计算k个周期移动平均值
    13 媒体存储的封装1
    【操作系统&C语言】作业调度算法 先来先服务&短作业优先
    frp使用oidc认证和搭建
    TCP/IP_第八章_静态路由_实验案例一
    Sim3相似变换
    云原生定义整理
    2019 Java面试题
  • 原文地址:https://blog.csdn.net/w_t_y_y/article/details/134049328