Advantages and disadvantages of the adapter mode

An adapter is an interface converter that can be a separate hardware interface device that allows a hardware or electronic interface to be connected to other hardware or electronic interfaces, or an information interface. For example: power adapter, tripod base adapter, USB and serial adapters.

A laboratory centrifuge can usually be equipped with multiple rotors with different capacities. When two centrifuges of the same type but different capacities are required in the same centrifuge, it is conceivable to place an adapter in the large-capacity test tube hole to reach the test tube hole suitable for the small-capacity test tube, so that only a large capacity is required. The rotor and a set of adapters can meet the requirements, which can eliminate the complicated process of dismantling when changing the rotor, and save money, because the price of a set of adapters is much lower than the price of a rotor.

Advantages and disadvantages of the adapter mode

Java adapter mode

The adapter mode is to convert the interface of one class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together. Functionally, classes that are not compatible with these interfaces generally have the same or similar functionality. Usually we solve this interface incompatibility by modifying the interface of this class, but if we don't want to modify the original interface for an application, or we don't have the source code of the object at all, the adapter mode will be sent. It’s time to use.

Advantages of the adapter:

Decoupling the target class from the adapter class

2, increase the transparency and reusability of the class, encapsulate the specific implementation in the adapter class, transparent to the client class, and improve the reusability of the adapter

3, flexibility and scalability are very good, in line with the principle of opening and closing

The roles involved in the adapter include the following:

Target: Defines a specific interface used by a client.

Client: Use the target interface to cooperate with objects that are consistent with the target interface.

Adaptee: An existing interface that needs to be adapted.

Adapter: The interface responsible for converting the Adaptee interface to Target. The adapter is a concrete class, which is the core of the pattern.

Adapters are divided into class adapters and object adapters, which are described in more detail below.

Class adapter

The so-called class adapter refers to the adapter adapter inheriting our adapted Adaptee and implementing the target interface Target. Since Java is a single inheritance, this adapter can only serve the inherited Adaptee. code show as below:

Adapted (Adaptee)

16package com.bluemsun.classadapter;

Public class Person {

Private int id;

Private String name;

/**

* person can only speak English now

*/

Public void sayEnglish(){

System.out.println("Person can say english!");

}

/**

* Omit the setter, getter.

*/

}

Target interface

Package com.bluemsun.classadapter;

/**

* The target requires the person to speak English, French, and Japanese. But now the person can only speak English.

* @author Administrator

*

*/

Public interface Target_Person {

Void sayEnglish();

Void sayFrench();

Void sayJapanese();

}

Adapter

Package com.bluemsun.classadapter;

/**

* Class adapter, because it inherits Person, and Java can only inherit from one, so this adapter only serves one class of person.

* This adapter allows the person class to implement the method specified by the target interface without modifying the source code.

* @author Administrator

*

*/

Public class Adapter_Person extends Person implements Target_Person{

@Override

Public void sayFrench() {

System.out.println("Person can say French!");

}

@Override

Public void sayJapanese() {

System.out.println("Person can say Japanese!");

}

}

Client (Client)

Package com.bluemsun.classadapter;

Public class Test {

Public staTIc void main(String[] args) {

Target_Person person = new Adapter_Person();

person.sayEnglish();

person.sayFrench();

person.sayJapanese();

}

}

The simple code above demonstrates the role of the class adapter. As we said at the beginning, this adapter Adapter can only serve one class of Person. At this time, you may think that if I need to adapt a lot of classes, do I need to write an Adapter for each class that needs to be adapted? Is there a more flexible way? The answer is: Yes! This is the object adapter we talked about below.

Object adapter

The so-called object adapter, in simple terms, is that the adapter implements our target interface, but does not inherit the classes that need to be adapted. Instead, adaptation is done by passing in the class that needs to be adapted in the constructor of the adapter. The code is as follows: (Target, Adaptee is the same as above)

Adapter

Package com.bluemsun.objectdapter;

Import com.bluemsun.classadapter.Person;

Import com.bluemsun.classadapter.Target_Person;

/**

* Object adapter, unlike class adapters: object adapters can be adapted to multiple sources to target

* @author Administrator

*

*/

Public class Adapter_Person implements Target_Person{ //implement only the target interface

Private person person;

/ / In the constructor pass the Adaptee class Person into

Public Adapter_Person(Person person){

This.person = person;

}

/ / Realize the sayEnglish () in the target interface - call sayEnglish () in Adaptee

@Override

Public void sayEnglish() {

this.person.sayEnglish();

}

/ / Implement other methods in the interface

@Override

Public void sayFrench() {

System.out.println("person can say French!");

}

@Override

Public void sayJapanese() {

System.out.println("person can say Japanese!");

}

}

Client (Client)

Package com.bluemsun.objectdapter;

Import com.bluemsun.classadapter.Person;

Import com.bluemsun.classadapter.Target_Person;

Public class Test {

Public staTIc void main(String[] args) {

Target_Person person = new Adapter_Person(new Person());

person.sayEnglish();

person.sayFrench();

person.sayJapanese();

}

}

The object adapter can be adapted to multiple classes with adaptations. You only need to pass different classes with adaptations in the constructor of the Adapter. Flexible.

Advantages of class adapters:

1, because the adapter class is a subclass of the adapter class, so you can replace some adapter methods in the adapter class, making the adapter more flexible.

Disadvantages of class adapters :

1. For languages ​​such as Java and C# that do not support multiple inheritance, only one adapter class can be adapted at a time, and the target abstract class can only be an interface, not a class. Its use has certain limitations and cannot be used. The adapter class and its subclasses are simultaneously adapted to the target interface.

Advantages of the object adapter:

1. Adapt multiple different adapters to the same target, that is, the same adapter can adapt the adapter class and its subclasses to the target interface.

Disadvantages of object adapters:

1. Compared with the class adapter mode, it is not easy to replace the adapter class.

Press Brake Protection

BLPS laser safety protective device is designed for personal safety used on hydraulic bender.
The dynamic test technology it used has passed the Type 4 functional safety assessment by TUV, and get the national invention patent. The product reaches the advanced technological level of similar products.
BLPS laser safety device provides protection zone near the die tip of the bender to protect fingers and arms of the operator in close to the upper mold die tip. It is the most effective solution so far to preserves the safety and productivity of the bender.

Press Brake Protection,Laser Guarding Device,Press Brake Guarding Systems,Press Brake Guarding

Jining KeLi Photoelectronic Industrial Co.,Ltd , https://www.sdkelien.com