编程语言
首页 > 编程语言> > Design Pattern - Proxy(Java)

Design Pattern - Proxy(Java)

作者:互联网

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击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:

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