• 适配器模式


    适配器模式

                  适配器模式 关键就在在在于一个转换,举例;中国的用电都在220v ,日本,美国都是110v 的,中国的家用电器到国外如何使用呢?国外的电器到中国又如何使用呢,这里就需要一个转换器-也叫适配器,看实例代码吧。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Patten001
    {
       public interface ITarget
        {
            void Request();
        }
    
        public class Target : ITarget
        {
            public void Request()
            {
    
                Console.WriteLine("客户想要的方法");
            }
        }
    
        public class Adaptee
        {
            //没有request 这方法;
            public void doOther()
            {
                Console.WriteLine("do...other..");
            }
    
            public void SpecificRequst()
            {
                Console.WriteLine("called specificRequsted()");
            }
        }
    
        public class Adapter : Adaptee, ITarget
        {
            public void Request()
            {
                this.SpecificRequst();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                ITarget t = new Adapter();
                t.Request();
    
            }
    
            public void Info()
            {
                //将一个类的借口转换成客户端希望的另一个借口;
                //模式使得原本由于接口不兼容而不能在一起工作的类可以再一起工作的呀;
    
                //使用场景;已经存在的接口不能符合我们的需求;
                //创建一个复用的类,使得该类可以与其他不相关类或不可预见的类;
                //即那些接口可能不一定兼容;
    
    
            }
        }
    
       
    }
  • 相关阅读:
    Mac下安装brew
    Mac下安装node.js
    Mac下mysql服务端密码重置及环境配置
    Mac配置jdk以及maven
    Mac下卸载jdk
    34个漂亮的应用程序后台管理界面(系列一)
    ViewState
    get和post
    无刷新 分页评论
    isPostBack原理
  • 原文地址:https://www.cnblogs.com/mc67/p/4826175.html
Copyright © 2020-2023  润新知