The difference between adapter mode and proxy mode

Proxy mode

composition:

Abstract role: A business method that declares a real role through an interface or abstract class.

Agent role: Implement abstract role, it is the agent of real role, realize the abstract method through the business logic method of real role, and attach its own operation.

Real role: Implement the abstract role and define the business logic to be implemented by the real role for the agent role to call.

Proxy Pattern, one of 23 commonly used object-oriented software design patterns. (The design pattern comes from the book Design Patterns, formerly known as Design Patterns: Elements of Reusable Object-Oriented Software. Published in 1995, publisher: Addison Wesly Longman. Inc. The book proposes 23 basic design patterns. For the first time, the design pattern has been upgraded to a theoretical level and standardized.)

Proxy mode definition: Provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or can not directly reference another object, and the proxy object can act as an intermediary between the client and the target object.

(1) Clear responsibilities

The real role is to realize the actual business logic, do not care about other non-responsible transactions, complete a transaction through the late agent, the incidental result is simple and clear programming.

(2) The proxy object can act as an intermediary between the client and the target object , thus acting as an intermediary and protecting the target object.

(3) High scalability

The difference between adapter mode and proxy mode

Adapter mode

In computer programming, an adapter pattern (sometimes called a wrapper style or wrapper) adapts the interface of a class to what the user expects. An adaptation allows classes that normally cannot work together because the interfaces are incompatible, by wrapping the class's own interface in an existing class.

There are two types of adapter modes:

Object adapter mode

-- In this adapter mode, the adapter holds an instance of the class it wraps around. In this case, the adapter calls the physical entity of the wrapped object.

Class adapter mode

-- In this adapter mode, the adapter inherits from the implemented class (general multiple inheritance).

Prerequisites for use:

1. All methods to be implemented are specified in the interface.

2. But a concrete class that implements this interface uses only a few of them, and the other methods are useless.

Implementation

1. Implementing an existing interface with an abstract class and implementing all the methods specified in the interface, the implementation of these methods can be "plain" implementation----empty method; but the method in this class is a concrete method, and It is not an abstract method. Otherwise, all methods are still implemented in a specific subclass, which loses the original role of the adapter.

2. The subclass that originally implements the interface can only implement the abstract class in 1 and implement it only when it is implemented internally.

Precautions

1. The class that acts as the adapter is: an abstract class that implements an existing interface.

2. Why use an abstract class:

This class is not to be instantiated. The role of acting as an adapter only provides a common interface for its subclasses, but its subclasses can focus on where they are interested.

Pattern resolution

You want to use an existing adapter mode, and his interface does not meet your needs. You want to create a class that can be reused, which can work with other unrelated classes or unforeseen classes. You want to use some subclasses that already exist, but it's impossible to subclass each one to match their interface, and the object adapter can adapt to its parent class interface. The adapter is like a common transformer. It is also like the power cable between the transformer and the power strip of the computer. Although they are all 3-phase, the jack on the back of the computer cannot be directly plugged into the power strip.

The difference between adapter mode and proxy mode

The difference between adapter mode and proxy mode

An introduction

Adapter mode: Adapter mode (English: adapter pattern) is sometimes called packaging style or packaging. Transfer the interface of a class to what the user expects. An adaptation works together for classes that cannot work together because the interfaces are incompatible, by wrapping the class's own interface in an existing class.

Proxy mode: Provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or can not directly reference another object, and the proxy object can act as an intermediary between the client and the target object.

Two puzzles

Design patterns are always decoupled by adding layers to improve extensibility, but if we can't see the true abstract meaning of this layer from this abstract dimension, then it is difficult to understand the true meaning of a pattern. I have this question, both of them have added a layer, but what is the difference between this layer?

Three understanding

Obviously, the adapter mode is caused by the inconsistency between the old and new interfaces, which causes the client to be unable to be satisfied. However, since the old interface cannot be completely refactored, we also want to use some services that implement this interface. Then in order to use the service that used to implement the old interface, we should convert the new interface into the old interface; the class that implements this conversion is the abstract meaning converter.

For example, the early enumeration interface in Java is EnumeraTIon and the enumeration interface defined is Iterator; there are many old classes that implement the enumeraTIon interface to expose some services, but these services we now want to pass the Iterator interface instead of EnumeraTIon The interface is called, then an adapter is needed, then the client can use this service (the server only wants to use Iterator or only know this interface).

Compared with the application scenario of the adapter, the proxy is different. Although the proxy is also added a layer, the interface provided by the proxy is the same as the original interface. The role of the proxy mode is to not expose the implementation directly to the client. Instead, through the proxy layer, the agent can do some processing.

Electronic Components Resistor

Resistor (Resistor) commonly known as resistance directly in our daily life.It is a current limiting element. When the resistance is connected to the circuit, the resistance value of the resistor is fixed, usually two pins.Fixed resistors are those whose resistances cannot be changed.Resistance variable is called potentiometer or variable resistor.The ideal resistor is linear, that is, the instantaneous current through the resistor is proportional to the applied instantaneous voltage.Variable resistor for partial pressure.On the exposed resistor body, one or two movable metal contacts are pressed tightly.The contact position determines the resistance between any end of the resistance body and the contact.

Flat tube resistor
Metal aluminum shell resistor
Stainless steel resistor box
Crowbar (wind power converter) resistor
RX21 Glazed Wirewound Resistor
RX21 Glazed Power Wirewound Resistor
Inverter ceramic resistor
Resistor Cabinet
Resistive load box
Ultra-thin aluminum shell resistor
High power resistor
water cooling resistor
RI80 high voltage resistor
RX20 High Power Painted Wirewound Resistor
stainless steel resistor
Howo resistor
KNP Porcelain Rod Painted Wirewound Resistor
Cement Resistor
Thick Film Resistors

RESISTOR

Electronic Components Resistor,Metal Film Resistor,Metal Oxide Film Resistor

YANGZHOU POSITIONING TECH CO., LTD. , https://www.pst-thyristor.com

This entry was posted in on