繁体   English   中英

设计图案适配器

[英]Design pattern Adapter

我正在学习适配器模式。 我有这个代码。 看起来像这种模式吗? SQLiteConnection,SQLiteCommand,SQLiteDataReader-来自其他库

现在我有一个任务: 连接并从数据库返回所有用户 我选择了适配器模式:

public class user {
    public string _name { get; set; }
    public string _surname { get; set;}

}

public interface IExercise {
    void connectToDataBase();
    void List<user> returnAllUsers();
}

public Adapter : IExercise {
    SQLiteConnection _conn = null;
    SQLiteCommand _cmd;
    public Adapter(SQLiteConnection conn, SQLiteCommand cmd){
        _conn = conn;
        _cmd = cmd;
    }
    public void connectToDataBase(){
        // not important yet
    }

    public List<user> returnAllUsers(){
        _cmd = _conn.newCommand();
        _cmd.CommandText = "SELECT * FROM users";
        SQLiteDataReader dt = _cmd.ExecuteReader();
        List<user> lu = new List<user>();
        while(dt.Read()){
            lu.Add(new user {
                     _name = dt["name"].ToString(),
                     _surname = dt["surname"].ToString()
                    }
                );
        }
        return lu;
    }
}

我的问题只是:它看起来像是Adapter模式?

我不明白为什么您认为您的代码看起来像一个适配器,没有没有匹配的接口,适配器模式将一个类的接口映射到另一个类,以便它们可以一起工作。 这些不兼容的类可能来自不同的库或框架。

在此处输入图片说明

工作实例

http://ideone.com/ZZbwAE

定义

适配器可帮助两个不兼容的接口一起工作。 这是适配器的真实定义。 当您希望具有不兼容接口的两个不同类一起工作时,可以使用适配器设计模式。 接口可能不兼容,但内部功能应适合需要。 适配器模式通过将一个类的接口转换为客户端期望的接口,可以使其他不兼容的类一起工作。

来自http://www.dofactory.com/Patterns/PatternAdapter.aspx的示例

using System;

namespace AdapterPattern
{
  /// <summary>
  /// MainApp startup class for Real-World
  /// Adapter Design Pattern.
  /// </summary>
  class MainApp
  {
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    static void Main()
    {
      // Non-adapted chemical compound
      Compound 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();

      // Wait for user
      Console.ReadKey();
    }
  }

  /// <summary>
  /// The 'Target' class
  /// </summary>
  class Compound
  {
    protected string _chemical;
    protected float _boilingPoint;
    protected float _meltingPoint;
    protected double _molecularWeight;
    protected string _molecularFormula;

    // Constructor
    public Compound(string chemical)
    {
      this._chemical = chemical;
    }

    public virtual void Display()
    {
      Console.WriteLine("\nCompound: {0} ------ ", _chemical);
    }
  }

  /// <summary>
  /// The 'Adapter' class
  /// </summary>
  class RichCompound : Compound
  {
    private ChemicalDatabank _bank;

    // Constructor
    public RichCompound(string name)
      : base(name)
    {
    }

    public override void Display()
    {
      // The Adaptee
      _bank = new ChemicalDatabank();

      _boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
      _meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
      _molecularWeight = _bank.GetMolecularWeight(_chemical);
      _molecularFormula = _bank.GetMolecularStructure(_chemical);

      base.Display();
      Console.WriteLine(" Formula: {0}", _molecularFormula);
      Console.WriteLine(" Weight : {0}", _molecularWeight);
      Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
      Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
    }
  }

  /// <summary>
  /// The 'Adaptee' class
  /// </summary>
  class ChemicalDatabank
  {
    // The databank 'legacy API'
    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;
        }
      }
      // Boiling Point
      else
      {
        switch (compound.ToLower())
        {
          case "water": return 100.0f;
          case "benzene": return 80.1f;
          case "ethanol": return 78.3f;
          default: return 0f;
        }
      }
    }

    public string GetMolecularStructure(string compound)
    {
      switch (compound.ToLower())
      {
        case "water": return "H20";
        case "benzene": return "C6H6";
        case "ethanol": return "C2H5OH";
        default: return "";
      }
    }

    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;
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM