• 手机站
  • 收藏
聚培教育网合作机构 > 福州达内教育
福州达内教育
400-998-6158
福州达内教育以打造符合企业需求的实战型人才为目标,结合多种就业渠道,将学员就业视为己任。
福州达内教育

C#实例解析适配器设计模式

软件测试学习网

更新时间:2021-10-14 浏览:124
核心提示:今日见到一个园区里的盆友写了一篇ASP.NET的电源适配器策略模式的文章内容。在其中提及了把键入的工作电压转化成电灯泡合适的工作电压,那样才可以使电灯泡一切正常工作

今日见到一个园区里的盆友写了一篇ASP.NET的电源适配器策略模式的文章内容。在其中提及了把键入的工作电压转化成电灯泡合适的工作电压,那样才可以使电灯泡一切正常工作。恰巧,因为我在学习策略模式,在其中翻阅了一下秦小波变换写的《设计模式与禅》这本书,在其中提及了策略模式的界定为:

将一个类的插口变为手机客户端所希望的另一种插口,进而使本来因插口不配对而没法在一起工作中的2个类可以一起工作中。

适配器模式又叫变电器方式,也叫包裝方式。

这儿创作者举得事例并沒有完成把一个插口或类变换到此外一个能够应用的类,只是是把键入主要参数干了分辨,这是否适配器模式我缄默不语,下边贴出来我完成的适配器模式。

我们知道,我国的工作电压是220V,而日本的工作电压为110V,大家我国生产制造的灯泡一般额定电流为220V,假如要想这一电灯泡在日本可以一切正常工作中就务必应用一个电源适配器,把110V工作电压转化成220V工作电压。

界定插口编码以下:

1.using System;

2.using System.Collections.Generic;

3.using System.Linq;

4.using System.Text;

5.

6.namespace HelloWorld

7.{

8. ///


9. /// *电插口

10. ///

11. public interface IChinaElectricity

12. {

13. ///


14. /// 工作电压

15. ///

16. ///

17. int Voltage();

18. }

19. ///


20. /// 日本电插口

21. ///

22. public interface IJapanElectricity

23. {

24. ///


25. /// 工作电压

26. ///

27. ///

28. int Voltage();

29. }

30. ///


31. /// 灯插口

32. ///

33. public interface IChinaLight

34. {

35. ///


36. /// 发亮

37. ///

38. ///

39. string Light(int voltage);

40. }

41.}

界定的类以下:

1.using System;

2.using System.Collections.Generic;

3.using System.Linq;

4.using System.Text;

5.

6.namespace HelloWorld

7.{

8. public class ChinaElectricity : IChinaElectricity

9. {

10. public int Voltage()

11. {

12. return 220;

13. }

14. }

15.

16. public class JapanElectricity : IJapanElectricity

17. {

18. public int Voltage()

19. {

20. return 110;

21. }

22. }

23.

24. public class ChinaLight : IChinaLight

25. {

26. ///


27. /// 发亮

28. ///

29. ///

30. public string Light(int voltage)

31. {

32. if (voltage == 220)

33. {

34. return "我发亮啦....";

35. }

36. else

37. {

38. return ("工作电压有误,没法一切正常工作中...");

39. }

40. }

41. }

42. ///


43. /// 界定一个工作电压电源适配器

44. ///

45. public class ElectricityAdapter : IChinaElectricity

46. {

47. private int voltage = 0;

48. private IJapanElectricity iJElectricity = null;

49.

50. public ElectricityAdapter(IJapanElectricity _baseElectricity)

51. {

52. iJElectricity = _baseElectricity;

53. voltage = this.iJElectricity.Voltage();

54. }

55. public int Voltage()

56. {

57. return voltage 110;

58. }

59. }

60.}

更多>同类资讯
更多>相关课程
顶部