代理模式Proxy(结构型)

参考文档:

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"));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值