分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Participants

The classes and/or objects participating in this pattern are:

  • Target (ChemicalCompound)

    • Defines the domain-specific interface that Client uses.
  • Adapter (Compound)
    • Adapts the interface Adaptee to the Target interface.
  • Adaptee (ChemicalDatabank)
    • Defines an existing interface that needs adapting.
  • Client (AdapterApp)
    • Collaborates with objects conforming to the Target interface.

Sample Code in C#


This structural code demonstrates the Adapter pattern which maps the interface of one class onto another so that they can work together. These incompatible classes may come from different libraries or frameworks.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Adapter Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Adapter Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Create adapter and place a request            Target target = new Adapter();            target.Request();        }        #endregion    }    /// <summary>    /// The 'Target' class    /// </summary>    internal class Target    {        #region Public Methods and Operators        /// <summary>        /// The request.        /// </summary>        public virtual void Request()        {            Console.WriteLine("Called Target Request()");        }        #endregion    }    /// <summary>    /// The 'Adapter' class    /// </summary>    internal class Adapter : Target    {        #region Fields        /// <summary>        /// The adaptee.        /// </summary>        private Adaptee adaptee = new Adaptee();        #endregion        #region Public Methods and Operators        /// <summary>        /// The request.        /// </summary>        public override void Request()        {            // Possibly do some other work and then call SpecificRequest            this.adaptee.SpecificRequest();        }        #endregion    }    /// <summary>    /// The 'Adaptee' class    /// </summary>    internal class Adaptee    {        #region Public Methods and Operators        /// <summary>        /// The specific request.        /// </summary>        public void SpecificRequest()        {            Console.WriteLine("Called SpecificRequest()");        }        #endregion    }}// Output:/*Called SpecificRequest()*/

