Facade in Java

Facade design pattern

  • Identify the desired unified interface for a set of subsystems
  • Design a “wrapper” class that can encapsulate the use of the subsystems
  • The client uses (is coupled to) the Facade
  • The facade/wrapper “maps” to the APIs of the subsystems
// 1. Subsystem
class PointCarte {
  private double x, y;
  public PointCarte( double xx, double yy ) {
    x = xx;
    y = yy;
  }
  public void  move( int dx, int dy ) {
    x += dx;
    y += dy;
  }
  public String toString() {
    return "(" + x + "," + y + ")";
  }
  public double getX() {
    return x;
  }
  public double getY() {
    return y;
  } 
}
// 1. Subsystem
class PointPolar {
  private double radius, angle;
  public PointPolar( double r, double a ) {
    radius = r;
    angle = a;
  }
  public void  rotate( int ang ) {
    angle += ang % 360;
  }
  public String toString() {
    return "[" + radius + "@" + angle + "]";
  }
}
// 1. Desired interface: move(), rotate()
class Point {
  private PointCarte pc; // 2. Design a "wrapper" class

  public Point( double xx, double yy ) {
    pc = new PointCarte( xx,yy );
  }
  public String toString() {
    return pc.toString();
  }

  // 4. Wrapper maps
  public void move( int dx, int dy ) {
    pc.move( dx,dy );
  }
  public void rotate( int angle, Point o ) {
    double x = pc.getX() - o.pc.getX();
    double y = pc.getY() - o.pc.getY();
    PointPolar pp = new PointPolar( Math.sqrt(x*x+y*y),
                          Math.atan2(y,x)*180/Math.PI );
    // 4. Wrapper maps
    pp.rotate( angle );
    System.out.println( "  PointPolar is " + pp );
    String str = pp.toString();  int i = str.indexOf( '@' );
    double r = Double.parseDouble( str.substring(1,i) );
    double a = Double.parseDouble( str.substring(i+1,str.length()-1) );
    pc = new PointCarte(r*Math.cos(a*Math.PI/180) + o.pc.getX(),
                  r*Math.sin(a*Math.PI/180) + o.pc.getY() );
  }
}

class Line {
  private Point o, e;
  public Line( Point ori, Point end ) {
    o = ori;
    e = end;
  }
  public void  move( int dx, int dy ) {
    o.move( dx, dy );
    e.move( dx, dy );
  }
  public void  rotate( int angle ) {
    e.rotate( angle, o );
  }
  public String toString() {
    return "origin is " + o + ", end is " + e;
  }
}

class FacadeDemo {
  public static void main( String[] args ) {
    // 3. Client uses the Facade
    Line line1 = new Line( new Point(2,4), new Point(5,7) );
    line1.move(-2,-4);
    System.out.println( "after move:  " + line1 );
    line1.rotate(45);
    System.out.println( "after rotate: " + line1 );
    Line line2 = new Line( new Point(2,1), new Point(2.866,1.5) );
    line2.rotate(30);
    System.out.println( "30 degrees to 60 degrees: " + line2 );
  }
}

Output

after move:   origin is (0.0,0.0), end is (3.0,3.0)
   PointPolar is [4.242@90.0]
after rotate: origin is (0.0,0.0), end is (0.000,4.242)
   PointPolar is [0.999@60.0]
30 degrees to 60 degrees: origin is (2.0,1.0), end is (2.499,1.866)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值