2015-09-11 41 views
1

這是如何使用適配器設計模式的?對象適配器模式的使用案例

我有Draw類(實現Shapes)和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(); 

UPDATE 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(); 

回答

0

適配器模式旨在成爲兩個不兼容的接口之間的橋,而不是一個新的功能添加到現有的對象。在你的情況

平局和廣場被封閉的修改,我需要在客戶端創建一個方形物體做「繪圖」

似乎更像Decorator模式,其目的是使新而不改變它的內部結構。

所以,在Adapter模式的情況下,你的Adapter類必須實現Shapes接口,而你希望它有一個繪製方法。儘管有兩種類型的適配器 - 類和對象適配器,但它們都需要接口實現。

這是最簡單的適配器實現:

class SquareAdapter implements Shapes{ 

    Polygon square; 

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

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

請查看我添加爲編輯以上職位的答案。該示例代碼有意義嗎? – John

+0

是的,它似乎是兩種適配器模式類型的正確用法,因爲在這種情況下,您可以使用適配器類作爲Shapes的實例 – Stanislav