This real-world code demonstrates the use of a legacy chemical databank. Chemical compound objects access the databank through an Adapter interface.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Adapter Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Real-World Adapter Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Non-adapted chemical compound            var unknown = new Compound("Unknown");            unknown.Display();            // Adapted chemical compounds            Compound water = new RichCompound("Water");            water.Display();            Compound benzene = new RichCompound("Benzene");            benzene.Display();            Compound ethanol = new RichCompound("Ethanol");            ethanol.Display();        }        #endregion    }    /// <summary>    /// The 'Target' class    /// </summary>    internal class Compound    {        #region Fields        /// <summary>        /// The chemical.        /// </summary>        protected readonly string Chemical;        /// <summary>        /// The boiling point.        /// </summary>        protected float BoilingPoint;        /// <summary>        /// The melting point.        /// </summary>        protected float MeltingPoint;        /// <summary>        /// The molecular formula.        /// </summary>        protected string MolecularFormula;        /// <summary>        /// The molecular weight.        /// </summary>        protected double MolecularWeight;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Compound"/> class.        /// </summary>        /// <param name="chemical">        /// The chemical.        /// </param>        public Compound(string chemical)        {            this.Chemical = chemical;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public virtual void Display()        {            Console.WriteLine("\nCompound: {0} ------ ", this.Chemical);        }        #endregion    }    /// <summary>    /// The 'Adapter' class    /// </summary>    internal class RichCompound : Compound    {        #region Fields        /// <summary>        /// The bank.        /// </summary>        private ChemicalDatabank bank;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="RichCompound"/> class.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public RichCompound(string name)            : base(name)        {        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public override void Display()        {            // The Adaptee            this.bank = new ChemicalDatabank();            this.BoilingPoint = this.bank.GetCriticalPoint(this.Chemical, "B");            this.MeltingPoint = this.bank.GetCriticalPoint(this.Chemical, "M");            this.MolecularWeight = this.bank.GetMolecularWeight(this.Chemical);            this.MolecularFormula = this.bank.GetMolecularStructure(this.Chemical);            base.Display();            Console.WriteLine(" Formula: {0}", this.MolecularFormula);            Console.WriteLine(" Weight : {0}", this.MolecularWeight);            Console.WriteLine(" Melting Pt: {0}", this.MeltingPoint);            Console.WriteLine(" Boiling Pt: {0}", this.BoilingPoint);        }        #endregion    }    /// <summary>    /// The 'Adaptee' class    /// </summary>    internal class ChemicalDatabank    {        // The databank 'legacy API'        #region Public Methods and Operators        /// <summary>        /// The get critical point.        /// </summary>        /// <param name="compound">        /// The compound.        /// </param>        /// <param name="point">        /// The point.        /// </param>        /// <returns>        /// The <see cref="float"/>.        /// </returns>        public float GetCriticalPoint(string compound, string point)        {            // Melting Point            if (point == "M")            {                switch (compound.ToLower())                {                    case "water":                        return 0.0f;                    case "benzene":                        return 5.5f;                    case "ethanol":                        return -114.1f;                    default:                        return 0f;                }            }            switch (compound.ToLower())            {                case "water":                    return 100.0f;                case "benzene":                    return 80.1f;                case "ethanol":                    return 78.3f;                default:                    return 0f;            }        }        /// <summary>        /// The get molecular structure.        /// </summary>        /// <param name="compound">        /// The compound.        /// </param>        /// <returns>        /// The <see cref="string"/>.        /// </returns>        public string GetMolecularStructure(string compound)        {            switch (compound.ToLower())            {                case "water":                    return "H2O";                case "benzene":                    return "C6H6";                case "ethanol":                    return "C2H5OH";                default:                    return string.Empty;            }        }        /// <summary>        /// The get molecular weight.        /// </summary>        /// <param name="compound">        /// The compound.        /// </param>        /// <returns>        /// The <see cref="double"/>.        /// </returns>        public double GetMolecularWeight(string compound)        {            switch (compound.ToLower())            {                case "water":                    return 18.015;                case "benzene":                    return 78.1134;                case "ethanol":                    return 46.0688;                default:                    return 0d;            }        }        #endregion    }}// Output:/*Compound: Unknown ------Compound: Water ------ Formula: H2O Weight : 18.015 Melting Pt: 0 Boiling Pt: 100Compound: Benzene ------ Formula: C6H6 Weight : 78.1134 Melting Pt: 5.5 Boiling Pt: 80.1Compound: Ethanol ------ Formula: C2H5OH Weight : 46.0688 Melting Pt: -114.1 Boiling Pt: 78.3*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Adapter(C#)相关推荐

  1. Design Pattern - Visitor(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Memento(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Flyweight(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. C语言 带比较器的归并排序
  2. 怎么知道网站是用什么程序做的
  3. 大数据学习笔记1000条
  4. Dubbo架构的特点
  5. GARFIELD@04-14-2005
  6. python查询字符串是否包含某字符串_python 判断检测字符串中是否包含指定字符或字符串(比如:?)...
  7. 计算机丢失first,求大神解答硬盘驱动丢失怎么办
  8. anaconda和python区别_初学 Python 者自学 Anaconda 的正确姿势是什么?
  9. 算法与数据结构 设计模式
  10. 图文详解贷中监控报表与资产质量分析全过程
  11. 长期没有工作是什么感觉?
  12. SHAP(SHapley Additive exPlanation):Python的可解释机器学习库
  13. x86下vx6.8软件wind River workbench使用说明
  14. python画图-python绘图入门(完整版)
  15. javascript基础常识问答(五)
  16. 程序开发中的软实力和硬实力
  17. 【Unity 手写PBR】补充:多光源 阴影 视差 自发光
  18. 发布一款新闻资讯软件(android版)
  19. 苹果消息推送服务教程:第一部分(共2部分)
  20. PDF文档一键自动生成目录和书签

热门文章

  1. 想做一个显示全国火车运行图的网站(2) 数据整理
  2. Go 语言 Session机制和 Cookie机制
  3. 安装高可用性虚拟机,livemigration系列之九
  4. 杀死linux-zombie僵尸进程
  5. 内核compiler.h的学习
  6. 树状数组(Binary Indexed Tree) 总结
  7. Exchange 2013部署系列之(七)配置SSL多域名证书
  8. 请注意Tokyo Tyrant (ttserver)在大数据量下的不稳定
  9. python 删除list中的第一个元素
  10. linux shell 文件比较 diff 简介