The adapter, as its name implies, is used to adapt to interfaces. Let's take a notebook that we usually use. We all know that when using our notebook, we connect to the power source through an adapter. This is because the voltage required by the notebook is different from the voltage provided by the socket. We need to adapt the voltage provided by the socket to the voltage acceptable to the notebook.
A uml diagram is required:
The adapters are divided into object adapters and class adapters. Because class adapters require multiple inheritance, they are not compatible with C #, so we will study object adapters.
In this article, we will adapt the wild monster to a hero. If there is a hero interface, what? Of course it is a talk. The interface is as follows:
/// <Summary>
/// Adaptation Interface
/// </Summary>
Public Interface Ihero
{
Void Talk ();
}
Dangdang ~, Our hero is a "regional Lord". He must have inherited the base class hero and implemented the ihero interface.
/// <Summary>
/// Hero
/// </Summary>
Public Class Hero
{
Protected String _ Name;
Public String Name
{
Get { Return _ Name ;}
Set {_ Name = Value ;}
}
}
/// <Summary>
/// Regional lord
/// </Summary>
Public Class DK: Hero, ihero
{
Public DK ()
{
_ Name = " DK " ;
}
Public Void Talk ()
{
Landpyform. Form. outputresult ( " For the Lich King " );
}
}
What are our lovely DK talking about :)
He shouted the familiar line "for the Lich King" (for the Lich King). He was so touched that his snot came out. Speaking of snot, it reminds me of the best hero I have ever used-Invincible.
Next it's our turn to play hero 2, that is, the legendary "man". He is a strange guy and of course he will not speak, but he will only roar like a beast, hmmm ~~~~~~ :
/// <Summary>
/// Nonov
/// </Summary>
Public Interface Ibotwildnpc
{
Void Howling ();
}
/// <Summary>
/// Personnel
/// </Summary>
Public Class Centaur: ibotwildnpc
{
Public Void Howling ()
{
Landpyform. Form. outputresult ( " Mmmm. " );
}
}
The introduction of the two leading actors has been completed. Now the director is out of stock. Please ask Mr. adapter ...........
Originally, I wanted to adapt a man to a woman, but it was a little against the Dota question. I was afraid that my brother could not understand Thai customs, so I adapted the wild monster to a hero. The adapter is easy to implement. It inherits the functional interface to be adapted, passes in the object to be adapted during the constructor, and implements the passed object as needed. The Code is as follows:
Public Class Heroadapter: ihero
{
Private Ibotwildnpc botwildnpc;
Public Heroadapter (ibotwildnpc botwildnpc)
{
This . Botwildnpc = Botwildnpc;
}
# RegionIheroadapter Member
Public Void Talk ()
{
This . Botwildnpc. Howling ();
}
# Endregion
}
In this way, the wild monsters after adaptation will be able to speak. You can use the following Code Test:
Dotapatternlibrary. Adapter. ihero hero = new dotapatternlibrary. Adapter. dk ();
Hero. Talk ();
Dotapatternlibrary. Adapter. ibotwildnpc botwildnpc = new dotapatternlibrary. Adapter. Centaur ();
Botwildnpc. Howling ();
Hero = new dotapatternlibrary. Adapter. heroadapter (botwildnpc );
Hero. Talk ();
We can see that after the new dotapatternlibrary. Adapter. heroadapter (botwildnpc) is adapted, the new object has the talk method. No ~~~~
Output the following results:
For the Lich King
Mmmm ......
Mmmm ......
The complete code is as follows: Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
UsingDotacommon;
Namespace Dotapatternlibrary. Adapter
{
/// <Summary>
/// Hero
/// </Summary>
Public Class Hero
{
Protected String _ Name;
Public StringName
{
Get{Return_ Name ;}
Set{_ Name=Value ;}
}
}
///
/// adaptation interface
///
Public interface ihero
{< br> void talk ();
}
/// <Summary>
/// Regional lord
/// </Summary>
Public Class DK: Hero, ihero
{
Public DK ()
{
_ Name = " DK " ;
}
Public void talk ()
{
landpyform. form. outputresult ( " for the Lich King " );
}< BR >}
///
/// noisy
///
Public interface ibotwildnpc
{< br> void howling ();
}
/// <Summary>
/// Personnel
/// </Summary>
Public Class Centaur: ibotwildnpc
{
Public Void Howling ()
{
Landpyform. Form. outputresult ( " Mmmm. " );
}
}
Public class heroadapter: ihero
{< br> private ibotwildnpc botwildnpc;
Public heroadapter (ibotwildnpc botwildnpc)
{< br> This . botwildnpc = botwildnpc;
}
# RegionIheroadapter Member
Public VoidTalk ()
{
This. Botwildnpc. Howling ();
}
# Endregion
}
}