Designpattern_java:proxy Pattern

Source: Internet
Author: User

Agent mode proxy pattern delegate mode provide a surrogate or placeholder for another object to control access to it. Provides a proxy for other objects to control the visit to this object Ask. Abstract theme Role (Subject): This role is a common interface for real-world themes and proxy topics so that you can use the proxy theme wherever you can use real-world themes.
package com.DesignPattern.Structural.Proxy;public interface Subject {    // 定义一个请求方法    public void request();}
Proxy Subject: Also known as the Delegate class, the proxy class, which is responsible for controlling references to real topics, creating or deleting real-world subject objects when needed, and preprocessing and aftercare before and after real-world theme roles are processed.
package com.DesignPattern.Structural.Proxy;public class ProxySubject implements Subject {    private Subject subject;    public ProxySubject(Subject subject) {        this.subject = subject;    }    // 实现请求方法    @Override    public void request() {        this.beforeRequest();        subject.request();        this.afterRequest();    }    //请求前的操作    private void beforeRequest() {        // 预处理    }    //请求后的操作    private void afterRequest() {        // 善后处理    }}
Real Subject: This role is also called the delegated role, the agent role, and the specific performer of the business logic.
package com.DesignPattern.Structural.Proxy;public class RealSubject implements Subject {    @Override    public void request() {        // 业务处理逻辑        System.out.println("realSubject request");    }}
Example of proxy mode Igameplayer.java
package com.DesignPattern.Structural.Proxy;public interface IGamePlayer {    public void killBoss();// 杀怪    public void upGrade();// 升级}
Gameplayer.java
package com.DesignPattern.Structural.Proxy;public class GamePlayer implements IGamePlayer {    private String name = "";    public GamePlayer(String name) {        this.name = name;    }    @Override    public void killBoss() {        System.out.println(this.name + " killBoss");    }    @Override    public void upGrade() {        System.out.println(this.name + " upgrade level 1");    }}
Gameplayerproxy.java
package com.DesignPattern.Structural.Proxy;import java.util.Date;public class GamePlayerProxy implements IGamePlayer {    private IGamePlayer player=null;    public GamePlayerProxy(IGamePlayer player){        this.player=player;    }    //记录打怪时间    private void log(){        System.out.println("打怪时间"+new Date().toString());    }    @Override    public void killBoss() {        this.log();        player.killBoss();    }    @Override    public void upGrade() {        player.upGrade();        this.count();    }    //计算升级所用的时间    private void count(){        System.out.println("upgrade cost time!");    }}
Clientdemo.java
package com.DesignPattern.Structural.Proxy;public class ClientDemo {    public static void main(String[] args){        IGamePlayer player=new GamePlayer("Tom");        IGamePlayer proxy=new GamePlayerProxy(player);        proxy.killBoss();        proxy.upGrade();    }}

Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.

Designpattern_java:proxy Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.