【text】
We know that the most important and most difficult UI control in Android is the ListView list control. If you want to use it flexibly, you must use the adapter adapter. Therefore, I think it is still necessary to learn the adapter mode in Java (regardless of whether it can be used in the future). After all, the Java language is a very important foundation for Android development.
To fully understand the adapter mode, there is a lot of knowledge to learn, for example: the adapter mode has two different forms: the adapter mode of class and the adapter mode of object. But as a beginner, I will simply learn the introductory knowledge of orchestration mode, and I will continue to improve it in the future. I hope that the children's shoes who are struggling on the road to coding don't complain →_→
1. Adapter introduction
•Convert an interface of one class to another interface that the customer wants. Adapter mode allows classes that were originally unable to work together due to incompatibility of the interfaces to work together.
•Adapter mode is very commonly used in modern Java frameworks. This pattern is suitable for the following scenarios: you want to use an existing class, but the class does not meet the interface requirements; or you need to create a reusable class that adapts to other classes that do not provide a suitable interface.
2. Examples of apples and oranges
The idea of an adapter can be illustrated by the following simple example. This example is to make an orange "adapt" into an apple. As shown in the figure below:
As you can see in the lower half of the above picture, the adapter contains an orange instance and inherits the apple class. The orange object is placed in the adapter, so the orange acts like an apple. The corresponding logic diagram is as follows:
3. Examples of socket box plugs
In the figure above, we can successfully connect the plug on the left socket through the middle adapter.
4. Code implementation of plug adapter
/** Adapter mode (Adapter): converts an interface of one class into another interface that the customer wants. Adapter mode allows classes that were originally unable to work together due to incompatibility of the interfaces to work together. */class AdapterDemo{ public static void main(String[] args){ //Power A starts working PowerA powerA = new PowerAImpl(); start(powerA); PowerB powerB = new PowerBImpl(); PowerAAdapter pa = new PowerAAdapter(powerB); start(pa); } //Definition method: PowerA works public static void start(PowerA powerA){ System.out.println("....some duplicate code......"); powerA.insert(); System.out.println("...some duplicate code.../n"); } /** public static void start(PowerB powerB){ System.out.println("...some duplicate code......"); powerB.connect(); System.out.println("...some duplicate code......"); } */}//Define the adapter class class PowerAAdapter implements PowerA{ private PowerB powerB;//Interface to adapt public PowerAAdapter(PowerB powerB){ this.powerB = powerB; } //Implementing the interface PowerA, you must implement the methods in PowerA public void insert(){ powerB.connect(); }}/** Power A interface*/interface PowerA{ public void insert();}class PowerAImpl implements PowerA{ public void insert(){ System.out.println("Power A interface is inserted, start working"); }}/** Power B interface*/interface PowerB{ public void connect();}class PowerBImpl implements PowerB{ public void connect(){ System.out.println("Power B interface is connected, start working"); }} In this example, we want PowerB to call the code in the Start() method in PowerA; of course, we don't want to repeat the commented 23 or 25 lines of code. At this time, you can use the adapter mode.
The above code explanation:
Line 30: Start defining the adapter, which is also the beginning of the core code;
Lines 33 and 34: Pass PowerB in through the construction method;
Line 37 code: Since it is to implement the interface PowerA, it is necessary to implement the method insert() in PowerA;
Line 38 code: We call PowerB's connect() method in PowerA's insert() method;
Immediately afterwards, the 10, 11, and 12 lines of code means: when new a PowerB, we pass it into the adapter PowerAAdapter, start the adapter, and then PowerB will execute the code on lines 16, 24, and 18.
Note: The order of 16, 24, and 18 is not wrong, because in the adapter, we have replaced 24 lines of code with line 17.
The operation effect is as follows:
Similarly, if I also want to use PowerA as PowerB, I can define another adapter PowerBAdapter to implement a bidirectional adapter.
5. Summary
The commented code in lines 23 and 25 above indicates that it is a lot of duplicate code and does not conform to the object-oriented thinking. We now envision an example of our project being online and customers are using it, but then some new demand has been added. There is an OO principle for object-oriented objects: close the modification (after going online, try not to modify the code, otherwise a chain reaction may occur, resulting in problems with other codes calling this method), and open to extensions (new methods defined by yourself, which others have not called yet, so of course we can modify them). At this point, we can reduce these duplicate code through adapters.
6. OO design principles
• Interface-oriented programming (abstract-oriented programming)
• Package Changes
• Use more combinations, use less inheritance
•Close to modifications to open to extensions
Personally, these design principles need to be continuously deepened in practice, so I will not describe them here too much~
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.