栏目:设计模式(<--点此查看更多)Java 的 23 中设计模式
  • Adapter(适配器) 模式代码示例

  • Wed Jan 09 23:13:22 CST 2013 编程者 我要收藏(...) 评论 ...
  • 对象模式实现: 类Point,表示画面坐标中的点类TextView,工具箱中的文本组件类,已经存在的类,对应Adapter模式中的Adaptee类TextShape,对象模式实现的Adapter
  • 对象模式实现:

    类Point,表示画面坐标中的点

    package qinysong.pattern.adapter;

    public class Point {
      private int coordinateX;
      private int coordinateY;

      public Point(int coordinateX, int coordinateY){
        this.coordinateX = coordinateX;
        this.coordinateY = coordinateY;
      }
      public String toString(){
        return "Point[x=" + coordinateX + ",y=" + coordinateY + "]";
      }
      public int getCoordinateX() {
        return coordinateX;
      }
      public int getCoordinateY() {
        return coordinateY;
      }
    }

    类Shape,表示图元接口,对应Adapter模式中的Target

    package qinysong.pattern.adapter;

    public interface Shape {
      public Point getBottomLeftPoint();
      public Point getTopRightPoint();
    }

    类TextView,工具箱中的文本组件类,已经存在的类,对应Adapter模式中的Adaptee(受改造者)


    package qinysong.pattern.adapter;

    public class TextView {

      public int getCoordinateX() {
        System.out.println("TextView.getCoordinateX()...");
        return 10;
      }
      public int getCoordinateY() {
        System.out.println("TextView.getCoordinateY()...");
        return 20;
      }
      public int getHeight() {
        System.out.println("TextView.getHeight()...");
        return 30;
      }
      public int getWidth() {
        System.out.println("TextView.getWidth()...");
        return 40;
      }
      public boolean isEmpty(){
        return false;
      }
    }

    类TextShape,对象模式实现的Adapter

    package qinysong.pattern.adapter;

    public class TextShape implements Shape {
      private TextView textView;

      public TextShape(TextView textView){
        this.textView = textView;
      }

      //通过TextView的实例进行协调实现
      public Point getBottomLeftPoint() {
        System.out.println("TextShape.getBottomLeftPoint()...");
        int coordinateX = textView.getCoordinateX();
        int coordinateY = textView.getCoordinateY();
        return new Point(coordinateX, coordinateY);
      }

      //通过TextView的实例进行协调实现
      public Point getTopRightPoint() {
        System.out.println("TextShape.getTopRightPoint()...");
        int coordinateX = textView.getCoordinateX();
        int coordinateY = textView.getCoordinateY();
        int height = textView.getHeight();
        int width = textView.getWidth();
        return new Point(coordinateX + width, coordinateY + height);
      }
    }

    类Client,Adapter模式的客户

    package qinysong.pattern.adapter;

    public class Client {
      public static void main(String[] args){
        System.out.println("Client.main begin ..........");
        System.out.println("Client.main 以下是通过实例委托方式实现的Adapter");
        Shape shape = new TextShape(new TextView());
        Point bottomLeft = shape.getBottomLeftPoint();
        Point topRight = shape.getTopRightPoint();
        System.out.println("Client.main shape's bottomLeft:" + bottomLeft);
        System.out.println("Client.main shape's topRight:" + topRight);

        System.out.println(" Client.main 以下是通过类继承方式实现的Adapter");
        Shape shape2 = new TextShape2();
        bottomLeft = shape2.getBottomLeftPoint();
        topRight = shape2.getTopRightPoint();
        System.out.println("Client.main shape2's bottomLeft:" + bottomLeft);
        System.out.println("Client.main shape2's topRight:" + topRight);
        System.out.println("Client.main end   ..........");
      }
    }

    类模式实现

    package qinysong.pattern.adapter;

    public class TextShape2 extends TextView implements Shape {

      //通过所继承的TextView,进行协调实现
      public Point getBottomLeftPoint() {
        System.out.println("TextShape2.getBottomLeftPoint()...");
        int coordinateX = getCoordinateX();
        int coordinateY = getCoordinateY();
        return new Point(coordinateX, coordinateY);
      }

      //通过所继承的TextView,进行协调实现
      public Point getTopRightPoint() {
        System.out.println("TextShape2.getTopRightPoint()...");
        int coordinateX = getCoordinateX();
        int coordinateY = getCoordinateY();
        int height = getHeight();
        int width = getWidth();
        return new Point(coordinateX + width, coordinateY + height);
      }

      //注意: 这一点体现了类模式的优势,可以很方便地重定义父类TextView中的方法
      public int getCoordinateX() {
        System.out.println("TextShape2.getCoordinateX()...");
        return 100;
      }
    }
  • 信息来源:http://www.cnblogs.com/ (举报这篇文章)
  • 好评(...) 中评(...) 差评(...)