Adapter mode in Design Mode
The design pattern is a kind of optimization of long-term code. I will explain my knowledge with my own opinions based on some examples. Speaking of the adapter mode,
The first important thing is the understanding of uml diagrams. Maybe you don't understand the code now, but understanding uml diagrams will be of great help for your future understanding.
Adapter modes include class adapters, object adapters, and default adapters.
Now let's take a look at the uml structure of the class adapter.
The above shows that the Adapter mode consists of three parts: Target, Adaptee, and Adapter. Here I write down the Code to give you an intuitive understanding.
Target
public interface Target { public void operation1();public void operation2();}
Adaptee
Public class Adaptee {public void operation1 () {System. out. println ("I am operating 1 ");}}
Adapter
public class Adapter extends Adaptee implements Target{public void operation2() {} }However, in addition to the code reuse, we can hardly see any other meaning. This is a bad feeling, so now we construct a classic scenario,
China's electrical appliances work at a voltage of 110 v, while the United States's electrical appliances work at a voltage of v. Now we have bought electrical appliances in the United States for use in China. What should we do? We need a transformer,
Therefore, the adapter mode has another name, transformer mode, or transformer mode.
How can the code be expressed? The following example is provided.
China Voltage
Public class ChinaStandard {public void useCnSta () {System. out. println ("220220220220220220 ");}}
China's U. S. standard voltage can be used
public interface AmericaStandard { public void useAmSta();}
US Voltage
Public class AmericaStandard implements AmericaEle {public void useAmSta () {System. out. println ("we can only use 110 v ");}}
Transformer
Public class Transform extends ChinaStandard implements AmericaEle {// use the Chinese 220v voltage public void useAmSta () {this. useCnSta ();}}We can use V of Chinese voltage through transformer. Haha, it's not hard.
From the above scenario, it is not hard to see that our adapter mode is code reuse (not nonsense). But what are the prerequisites for code reuse?
The premise of code reuse is not to break the inheritance structure of the original code, and not to break the internal structure of the code, that is, what we call
For a certain purpose, temporarily reuse a class method. Since it is temporary, we cannot destroy the original part. This is hard to understand. I will use the jdk io stream for everyone.
Detailed description of why.
The code above can be used very well for a single class, but if our source has many derived classes, but our inheritance can only inherit one, we can't,
So our Object Adapter is blank. The following is the uml of our Object Adapter:
So in this way, we optimized the original transformer and it became our Object Adapter transformer.
public class Transform implements AmericaEle{ private ChinaStandard chinaStandard=null; public Transform(ChinaStandard chinaStandard){ this.chinaStandard=chinaStandard; } public void useAmSta() { chinaStandard.useCnSta(); }}
From this code, we can see that we only need to define a parent class. By passing in the constructor, we can adapt the source of multiple inheritance relationships.
The situation we encounter will not be as simple as above, but we can see from the example of transformer that our adapter is born for compatibility, so most of
When the system is upgraded, or two classes of functions must be used at the same time. Both classes have their own systems. The damage will affect a series of problems, so much,
The meaning is not changed, so let's look at the complicated situation.
Let's take the java io structure as an example. Architecture diagram of java io:
It seems complicated and a headache for everyone, so let's say a little bit:
Our top-level InputStream is an abstract class with its own stream function. However, our InputStream now needs a file processing function,
However, our File processing and streaming functions are not compatible. Maybe you will say that we can add File processing directly in InputStream?
However, if you rashly add some methods, we will increase the complexity of InputStream when extending other functions for processing. What we need is only part of the File function,
If it is inherited, it will not only undermine the original system, but also add unused features. In this case, we use the adapter mode,
The situation is like the figure below
Code Section:
publicclass FileInputStream extends InputStream{ /* File Descriptor - handle to the open file */ private FileDescriptor fd; private FileChannel channel = null; public FileInputStream(FileDescriptor fdObj) { SecurityManager security = System.getSecurityManager(); if (fdObj == null) { throw new NullPointerException(); } if (security != null) { security.checkRead(fdObj); } fd = fdObj; }}
The specific method is to use an Object Adapter, just as we have StringBufferInputStream, but now we need a file stream, which must be incompatible with StringBufferInputStream, just like the Chinese and American voltages, although the form is not exactly the same, the ideology is the same. StringBufferInputStream, ByteArrayInputStream, and InputStreamReader are both adapters, because they do not know the existing stream.
Here we will not elaborate on it, so let's take a look at the default adapter, its uml:
WindowAdapter is better used by default adapters in java. How can we understand it?
Take a look at the code structure:
public interface WindowListener extends EventListener { public void windowOpened(WindowEvent e); public void windowClosing(WindowEvent e); public void windowClosed(WindowEvent e); public void windowIconified(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowActivated(WindowEvent e); public void windowDeactivated(WindowEvent e);}If we implement WindowListener, we must write all the methods, but now we have WindowAdapter, which is an abstract class and code structure.
public abstract class WindowAdapter implements WindowListener{ public void windowOpened(WindowEvent e); public void windowClosing(WindowEvent e); public void windowClosed(WindowEvent e); public void windowIconified(WindowEvent e); public void windowDeiconified(WindowEvent e); public void windowActivated(WindowEvent e); public void windowDeactivated(WindowEvent e);}
I only wrote some, but if we implement WindowAdapter, we can only rewrite the methods we need without listing all the methods. Of course, there is another benefit. Abstract classes can be written
Default implementation, but our interface does not work, and sometimes the default behavior is very useful.
This article is almost over and has been prepared for a long time. However, I am trying to discuss problems with you in a deeper way. I believe I will become more and more proficient.
This method. Here, I hope that you can tell me my mistakes. The Code thought is shared by me. To avoid mistakes, please tell me if you have any mistakes. Thank you, both the author and everyone are struggling on this road.