java - 对象适配器模式的使用案例

标签 java design-patterns adapter

适配器设计模式就是这样使用的吗?

我有 Draw 类(实现形状)和 Square 类(实现多边形)。现在,如果 Draw 和 Square 都无法修改,并且我需要客户端创建的方形对象来进行“绘图”,那么我就会选择适配器。

下面的实现对象适配器模式还是类适配器模式?

interface Shapes {
    public void draw();
}

class Draw implements Shapes {

    @Override
    public void draw() {
        println("Drawing a shape");
    }
}


interface Polygon {
    public void getSides();
    public void getArea();
}

class Square implements Polygon {

    int length;

    Square(int length){
        this.length = length;
    }

    @Override
    public void getSides() {
        println("Sides: 4");
    }

    @Override
    public void getArea() {
        println("Area: "+length*length);
    }
}


class SquareAdapter extends Square{

    Shapes shape;

    public SquareAdapter(Shapes shape, int length){
        super(length);
        this.shape = shape;
    }

    public void draw(){
        shape.draw();
    }
}

客户端代码:

SquareAdapter adapter = new SquareAdapter(new Draw(), 3);
adapter.draw();
adapter.getArea();
adapter.getSides();

更新1:解决方案

感谢斯坦尼斯拉夫。我以更合适的方式修改了示例。

interface Draw {
    public void draw();
}

class Circle implements Draw {

    @Override
    public void draw() {
        println("Drawing a circle");
    }
}


interface Polygon {
    public void getSides();
    public void getArea();
}

class Square implements Polygon {

    int length;

    Square(int length){
        this.length = length;
    }

    @Override
    public void getSides() {
        println("Sides: 4");
    }

    @Override
    public void getArea() {
        println("Area: "+length*length);
    }
}

//object composition adapters

class SquareAdapter implements Draw {

    Polygon square;

    public SquareAdapter(Polygon square){
        this.square = square;
    }

    @Override
    public void draw(){
        println("Drawing a square");
    }

    public Polygon getSquare() {
        return square;
    }
}

客户端代码:

Draw drawingObj = null;

//Now lets say the client wants to draw a Square but it
//doesn't implement Draw
//drawingObj = new Square();
//drawingObj.draw() //this is not possible so we write a adapter

drawingObj = new SquareAdapter(new Square(5));
drawingObj.draw();
((SquareAdapter) drawingObj).getSquare().getSides();
((SquareAdapter) drawingObj).getSquare().getArea();



//class inheritance adapters

class SquareAdapter extends Square implements Draw {

    SquareAdapter(int length) {
        super(length);
    }

    @Override
    public void draw(){
        println("Drawing a square");
    }
}

客户端代码:

Draw drawingObj = null;

//Now lets say the client wants to draw a Square but it
//doesn't implement Draw
//drawingObj = new Square();
//drawingObj.draw() //this is not possible so we write a adapter

drawingObj = new SquareAdapter(5);
drawingObj.draw();
((Square) drawingObj).getSides();
((Square) drawingObj).getArea();

最佳答案

适配器模式旨在成为两个不兼容接口(interface)之间的桥梁,而不是向现有对象添加新功能。对于你的情况

Draw and Square are closed to modifications and I need a square object created by the client to do the "drawing"

它看起来更像是装饰器模式,旨在为现有对象创建新功能,而不改变其内部结构。

因此,在适配器模式的情况下,您的适配器类必须实现 Shapes 接口(interface),同时您希望它有一个绘制方法。尽管有两种类型的适配器 - 类适配器和对象适配器,但它们都需要接口(interface)实现。

这是最简单的适配器实现:

class SquareAdapter implements Shapes{

    Polygon square;

    public SquareAdapter(Polygon square){
        this.square = square;
    }

    public void draw(){
        //some logic to draw Square object
    }
}

关于java - 对象适配器模式的使用案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32514979/

相关文章:

java - Jlabel 和不可编辑的 JTextField 之间的区别

Java - MyBatis - 一次处理一条记录

java - java中是否需要添加 volatile 关键字来保证单例类的线程安全?

python - Flask SQLAlchemy 数据映射器与事件记录模式

java - 使用适配器与代理与多个提供商集成

Scala 适配器模式 - 自动允许具有相同方法的类使用 "duck typing"

java - 有没有更有效的方法来更新 ListView 或循环遍历字符串数组

java - 加到 n 的 1 + 2 的所有组合

c# - 创建松散耦合/可扩展的软件架构

Android:如何更改 ListView 中一项的颜色?