[AppleScript] 纯文本查看 复制代码
//定义一个接口, 和一个draw方法
public interface Shape {
void draw();
}
//实现Shape接口
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Rectangle");
}
}
//实现Shape接口
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Circle");
}
}
//装饰
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
//测试效果
public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new ShapeDecorator(new Circle());
Shape redRectangle = new ShapeDecorator(new Rectangle());
System.out.println("Circle with normal border");
circle.draw();
System.out.println("\nCircle of red border");
redCircle.draw();
System.out.println("\nRectangle of red border");
redRectangle.draw();
}
}
```
[AppleScript] 纯文本查看 复制代码
//定义接口
public interface Shape {
public void draw();
}
//实现接口
public class RealShape implement Shape{
@Override
public void draw() {
System.out.println("Shape: RealShape");
}
}
//使用代理对象实现接口
public class Proxy implements Shape{
@Override
public void draw{
//引用并创建真实对象实例,即”我“
RealShape realShape = new RealShape();
//调用真实对象的方法,进行代理
realSubject.draw();
//代理对象额外做的操作
this.proxyDraw();
}
//增强的额外操作
public void proxyDraw(){
System.out.println("Proxy Shape: RealShape");
}
}
public class ProxyPattern {
public static void main(String[] args){
Shape proxy = new Proxy();
proxy.draw();
}
}
```