• Adapter 模式(对象适配器模式)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Adapter
    {
        public interface INewLog
        {
            void WriteLog();
        }
        public interface IOldLog
        {
            void Write();
        }
        class FileLog:IOldLog
        {
            public void Write()
            {
                Console.WriteLine("Write log in file. ");
            }
        }
        class DataBaseLog:IOldLog
        {
            public void Write()
            {
                Console.WriteLine("Write log in database. ");
            }
        }
        class Adapter:INewLog         //须具备新接口,老功能
        {
            private IOldLog iol;
            private IOldLog iol2;
            public Adapter()
            {

            }
            public Adapter(IOldLog iol, IOldLog iol2)
            {
                this.iol = iol;
                this.iol2 = iol2;

            }
            public void WriteLog()
            {
                iol.Write();
                iol2.Write();
            }
        }
        class Program
        {
            public static void Main()
            {
    /*
                FileLog fl = new FileLog();
    //            Adapter a1 = new Adapter(fl);
                INewLog a1 = new Adapter(new FileLog());
                a1.WriteLog();

                DataBaseLog dl = new DataBaseLog();
    //            Adapter a2 = new Adapter(dl);
                INewLog a2 = new Adapter(new DataBaseLog());
                a2.WriteLog();*/
                FileLog fl = new FileLog();
                DataBaseLog dl = new DataBaseLog();
                INewLog inl = new Adapter(fl,dl);

                inl.WriteLog();
                Console.Read();
            }
        }

    }
    /*
    namespace Adapter
    {
        class OldClassA
        {
            public void XXOO()                  //A 老代码
            {
                Console.WriteLine("Hello, Everyone!");
            }
            public void OOXX()
            {
                Console.WriteLine("Byebye, Everyone!");
            }
        }
        abstract class NewClassB               //B 这样用
        {
            abstract public void SayHello();
            abstract public void SayBye();
        }
        class OldAdapterNew:NewClassB
        {
            private OldClassA oca = new OldClassA();
            public override void SayHello()
            {
                oca.XXOO();
            }
            public override void SayBye()
            {
                oca.OOXX();
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                OldAdapterNew oan = new OldAdapterNew();
                oan.SayHello();
                oan.SayBye();

                Console.Read();
            }
        }
    }
    */

  • 相关阅读:
    Spring Boot2 系列教程(二十)Spring Boot 整合JdbcTemplate 多数据源
    Spring Boot 如何给微信公众号返回消息
    Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate
    Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
    Spring Boot 开发微信公众号后台
    Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
    Spring Boot2 系列教程(十六)定时任务的两种实现方式
    Spring Boot2 系列教程(十五)定义系统启动任务的两种方式
    Spring Boot2 系列教程(十四)CORS 解决跨域问题
    JavaScript二维数组
  • 原文地址:https://www.cnblogs.com/MayGarden/p/1530872.html
Copyright © 2020-2023  润新知