[Design Mode]-Adapter Mode

1. Introduction to Adapter Mode

1. What is the adapter mode

**Adapter patterns** are also called wrapper patterns. They exist to bridge the gap between two incompatible interfaces. It belongs to the structural design mode and is dedicated to solving the incompatibility problem of existing objects in the new environment.

2. Adapter business scenario

The most suitable real-life scenario for the adapter mode should be the converter. There are many types of converters. We can search for headset converters on Goudong:
Insert picture description here
because many mobile phone manufacturers canceled the 3.5mm format in order to be thinner. The headphone jack (that is, our common round head jack), they solve the problem of the headphone plug based on the combination of the headphone jack and the charging jack. So here comes the problem. Many earphone enthusiasts may have spent a lot of money to buy a HiFi earphone. If you have to replace the earphone with the corresponding plug format every time you change your phone, the cost is undoubtedly too high, then we need to Design a plug format converter.

Second, the realization of the adapter mode

There are many ways to implement the adapter mode. Today I will mainly talk about the three more common adapter modes: class adapter, object adapter and interface adapter.

1- type adapter

1. Implementation of the class adapter

First of all, we use the simple implementation of the class adapter pattern.

(1) Create a headphone interface

public interface HeadSet {
    
    
	// 播放音乐
	void music();
	// 获取种类
	String type();
}

(2) Create a normal 3.5mm headset

public class SimpleHeadset implements HeadSet {
    
    

	protected String type = "3.5mm接口";

	public void music() {
    
    
		System.out.println("播放音乐");
	}

	@Override
	public String type() {
    
    
		return type;
	}

}

(3) Create converter interface

public interface TypecHeadSetAdapter {
    
    

	void transformation();
}

(4) Create an interface conversion class

public class SimpleToTypecAdapter extends SimpleHeadset implements TypecHeadSetAdapter {
    
    
	@Override
	public void transformation() {
    
    
		super.type = "typeC";
	}
	public SimpleToTypecAdapter() {
    
    
		transformation();
	}
}

(5) Create a mobile phone object

public class Phone {
    
    

	private HeadSet headSet;

	public void check() {
    
    

		if ("typeC".equals(headSet.type())) {
    
    
			headSet.music();
		} else {
    
    
			System.out.println("接口格式不符,无法播放音乐");
		}
	}

	public Phone(HeadSet headSet) {
    
    
		super();
		this.headSet = headSet;
	}

}

(6) Create a test class

public class Test {
    
    
	public static void main(String[] args) {
    
    

		Phone phone = new Phone(new SimpleToTypecAdapter());
		phone.check();
	}

}

Test Results:
Insert picture description here

2. Design Ideas of Class Adapter

Look at the picture first:
Insert picture description here
Class adapters are mainly implemented by using inherited features. The main ideas are:

  1. Create product interface
  2. Realize product interface through the class to be adapted
  3. Since the new service cannot use the existing class to be adapted, the class to be adapted needs to be converted
  4. Create adapter interface
  5. Create an adapter subclass, inherit and adapt the class, and implement the adapter interface
  6. The business caller can create an adapter subclass

2-Object adapter

1. Implementation of Object Adapter

Object adapter we only need to change the adapter object

public class SimpleToTypeCAdapter implements  HeadSet,TypecHeadSetAdapter {
    
    

	private SimpleHeadset simpleHeadset;

	@Override
	public void transformation() {
    
    
		simpleHeadset.type = "typeC";
	}

	public SimpleToTypeCAdapter(SimpleHeadset simpleHeadset) {
    
    
		super();
		this.simpleHeadset = simpleHeadset;
		transformation();
	}

	@Override
	public void music() {
    
    
		simpleHeadset.music();
	}

	@Override
	public String type() {
    
    
		return simpleHeadset.type();
	}

}

Test Results:

public class Test {
    
    

	public static void main(String[] args) {
    
    
		HeadSet simpleToTypeCAdapter = new SimpleToTypeCAdapter(new SimpleHeadset());
		Phone phone = new Phone(simpleToTypeCAdapter);
		phone.check();
	}
}

Insert picture description here

1. Design Ideas of Object Adapter

As shown in the figure:
Insert picture description here
there should be a smart guy who discovered the difference between a class adapter and an object adapter is that the class adapter is adapted through object inheritance, while the object adapter is adapted through aggregation.

2-Interface adapter

The application scenario of the interface adapter is slightly different from the above two adapters. The interface adapter is usually used when we want to use one or a few methods in a certain complex interface. For example, we create an interface with ten methods:

public interface SourceInterface {
    
    

	void method01();

	void method02();

	void method03();

	void method04();

	void method05();

	void method06();

	void method07();

	void method08();

	void method09();

	void method10();
}

If we only want to use the method01 in the interface at this time, what should we do? Very simple, first create a subclass that inherits from the target interface and implement all methods empty by default :

public class Adapter implements SourceInterface {
    
    

	@Override
	public void method01() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method02() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method03() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method04() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method05() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method06() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method07() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method08() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method09() {
    
    
		// TODO Auto-generated method stub

	}

	@Override
	public void method10() {
    
    
		// TODO Auto-generated method stub

	}

}

Then we are creating a business caller and rewriting the method that needs to be adapted through the internal class

public class Test {
    
    

	public static void main(String[] args) {
    
    
		Adapter adapter = new Adapter() {
    
    

			@Override
			public void method01() {
    
    
				System.out.println("适配了方法01");
			}
		};
		
		adapter.method01();
	}

}

Test Results:
Insert picture description here

The realization principle of interface adapter

Insert picture description here
The implementation of the interface adapter is very simple:

  1. We inherit the target interface through an adaptation class
  2. The adaptation class implements all interface methods empty by default
  3. The business caller creates an adaptation class and calls the desired method through the internal class

Three, the characteristics of the adapter mode

1. Usage scenarios of the adapter

  1. The system needs to use existing classes, but the properties of existing classes are different from expectations
  2. Through interface conversion, insert a class into another interface system
  3. Want to establish a connection between two classes that might work together

2. Features of Adapter Mode

advantage:

  1. You can make any two unrelated classes run together
  2. Improved reusability of classes
  3. Increased class transparency
  4. High flexibility

Disadvantages:

  1. Excessive use of adapters will fragment the system and make it difficult to organize and control.
  2. Java single inheritance limits the flexibility of adaptation.

3. Matters needing attention

Because the adaptation mode will make users have the illusion of calling, for example, they clearly see that the A interface is called, but its internal is adapted to the implementation of the B interface. This leads to too many application adaptation modes, which undoubtedly affect the semantics and abstraction of the system. It is a disaster. Therefore, if it is not necessary, it is not recommended to use the adapter mode, but it is recommended to directly reconstruct the existing system to reduce the probability of potential problems.

Okay, this concludes today's content. If you have any questions, you can privately message me or leave a message in the comment area, and I will answer you as soon as possible. Friends who feel rewarded, please remember one-click three consecutive times, pay attention to bloggers, don’t get lost, and refuse to be a prostitute~

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/112762231