ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Design Pattern - Proxy(Java)

2019-03-23 17:55:03  阅读:375  来源: 互联网

标签:Java Chimomo Pattern param Design proxy double return chimomo


分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

Definition

Provide a surrogate or placeholder for another object to control access to it.

Participants

    The classes and/or objects participating in this pattern are:

  • Proxy (MathProxy)
    • Maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • Provides an interface identical to Subject's so that a proxy can be substituted for the real subject.
    • Controls access to the real subject and may be responsible for creating and deleting it.
    • Other responsibilites depend on the kind of proxy:
      • Remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
      • Virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
      • Protection proxies check that the caller has the access permissions required to perform a request.
  • Subject (IMath)
    • Defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
  • RealSubject (Math)
    • Defines the real object that the proxy represents.

Sample Code in Java


This structural code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.sample;

/**
 * Startup class for Structural Proxy Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args The arguments
     */
    public static void main(String[] args) {

        // Create proxy and request a service.
        Proxy proxy = new Proxy();
        proxy.request();
    }

}

/*
Output:
Called RealSubject.request()
 */
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.sample;

/**
 * The 'Proxy' class.
 *
 * @author Chimomo
 */
class Proxy extends Subject {

    // The real subject.
    private RealSubject realSubject;

    /**
     * Request.
     */
    @Override
    public void request() {

        // Use 'lazy initialization'.
        if (this.realSubject == null) {
            this.realSubject = new RealSubject();
        }

        this.realSubject.request();
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.sample;

/**
 * The 'RealSubject' class.
 *
 * @author Chimomo
 */
class RealSubject extends Subject {

    /**
     * Request.
     */
    @Override
    public void request() {
        System.out.println("Called RealSubject.request()");
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.sample;

/**
 * The 'Subject' abstract class.
 *
 * @author Chimomo
 */
abstract class Subject {

    /**
     * Request.
     */
    public abstract void request();

}

This real-world code demonstrates the Proxy pattern for a Math object represented by a MathProxy object.

/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.realworld;

/**
 * The 'Subject' interface.
 *
 * @author Chimomo
 */
public interface IMath {

    /**
     * Add.
     *
     * @param x The x
     * @param y The y
     * @return The sum of x and y
     */
    double add(double x, double y);

    /**
     * Sub.
     *
     * @param x The x
     * @param y The y
     * @return The difference of x and y
     */
    double sub(double x, double y);

    /**
     * Mul.
     *
     * @param x The x
     * @param y The y
     * @return The product of x and y
     */
    double mul(double x, double y);

    /**
     * Div.
     *
     * @param x The x
     * @param y The y
     * @return The quotient of x and y
     */
    double div(double x, double y);

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.realworld;

/**
 * The 'RealSubject' class.
 *
 * @author Chimomo
 */
class Math implements IMath {

    /**
     * Add.
     *
     * @param x The x
     * @param y The y
     * @return The sum of x and y
     */
    @Override
    public double add(double x, double y) {
        return x + y;
    }

    /**
     * Sub.
     *
     * @param x The x
     * @param y The y
     * @return The difference of x and y
     */
    @Override
    public double sub(double x, double y) {
        return x - y;
    }

    /**
     * Mul.
     *
     * @param x The x
     * @param y The y
     * @return The product of x and y
     */
    @Override
    public double mul(double x, double y) {
        return x * y;
    }

    /**
     * Div.
     *
     * @param x The x
     * @param y The y
     * @return The quotient of x and y
     */
    @Override
    public double div(double x, double y) {
        return x / y;
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.realworld;

/**
 * The 'Proxy Object' class.
 *
 * @author Chimomo
 */
class MathProxy implements IMath {

    // The math.
    private Math math = new Math();

    /**
     * Add.
     *
     * @param x The x
     * @param y The y
     * @return The sum of x and y
     */
    @Override
    public double add(double x, double y) {
        return this.math.add(x, y);
    }

    /**
     * Sub.
     *
     * @param x The x
     * @param y The y
     * @return The difference of x and y
     */
    @Override
    public double sub(double x, double y) {
        return this.math.sub(x, y);
    }

    /**
     * Mul.
     *
     * @param x The x
     * @param y The y
     * @return The product of x and y
     */
    @Override
    public double mul(double x, double y) {
        return this.math.mul(x, y);
    }

    /**
     * Div.
     *
     * @param x The x
     * @param y The y
     * @return The quotient of x and y
     */
    @Override
    public double div(double x, double y) {
        return this.math.div(x, y);
    }

}
/*
 *  Chimomo's Blog: https://blog.csdn.net/chimomo/
 */
package chimomo.learning.java.designpattern.proxy.realworld;

/**
 * Startup class for Real-World Proxy Design Pattern.
 *
 * @author Chimomo
 */
class Program {

    /**
     * Entry point into console application.
     *
     * @param args The arguments
     */
    public static void main(String[] args) {

        // Create math proxy.
        MathProxy proxy = new MathProxy();

        // Do the math.
        System.out.println("4 + 2 = " + proxy.add(4, 2));
        System.out.println("4 - 2 = " + proxy.sub(4, 2));
        System.out.println("4 * 2 = " + proxy.mul(4, 2));
        System.out.println("4 / 2 = " + proxy.div(4, 2));
    }

}

/*
Output:
4 + 2 = 6.0
4 - 2 = 2.0
4 * 2 = 8.0
4 / 2 = 2.0
 */

标签:Java,Chimomo,Pattern,param,Design,proxy,double,return,chimomo
来源: https://blog.csdn.net/chimomo/article/details/80988300

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有