参考文档:
1.设计模式-可复用面向对象软件的基础
2.http://blog.csdn.net/hguisu/article/details/7542143(设计模式(十一)代理模式Proxy(结构型))
代理模式Proxy(结构型)
理论方面的知识,可以参考 参考文档中的知识,本博客只是实现一个案例。
结构,看图:
调用逻辑,看图:
案例实现,案例为书中代码实现中的第一个案例。
代码实现:
Point.java:
package com.rick.designpattern.proxy; /** * Created by MyPC on 2017/6/15. */ public class Point { private int textSize; private int textColor; public Point() { } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public int getTextSize() { return textSize; } public void setTextSize(int textSize) { this.textSize = textSize; } public Point(int textSize, int textColor) { this.textSize = textSize; this.textColor = textColor; } }
Event.java:
package com.rick.designpattern.proxy; /** * Created by MyPC on 2017/6/15. */ public class Event { private int x,y; public Event() { } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public Event(int x, int y) { this.x = x; this.y = y; } }
Graphic.java:
package com.rick.designpattern.proxy; import java.io.InputStream; import java.io.OutputStream; /** * Created by MyPC on 2017/6/15. */ public class Graphic { public Graphic() { } public void draw(Point point) { } public void handleMouse(Event event) { } public Point getExtent() { return null; } public void load(InputStream inputStream) { } public void save(OutputStream outputStream) { } }
Image.java:
package com.rick.designpattern.proxy; import java.io.InputStream; import java.io.OutputStream; /** * Created by MyPC on 2017/6/15. */ public class Image extends Graphic { private String fileName; private Point point; public Image(String fileName) { this.fileName = fileName; } public Image() { } @Override public void draw(Point point) { this.point = point; System.out.println("字体大小:" + point.getTextSize() + "sp;字体颜色:" + point.getTextColor()); } @Override public void handleMouse(Event event) { super.handleMouse(event); } @Override public Point getExtent() { return point; } @Override public void load(InputStream inputStream) { System.out.println("Class Image is method load"); } @Override public void save(OutputStream outputStream) { System.out.println("Class Image is method save"); } }
ImageProxy.java:
package com.rick.designpattern.proxy; import java.io.InputStream; import java.io.OutputStream; /** * Created by MyPC on 2017/6/15. */ public class ImageProxy extends Graphic { private String fileName; private Image image; private Point point; public ImageProxy(String fileName) { this.fileName = fileName; point = new Point(15, 0x000000); image = null; } public ImageProxy() { } @Override public void draw(Point point) { getImage().draw(point); } @Override public void handleMouse(Event event) { getImage().handleMouse(event); } @Override public Point getExtent() { if (null != point) { point = getImage().getExtent(); } return point; } @Override public void load(InputStream inputStream) { getImage().load(inputStream); } @Override public void save(OutputStream outputStream) { getImage().save(outputStream); } public Image getImage() { if (null == image) { image = new Image(fileName); } return image; } }
TextDocument.java:
package com.rick.designpattern.proxy; /** * Created by MyPC on 2017/6/15. */ public class TextDocument { public TextDocument() { } public void insert(Graphic graphic) { graphic.draw(new Point(30, 0x777777)); } }
Client.java:
package com.rick.designpattern.proxy; /** * Created by MyPC on 2017/6/15. */ public class Client { public static void main(String[] args) { TextDocument textDocument = new TextDocument(); textDocument.insert(new ImageProxy("test.txt")); } }