对象适配器模式的使用案例
Usage case of object adapter pattern
适配器设计模式是这样使用的吗?
我有绘图 class(实现形状)和方形 class(实现多边形)。现在,如果 Draw 和 Square 都禁止修改,我需要一个由客户端创建的方形对象来执行 "drawing",那么我会选择一个适配器。
下面的实现对象适配器模式还是class适配器模式?
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();
适配器模式旨在成为两个不兼容接口之间的桥梁,而不是向现有对象添加新功能。在你的情况下
Draw and Square are closed to modifications and I need a square object created by the client to do the "drawing"
它看起来更像是装饰器模式,旨在为现有对象添加新功能,而不改变其内部结构。
因此,在 Adapter 模式的情况下,您的 Adapter class 必须实现 Shapes 接口,而您希望它具有 draw 方法。虽然有两种类型的适配器 - Class 和对象适配器,但它们都需要接口实现。
这是最简单的适配器实现:
class SquareAdapter implements Shapes{
Polygon square;
public SquareAdapter(Polygon square){
this.square = square;
}
public void draw(){
//some logic to draw Square object
}
}
适配器设计模式是这样使用的吗?
我有绘图 class(实现形状)和方形 class(实现多边形)。现在,如果 Draw 和 Square 都禁止修改,我需要一个由客户端创建的方形对象来执行 "drawing",那么我会选择一个适配器。
下面的实现对象适配器模式还是class适配器模式?
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();
适配器模式旨在成为两个不兼容接口之间的桥梁,而不是向现有对象添加新功能。在你的情况下
Draw and Square are closed to modifications and I need a square object created by the client to do the "drawing"
它看起来更像是装饰器模式,旨在为现有对象添加新功能,而不改变其内部结构。
因此,在 Adapter 模式的情况下,您的 Adapter class 必须实现 Shapes 接口,而您希望它具有 draw 方法。虽然有两种类型的适配器 - Class 和对象适配器,但它们都需要接口实现。
这是最简单的适配器实现:
class SquareAdapter implements Shapes{
Polygon square;
public SquareAdapter(Polygon square){
this.square = square;
}
public void draw(){
//some logic to draw Square object
}
}