【. NET Foundation Supplements "adapter mode (Adapter) and polymorphic
Source: Internet
Author: User
<span id="Label3"></p><p><p>tonight, Let's talk about adapter mode and polymorphism, what is adapter mode? Let's first give you a question: assuming that two classes student and teacher inherit an abstract base class person, how to extend the multi-student and teacher functions without altering the three categories? Let's See.</p></p><p><p></p></p><p><p>Suppose we are now going to add a showtype () method to the student and teacher two classes without altering the three classes, what should we do? Think first, then look down.</p></p><p><p>Let's go back to the beginning of the question, what is adapter mode? Baidu Encyclopedia Ticket: point me into</p></p><p><p>In Layman's terms, adapters are generally used to extend new functionality when they should not change the original Structure. For example, Some functions are not enough to meet our current needs, but we can not modify the previous system, then this time we will be implemented through the adapter Mode. What do we look like when we add the adapter?</p></p><p><p></p></p><p><p>As we can see, we have defined the Ipersonadapterable interface and added a Personadapter implementation class to it, and we can add it in the Personadapter class if we want to use the new functionality in the Project. Paste the following example Code:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">using</span></span><span style="color: #000000;"><span style="color: #000000;">System;</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">namespace</span></span><span style="color: #000000;"><span style="color: #000000;">Big strong his brother blog Park</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;"></span> program</span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Main (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span><span style="color: #000000;"><span style="color: #000000;">[] Args)</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Student =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Student ();</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>Personadapterable =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">personadapterable (student);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">Personadapterable.showtype ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">Console.ReadLine ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;">person <span style="color: #000000;">{}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Teacher:person {}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Student:person {}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Interface</span></span><span style="color: #000000;"><span style="color: #000000;">ipersonadapterable</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">ShowType ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">personadapterable:ipersonadapterable</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;">person <span style="color: #000000;">_person;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">personadapterable (</span> person Person)</span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. _person =<span style="color: #000000;">person <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">ShowType ()</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_person<span style="color: #0000ff;"><span style="color: #0000ff;"></span> is</span><span style="color: #000000;"><span style="color: #000000;">Student)</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Console.WriteLine (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">I'm the Student class</span> .</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_person<span style="color: #0000ff;"><span style="color: #0000ff;"></span> is</span><span style="color: #000000;"><span style="color: #000000;">Teacher)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Console.WriteLine (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">I'm the Teacher class</span> .</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">corresponding operation can be implemented according to the specific Type.</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>Look at the above example, I believe you have a certain understanding of the adapter Mode. So what is polymorphism? Polymorphism can be simply defined as: presenting different forms or abilities. A chestnut, my girlfriend wants to eat noodles, I need to help her to buy, but she did not tell me what she wants to eat, I might buy her a big Wuhan hot-dried noodles or Lanzhou Ramen. In this case, my girlfriend does not care what I bought her, as long as the noodles she would like to Eat. But if I went to buy a steamed bun, I would have to kneel and rub the clothes board.</p></p><p><p>Perhaps the above example is not enough image, I quote the "you must know. NET" in the Book of a universal loader Example. First of all, the "universal loader" needs:</p></p><p><p>1: can open the common document class material, such as: txt/word/pdf, etc.</p></p><p><p>2: can open the common picture material, such as: jpg/gif/png etc.</p></p><p><p>3: can open common audio data, such as: mp3/mp4/avi, etc.</p></p><p><p>4: support simple and easy to use type expansion interface, easy to implement more file types of Loading.</p></p><p><p>After getting the demand, our first reaction is, OK, so simple function, minutes to achieve, so you have the following basic framework:</p></p><p><p> </p></p><p><p>We first defined a file type enumeration:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">enum</span> <span style="color: #000000;">FileType { doc, pdf, txt, ppt, jpg, gif, mp3, avi }</span></pre></pre><p><p>With the supported file types, The first thing we think about is creating a file class that represents the different file types:</p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;">public class Files { private FileType _filetype; Public FileType FileType = _filetype; Public Files (FileType FileType) { this._filetype = FileType; } }</pre></pre><p><p>We created a FileManager class to encapsulate different file opening methods:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">FileManager {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OpenDoc () {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #008080;"><span style="color: #008080;">5</span></span>Console.WriteLine (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OpenDoc</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OpenMp3 () {</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span>Console.WriteLine (<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OpenDoc</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">slightly</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>Here's How we call the Main method as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Main (<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span><span style="color: #000000;"><span style="color: #000000;">[] Args) {IList Files</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>List<files><span style="color: #000000;"><span style="color: #000000;">(); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>FileManager =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">FileManager (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">foreach</span></span>(Files Item<span style="color: #0000ff;"><span style="color: #0000ff;">inch</span></span><span style="color: #000000;"><span style="color: #000000;">files) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(item. FileType) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">FileType.doc:fileManager.OpenDoc (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">FILETYPE.MP3:FILEMANAGER.OPENMP3 (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">slightly lower n multiple branches</span></span><span style="color: #000000;"><span style="color: #000000;">}} Console.ReadLine (); }</span></span></pre><p><p>For the time being, this universal loader is complete, but if you suddenly want to support a format that has never been supported before, think about how our system is going to deal with it. first, we are going to increase the file type in the filetype enumeration, then add an open method to the FileManager and then add the branch to the place where it was called ... A couple may feel ok, think about it, if a few hundred ... Think about going crazy ... Can we simplify it?? Why not? This time, polymorphism is our lifeline, to see how we deal with Polymorphism.</p></p><p><p>First of all, whether it is doc or PDF or MP3, they belong to the file, we can first abstract them, and then within each class to open the file Operation.</p></p><p><p></p></p><p style="text-align: center;"><p style="text-align: center;">There is no abstraction before the</p></p><p style="text-align: center;"><p style="text-align: center;"></p></p><p style="text-align: center;"><p style="text-align: center;">Abstract after the</p></p><p style="text-align: left;"><p style="text-align: left;">As you can see, we separate the files by a file class, and then all of the file processing classes go to the files class, so if you want to add a new file type later, We just need to append a class to inherit file, and then after such a modification, pdffile. What is the change to the Main method call?</p></p><p style="text-align: left;"><p style="text-align: left;"> </p></p><pre><pre> <span style="color: #0000ff;">New</span> list<file><span style="color: #000000;">(); </span> <span style="color: #0000ff;">foreach</span> <span style="color: #0000ff;">inch</span> <span style="color: #000000;">files) { Item.open (); } Console.ReadLine ();</span></pre></pre><p><p>In the previous code we need to judge each item and then call the appropriate method, why not to judge after the modification? Because when you add objects to the files collection, the object value must be a subclass of the file class, and files inside each object is equivalent to a type of files, so we do not have to judge, the direct invocation of the Object's open method can be ...</p></p><p><p>【. NET Foundation Supplements "adapter mode (Adapter) and polymorphic</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service