关于Java 设计模式 桥接模式(桥梁模式) Bridge

关于Java 设计模式 桥接模式(桥梁模式) Bridge


1、IDrawAPI.java

package com.ssm.cts.pattern.bridge;

public interface IDrawAPI {
  public void drawCircle(String name);
}

2、RedCircle.java

package com.ssm.cts.pattern.bridge;

public class RedCircle implements IDrawAPI {

  @Override
  public void drawCircle(String name) {
    System.out.println("红圆 " + name);
  }

}

3、GreenCircle.java

package com.ssm.cts.pattern.bridge;

public class GreenCircle implements IDrawAPI {
  @Override
  public void drawCircle(String name) {
    System.out.println("绿圆 " + name);
  }

}

4、Shape.java

package com.ssm.cts.pattern.bridge;

public abstract class Shape {
  protected IDrawAPI drawAPI;

  protected Shape(IDrawAPI drawAPI) {
    this.drawAPI = drawAPI;
  }

  public abstract void draw();
}

5、Circle.java

package com.ssm.cts.pattern.bridge;

public class Circle extends Shape {

  private String name;

  public Circle(String _name, IDrawAPI drawAPI) {
    super(drawAPI);
    this.name = _name;
  }

  public void draw() {
    drawAPI.drawCircle(name);
  }

}

6、Demo.java

package com.ssm.cts.pattern.bridge;

public class Demo {

  public static void main(String[] args) {
    Shape redCircle = new Circle("AA", new RedCircle());
    Shape greenCircle = new Circle("BB", new GreenCircle());

    redCircle.draw();
    greenCircle.draw();
  }

}

 

发表回复

您的电子邮箱地址不会被公开。