Speaking of the adapter mode, I believe that many students have some understanding of the adapter mode, when it comes to ListView, RecyclerView is the use of the adapter mode, this is from the use of the aspect, if from the theoretical point of view, what is the adapter mode, in what scenario to use the adapter mode, Maybe a lot of you don’t know.
When we study theory, it’s more about abstracting practical applications. What’s the purpose of doing that? It is in some appropriate scenarios, we can choose the right mode to help us solve the problem.
Now let’s combine theory with practice to learn.
Usage scenarios for the adapter pattern
1, the user using the existing class interface can not meet the requirements, in this case, we need to expose a new interface to the user, can use the adapter pattern.
For example, let’s say there’s a class PersonManager that provides a persons() interface that internally outputs information about all persons. However, the user needs an interface to count the number of people, and we cannot modify the PersonManager. We can only encapsulate a class PersonMangerAdapter on this basis, so that we can provide the user with the interface it needs.
2. A unified output interface is required, but the input type is unknown.
Such as: The Adapter of the ListView, the Item View returned by the getView method is a View, is a unified output interface, but the user input, that is, the type of construction is variable, The ListView method returns a View from the Adapter getView method. I don’t care what the View is. Andorid architects hand over these changes to the user to handle, using getCount, getItemt and other methods to cope with the changes, flexibly applying the adaptor pattern to achieve unlimited adaptation and embrace the purpose of change. Similar to: Foxconn factories and manpower centers.
The above learning, I believe to give you a different perspective.
Now we continue to learn the classification of adapter patterns, many students should not be clear, so let’s learn.
Classification of adapter patterns
Class adapter pattern.
Object adapter pattern.
So what’s the difference between them?
Class adapter pattern (parent-child relationship: inheritance required)
The UML class diagram for the class adapter pattern looks like this:
The specific implementation steps are as follows:
Step 1: Define an interface Target.
package com.example.createproject;
public interface Target {
void operation1();
void operation2();
}
Copy the code
Step 2: Define a class that the API does not conform to for users.
package com.example.createproject; import android.util.Log; public class Adaptee { private static final String TAG = Adaptee.class.getSimpleName(); Public void operation3(){log.e (TAG,"operation3 executed "); }}Copy the code
Step 3: Define an adapter class that inherits classes that the API does not conform to, while implementing methods that need to be extended.
package com.example.createproject; import android.util.Log; public class Adapter extends Adaptee implements Target{ private static final String TAG = Adapter.class.getSimpleName(); @Override public void operation1() { } @Override public void operation2() { operation3(); The e (TAG, "processing"); }}Copy the code
Step 4: Let’s see how the user call works.
package com.example.createproject import android.support.v7.app.AppCompatActivity import android.os.Bundle class Test8Activity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.oncreate (savedInstanceState) setContentView(r.layout.activity_main4) // There is no way for the user to call the operation2() method through Adaptee We can construct an Adapter class to implement val Adapter = Adapter() adapter.operation2()}}Copy the code
Let’s continue learning about the object adapter pattern
Object adapter pattern (sibling: proxy required)
The UML class diagram is as follows:
The concrete implementation is as follows:
Step 1: Define an interface.
package com.example.createproject;
public interface Target {
void operation1();
void operation2();
}
Copy the code
Step 2: Define a class that the API does not conform to for users.
package com.example.createproject; import android.util.Log; public class Adaptee { private static final String TAG = Adaptee.class.getSimpleName(); Public void operation3(){log.e (TAG,"operation3 executed "); }}Copy the code
Step 3: Define the object adapter.
package com.example.createproject; import android.util.Log; public class Adapter implements Target{ Adaptee adaptee; private static final String TAG = Adapter.class.getSimpleName(); public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void operation1() { } @Override public void operation2() { adaptee.operation3(); The e (TAG, "processing"); }}Copy the code
Step 4: How to call
package com.example.createproject import android.support.v7.app.AppCompatActivity import android.os.Bundle class Test8Activity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.oncreate (savedInstanceState) setContentView(r.layout.activity_main4) // There is no way for the user to call the operation2() method through Adaptee Val adaptee = adaptee () val Adapter = Adapter(adaptee) adapt.operation2 ()}}Copy the code
Using the example above, let’s sum up the differences between them