Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设计模式适配器_C#_Design Patterns_Adapter - Fatal编程技术网

C# 设计模式适配器

C# 设计模式适配器,c#,design-patterns,adapter,C#,Design Patterns,Adapter,我正在学习适配器模式。我有这个密码。看起来像这个图案吗? SQLiteConnection、SQLiteCommand、SQLiteDataReader—它来自其他库 现在我有了任务:连接和从数据库返回所有用户。我选择了适配器模式: public class user { public string _name { get; set; } public string _surname { get; set;} } public interface IExercise {

我正在学习适配器模式。我有这个密码。看起来像这个图案吗? 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;
    }
}
公共类用户{
公共字符串_name{get;set;}
公共字符串_姓氏{get;set;}
}
公共接口IExercise{
void connectToDataBase();
void List returnalUsers();
}
公共适配器:IExercise{
SQLiteConnection _conn=null;
SQLiteCommand_cmd;
公共适配器(SQLiteConnection conn,SQLiteCommand cmd){
_conn=conn;
_cmd=cmd;
}
public void connectToDataBase(){
//还不重要
}
公开名单{
_cmd=_conn.newCommand();
_cmd.CommandText=“从用户中选择*”;
SQLiteDataReader dt=_cmd.ExecuteReader();
List lu=新列表();
而(dt.Read()){
添加(新用户){
_name=dt[“name”].ToString(),
_姓氏=dt[“姓氏”]。ToString()
}
);
}
返回lu;
}
}

我的问题是:它看起来像适配器模式?

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

工作示例

定义

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

来自

使用系统;
命名空间适配器模式
{
/// 
///适用于真实世界的MainApp启动类
///适配器设计模式。
/// 
类MainApp
{
/// 
///进入控制台应用程序的入口点。
/// 
静态void Main()
{
//不适应化合物
未知化合物=新化合物(“未知”);
未知。Display();
//适应化合物
复合水=新富氏化合物(“水”);
水。显示();
化合物苯=新化合物(“苯”);
苯显示();
化合物乙醇=新化合物(“乙醇”);
乙醇显示();
//等待用户
Console.ReadKey();
}
}
/// 
///“目标”类
/// 
类化合物
{
保护管柱(化学);;
受保护浮点数;
受保护的浮点数;
保护双分子量;
保护串分子式;
//建造师
公共化合物(字符串化学品)
{
这是化学的;
}
公共虚拟空间显示()
{
Console.WriteLine(“\n复合:{0}------”,\u化学);
}
}
/// 
///“适配器”类
/// 
类化合物:化合物
{
私人化学数据库;
//建造师
公共RichCompound(字符串名称)
:base(名称)
{
}
公共覆盖无效显示()
{
//改编者
_bank=新化学数据库();
_沸点=_bank.GetCriticalPoint(_chemical,“B”);
_熔化点=_bank.GetCriticalPoint(_chemical,“M”);
_分子量=_bank.GetMolecularWeight(_chemical);
_分子式=_bank.GetMolecularStructure(_chemical);
base.Display();
WriteLine(“公式:{0}”,分子式);
控制台写入线(“重量:{0}”,分子量);
WriteLine(“熔点:{0}”,熔点);
WriteLine(“沸点:{0}”,沸点);
}
}
/// 
///“适应”类
/// 
类化学数据库
{
//数据库“遗留API”
公共浮点GetCriticalPoint(字符串复合,字符串点)
{
//熔点
如果(点==“M”)
{
开关(composite.ToLower())
{
案例“水”:返回0.0f;
案例“苯”:返回5.5f;
案例“乙醇”:返回-114.1f;
默认值:返回0f;
}
}
//沸点
其他的
{
开关(composite.ToLower())
{
“水”情况:返回100.0f;
案例“苯”:返回80.1f;
案例“乙醇”:返回78.3f;
默认值:返回0f;
}
}
}
公共字符串GetMolecularStructure(字符串化合物)
{
开关(composite.ToLower())
{
案例“水”:返回“H20”;
案例“苯”:返回“C6H6”;
案例“乙醇”:返回“C2H5OH”;
默认值:返回“”;
}
}
公共双分子量(字符串化合物)
{
开关(composite.ToLower())
{
案例“水”:返回18.015;
案例“苯”:返回78.1134;
案例“乙醇”:返回46.0688;
默认值:返回0d;
}
}
}
}

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

工作示例

定义

适配器帮助两个不兼容的接口协同工作。这是 适配器的真实定义。适配器设计模式 当需要两个不同的类时使用
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;
      }
    }
  }
}