Agency model - wine distribution

a schema definition

In the proxy mode, there are two objects involved in processing the same request. The received request is delegated by the proxy object to the real object for processing. The proxy object controls the access of the request. It acts as an intermediary bridge between the client application and the real object. effect. The proxy pattern uses object aggregation instead of inheritance, which effectively reduces the coupling between software modules.

 

Two-mode example

1 Pattern Analysis

We use wine distribution to illustrate this pattern.


 

2 Proxy mode static class diagram


 

3 Code Examples

3.1 Wine Factory Interface - IRedWine

package com.demo.real;

/**
 * Wine interface
 *
 * @author
 *
 */
public interface IRedWine {
	// method of producing wine
	public void product();

	// method of selling wine
	public void sell();
}

3.2 RealRedWineFactory RealRedWineFactory

package com.demo.real.impl;

import com.demo.real.IRedWine;

/**
 * Real production wine factory
 *
 * @author
 *
 */
public class RealRedWineFactory implements IRedWine {
	// method of producing wine
	@Override
	public void product() {
		System.out.println("The wine factory produces wine...");
	}

	// method of selling wine
	public void sell() {
		System.out.println("The wine factory sells wine...");
	}

}

3.3 Create a red wine agent—RedWineProxy

package com.demo.proxy;

import com.demo.real.IRedWine;

/**
 * Wine agent
 *
 * @author
 *
 */
public class RedWineProxy implements IRedWine {

	// real wine producer
	private final IRedWine redWine;
	// The agent's permission to sell wine
	private final boolean permission = true;

	// default constructor
	public RedWineProxy(IRedWine redWine) {
		this.redWine = redWine;
	}

	// The agent produces wine method (the agent does not produce red wine, and sells it from the real factory)
	@Override
	public void product() {
		// Determine whether the agent has the right to act as a wine agent
		if (this.permission) {
			// It is legal for agents to have the authority to sell wine
			System.out.println("[This is a legal wine dealer]");
			System.out.println("The agent receives the order and informs the factory to produce...");
			this.redWine.product();
		} else {
			System.out.println("[This is an illegal wine dealer!]");
		}

	}

	// method for agents to sell wine
	@Override
	public void sell() {
		if (this.permission) {
			this.redWine.sell();
			System.out.println("The agent gets the wholesale price red wine from the factory, and then sells it at a higher price, earning a certain price difference...");
		} else {
			System.out.println("[This is an illegal wine dealer!]");
		}
	}

}

3.4 Buying wine from a wine agent—Client

package com.demo;

import com.demo.proxy.RedWineProxy;
import com.demo.real.IRedWine;
import com.demo.real.impl.RealRedWineFactory;

/**
 * Main application
 *
 * @author
 *
 */
public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Create an instance of the real wine factory object
		IRedWine realRedWineFactory = new RealRedWineFactory ();
		// get the proxy object instance
		IRedWine redWineProxy = new RedWineProxy(realRedWineFactory);
		// The agent produces red wine (in fact, the real production is the factory)
		redWineProxy.product();
		// Agents sell red wine (get the goods at wholesale prices, and then sell them at higher prices to earn the difference profit)
		redWineProxy.sell();
	}
}

4 Running results

[ This is a legitimate wine agent ]

The agent receives the order and informs the factory to produce...

The wine factory produces wine...

Wine factory sells red wine...

The agent gets the wholesale price red wine from the factory, and then sells it at a higher price, earning a certain price difference...

 

Three principles of pattern design

1 Lazy loading to improve system efficiency

2 Single Responsibility Principle

 

Four use cases

1 Remote proxy: Provides a local proxy for an object in a different address space.

2 Virtual proxy: If the creation of an object is very time-consuming, it can be called through the proxy object. Before the real object is created, a fake call is returned. When the real object is created, the real object is returned to the customer. corresponding method calls.

3 The protection agent controls access to the original object.

4 Smart pointers replace simple pointers, which perform some additional operations when accessing objects.

 

Five-agent mode static class diagram


 

Abstract Role: Declares the common interface of the real object and the proxy object.

Real role: The target object that actually handles the request.

Proxy role: The proxy object role contains a reference to the real object, so that the proxy object can convert the request to the real object for processing. At the same time, the proxy object can also add additional operations before and after the real object operation.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326643321&siteId=291194637