2.構造型モード(5)ブリッジモード


詳細
2.構造型モード(5)ブリッジモード

  (Bridge)                   ,        ,          。

            ,           。

  :
1.         。
2.        。
3.          。
  :
                    ,              ,                。

1.ブリッジモード

package com.andrew.pattern0205.bridge.model01;
public interface DrawAPI {
    public void drawCircle(int radius, int x, int y);
}

package com.andrew.pattern0205.bridge.model01;
public class RedCircle implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: " + radius +", x: " +x+", "+ y +"]");
    }
}

package com.andrew.pattern0205.bridge.model01;
public class GreenCircle implements DrawAPI {
    @Override
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: green, radius: " + radius +", x: " +x+", "+ y +"]");
    }
}

package com.andrew.pattern0205.bridge.model01;
public abstract class Shape {
    protected DrawAPI drawAPI;
    protected Shape(DrawAPI drawAPI) {
        this.drawAPI = drawAPI;
    }
    public abstract void draw();
}

package com.andrew.pattern0205.bridge.model01;
public class Circle extends Shape {
    private int x, y, radius;
    public Circle(int x, int y, int radius, DrawAPI drawAPI) {
        super(drawAPI);
          this.x = x;  
          this.y = y;  
          this.radius = radius;
    }
    public void draw() {
        drawAPI.drawCircle(radius,x,y);
    }
}

package com.andrew.pattern0205.bridge.model01;
/**
 * 1.     
 * 
 * @author andrew
 * @date 2018/07/15
 */
public class Client {
    public static void main(String[] args) {
        Shape redCircle = new Circle(100,100, 10, new RedCircle());
        Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
        redCircle.draw();
        greenCircle.draw();
    }
}
    :
Drawing Circle[ color: red, radius: 10, x: 100, 100]
Drawing Circle[ color: green, radius: 10, x: 100, 100]