In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to parse the adapter design pattern in C#. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Today, I saw a friend in the garden write an article about ASP.NET 's adapter design pattern. It is mentioned that the input voltage is converted into the appropriate voltage of the light bulb so that the light bulb can work properly. Coincidentally, I am also learning design patterns, in which I looked through the book "Design patterns and Zen" written by Qin Xiaobo, which mentioned that the definition of design patterns is:
Change the interface of one class into another interface expected by the client, so that the two classes that cannot work together because of the interface mismatch can work together.
Adapter mode is also known as transformer mode, also known as packaging mode.
We know that the voltage in China is 220V, while the voltage in Japan is 110V. Our light bulb made in China is generally rated at 220V. If you want this bulb to work properly in Japan, you must use an adapter to convert 110V voltage to 220V voltage. The example given by the author here does not implement the conversion of one interface or class to another that can be used, but only determines the input parameters. I do not comment on whether this is an adapter pattern. I will post my implemented adapter pattern below.
The defined API code is as follows:
Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld {/ China Electric Interface / public interface IChinaElectricity {/ Voltage / int Voltage () } / Nippon Electric Interface / public interface IJapanElectricity {/ Voltage / int Voltage () } / Light Interface / public interface IChinaLight {/ Light / string Light (int voltage);}}
The classes defined are as follows:
Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld {public class ChinaElectricity: IChinaElectricity {public int Voltage () {return 220;}} public class JapanElectricity: IJapanElectricity {public int Voltage () {return 110 }} public class ChinaLight: IChinaLight {/ public string Light (int voltage) {if (voltage = = 220) {return "I am glowing...." } else {return ("incorrect voltage does not work...");} / define a voltage adapter / public class ElectricityAdapter: IChinaElectricity {private int voltage = 0 Private IJapanElectricity iJElectricity = null; public ElectricityAdapter (IJapanElectricity _ baseElectricity) {iJElectricity = _ baseElectricity; voltage = this.iJElectricity.Voltage ();} public int Voltage () {return voltage + 110;}
Here we define a class of adapters to adapt the Japanese voltage to the Chinese voltage so that the light bulb can work properly. The code is as follows:
Static void Main (string [] args) {/ / initialize a Chinese electric lamp IChinaLight iCLight = new ChinaLight (); / / initialize China's electric IChinaElectricity iCElectricity = new ChinaElectricity (); / / initialize Nippon Electric IJapanElectricity iJElectricity = new JapanElectricity () / / use adapter IChinaElectricity iCAElectricity = new ElectricityAdapter (iJElectricity); / / correct voltage electric lamp Console.WriteLine (iCLight.Light (iCElectricity.Voltage (); / / use voltage adapter to adapt Console.WriteLine (iCLight.Light (iCAElectricity.Voltage () / / incorrect voltage, the lamp produces abnormal Console.WriteLine (iCLight.Light (iJElectricity.Voltage ();}
From the definition of the adapter pattern, we can see that the adapter pattern is nothing more than converting a source role that can no longer be modified or which is expensive to modify into a target role through an adaptive role. in this way, the whole business system can operate normally at the least cost.
Advantages of the adapter pattern:
The adapter pattern allows two unrelated classes to run together, as long as the adapter role can handle them.
Increases the transparency of the class.
The reuse of the class is improved.
Very flexible.
The usage scenario of the adapter pattern:
Just remember one thing in the adapter application scenario: when you have an incentive to modify an interface that is already in production, the adapter pattern may be the best pattern for you. For example, when the system is extended, only an existing or newly created class is needed, but this class does not conform to the interface of the system, so it is much less expensive to use the adapter pattern than to modify the existing class.
So much for sharing about how to parse the adapter design pattern in C#. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.