1,
Adapter Mode .
"Adapter", as its name implies, is to convert an interface of a class into another interface, so that those classes that originally cannot work together due to interface incompatibility work together. In software development, the system data and behavior are correct, but the interface does not match, we should consider the adapter. For example, we have learned the Chinese version of the Analects of Confucius (several famous sections in the Analects of Confucius) in high school. If the English teacher wants us to learn the Analects of Confucius in English, when I used to study the Chinese version, I took a fancy to the document. Now I want to learn the English version. Of course I want to see the English document. (Note that the English document here is actually true. Exist The constant is to continue listening to what the child once said. Okay, code is cheap. Let Code Speaker: Code
Using System;
Class Adapterpattern
{
/// <Summary>
/// Chinese Analects of Confucius
/// </Summary>
Class Chineseconfuciananalects
{
// The most famous sentence in the Analects of Confucius
Public Virtual Void Confuciussaid ()
{
Console. writeline ( " It's a pleasure to have friends from afar (Shandong cavity) " );
}
}
/// <Summary>
/// Classes to be adapted: English Analects of Confucius
/// </Summary>
Class Englishconfuciananalects
{
Public Void Englishconfuciussaid ()
{
Console. writeline ( " Is it not delightful to have friends coming from distant quarters? " );
}
}
Class Adapter: chineseconfuciananalects
{
Private Englishconfuciananalects MC = New Englishconfuciananalects ();
Public Override Void Confuciussaid ()
{
MC. englishconfuciussaid (); // Adapt the famous sub-statement to English
}
}
/// <Summary>
/// Client call
/// </Summary>
Static Void Main ( String [] ARGs)
{
Chineseconfucianalects CCA;
Console. writeline ( " Common call before adaptation " );
CCA = New Chineseconfuciananalects ();
CCA. confuciussaid ();
Console. writeline ( " ------------------------------------------------------------ " );
console. writeline ( " special calls using the adapter, " );
CCA = New adapter ();
CCA. confuciussaid (); // for the client, the call looks like the Chinese Analects of Confucius function, which has actually been converted into the English Analects of Confucius
console. read ();
}< BR >}
The use of the adapter mode is usually in the late stage of software development or maintenance, because no development team is willing to design similar functional interfaces at the early stage of development, usually, we will first consider restructuring the unified interface. Therefore, this mode should be avoided at the initial stage of development.
2,Proxy Mode.
A proxy represents a real object ." Proxy mode "provides a proxy for other objects to control access to this object. The proxy mode introduces a certain degree of indirect property when accessing objects, because this indirect property can be used for multiple purposes.
For example, we have learned the Chinese version of the Analects of Confucius (several famous sections in the Analects of Confucius) in high school. Now, when we are in college, we have a strong desire for knowledge, I really want to learn the English version of the Analects of Confucius. I used to take a fancy to the Chinese version. Now I want to learn the English version. Of course I want to read the English version, however, the Chinese version of the Analects of Confucius (or unauthoritative, we can also translate it ourselves) without passing through quality ). At this time, foreign Chinese experts translated the Analects of Confucius, but the copyright control was very strict. We could not buy it directly. At this time, a press was authorized to publish this book in China, in this way, we can see the Analects of Confucius translated by foreigners. Now let's take a look at what the English "son once said" is. Code:Code
UsingSystem;
Class Proxypattern
{
/// <Summary>
/// Defines the common interfaces of the English Analects class englishconfuciananalects and proxy class proxy,
/// In this way, the proxy class proxy can be used in any place where the English version of the Analects class is used.
/// </Summary>
Abstract Class Confuciananalects
{
Public Abstract Void Confuciussaid ();
}
/// <Summary>
/// Definition of the real entity represented by proxy
/// </Summary>
Class Englishconfucianalects: confucianalects
{
Public Override Void Confuciussaid ()
{
Console. writeline ( " Is it not delightful to have friends coming from distant quarters? " );
}
}
/// <Summary>
/// The proxy class that saves a reference so that the proxy can access the entity (currently the englishconfucianalects class ),
/// It also provides the same interface as the confucianalects interface, so that the proxy class can replace the entity class.
/// </Summary>
Class Proxy: confuciananalects
{
Private Englishconfucianalects ECA;
Public Override Void Confuciussaid ()
{
If (ECA = Null )
{
ECA = New Englishconfuciananalects ();
}
ECA. confuciussaid ();
}
}
/// <Summary>
/// Client call
/// </Summary>
Static Void Main ( String [] ARGs)
{
Console. writeline ( " It's a pleasure to have friends from afar (Shandong cavity) " );
Console. writeline ( " Let's see how it works. " );
Proxy= NewProxy ();
Proxy. confuciussaid ();//Analects of English
Console. Read ();
}
}
The proxy in the code above and the publishing house in this example (in reality) are still very different in usage. Here is just an example. If not, please pay attention to it.