Home >Java >javaTutorial >Java proxy mode learning

Java proxy mode learning

黄舟
黄舟Original
2017-10-13 10:31:531025browse

This article mainly introduces the relevant information of the proxy mode in java design pattern learning in detail. It has certain reference value. Interested friends can refer to

Proxy mode (Proxy): Provides a proxy for other objects to control access to this object.

Agency mode structure chart

Simulation requirements: Xiao Zhang from Class 3 likes Xiao Hong from Class 1, but does not know Xiao Hong, so he entrusts Xiao Ming from Class 1 to give it to him Little red gift.

1: Create an interface that both Xiao Zhang and Xiao Ming can implement.


package ProxyModel;

/**
 * 总的接口
 * @author 我不是张英俊
 *
 */
interface ISendGift {

  void GiveDolls();
  void GiveFlowers();
  void GiveChocolate();
}

2: Realize Xiao Zhang sending gifts.


package ProxyModel;

/**
 * 
 * 小张是送鲜花的本体,送其实是小张送的,小明只是借助小张的送的鲜花去给小红
 * @author 我不是张英俊
 *
 */
public class Pursuit implements ISendGift{

  String mm;
  public Pursuit(String mm){
    this.mm=mm;
  }
  @Override
  public void GiveDolls() {
    // TODO Auto-generated method stub
    System.out.println("送"+mm+"洋娃娃");
  }

  @Override
  public void GiveFlowers() {
    // TODO Auto-generated method stub
    System.out.println("送"+mm+"鲜花");
  }

  @Override
  public void GiveChocolate() {
    // TODO Auto-generated method stub
    System.out.println("送"+mm+"巧克力");
  }

}

3: Realize the gift sent by Xiao Ming through Xiao Zhang and transfer it to Xiao Hong.


package ProxyModel;

/**
 * 小明转送小红礼物的完成方法,其实是借助小张送的礼物来完成的,此处小明即为代理
 * @author 我不是张英俊
 *
 */
public class Proxy implements ISendGift{

  Pursuit gg;
  public Proxy(String mm){
    gg=new Pursuit(mm);
  }
  
  @Override
  public void GiveDolls() {
    // TODO Auto-generated method stub
    gg.GiveDolls();
  }

  @Override
  public void GiveFlowers() {
    // TODO Auto-generated method stub
    gg.GiveFlowers();
  }

  @Override
  public void GiveChocolate() {
    // TODO Auto-generated method stub
    gg.GiveChocolate();
  }

}

4: Test category:


package ProxyModel;

/**
 * 需求,3班小张喜欢1班小红,但是不认识小红,委托1班小明给小红送东西
 * 总结:小张送小红礼物,小张送,然后小明通过小张接过送的礼物,给小红,
 * 其实小明是借助小张送的礼物,来代理完成送礼物的操作。
 * 代理模式
 * @author 我不是张英俊
 *
 */
public class test {

  public static void main(String[] args) {
    String mm="小红";
    Proxy xiaoming=new Proxy(mm);
    xiaoming.GiveDolls();
    xiaoming.GiveFlowers();
    xiaoming.GiveChocolate();

  }

}

5: Console

Send Xiaohong Doll
Send little red flowers
Send little red chocolate

Summary:

Applicable scenarios of agent mode

1: Remote agent, that is, for An object provides local representation in different address spaces. This hides the fact that an object exists in a different address space.

2: Virtual agents are expensive objects to create as needed. Use it to store real objects that take a long time to instantiate.

3: Security proxy, used to control permissions when accessing real objects.

4: Intelligent guidance means that when the real object is called, the agent handles other things.

Advantages:

1: The proxy mode can coordinate the caller and the callee, reducing the coupling of the system to a certain extent;

2: The proxy object can be It acts as an intermediary between the client and the target object, thus protecting the target object.

Disadvantages:

1: Due to the addition of a proxy object between the client and the real object, some types of proxy modes may cause the request processing speed to slow down;

2: Implementing proxy patterns requires additional work, and the implementation of some proxy patterns is very complex.

The above is the detailed content of Java proxy mode learning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn