📄 adapterrealworld.cs
字号:
using System;
using System.Windows.Forms;
using System.Text;
//适配器模式(Adapter)
//意图
// 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
//适用性
// 1.你想使用一个已经存在的类,而它的接口不符合你的需求。
// 2.你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
// 3.(仅适用于对象Adapter)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
namespace DesignPattern.AdapterRealWorld
{
class AdapterRealWorld : AbstractPattern
{
public static void Run(TextBox tbInfo)
{
s_tbInfo = tbInfo;
s_tbInfo.Text = "";
// Non-adapted chemical compound
Compound stuff = new Compound("Unknown");
stuff.Display();
// Adapted chemical compounds
Compound water = new RichCompound("Water"); //水
water.Display();
Compound benzene = new RichCompound("Benzene"); //苯
benzene.Display();
Compound alcohol = new RichCompound("Alcohol"); //酒精
alcohol.Display();
// Wait for user
////Console.Read();
}
}
// "Target"
class Compound
{
protected string name;
protected float boilingPoint;
protected float meltingPoint;
protected double molecularWeight;
protected string molecularFormula;
// Constructor
public Compound(string name)
{
this.name = name;
}
public virtual void Display()
{
DesignPattern.FormMain.OutputInfo("\nCompound: {0} ------ ", name);
}
}
// "Adapter"
class RichCompound : Compound
{
private ChemicalDatabank bank;
// Constructor
public RichCompound(string name) : base(name)
{
}
public override void Display()
{
// Adaptee
bank = new ChemicalDatabank();
boilingPoint = bank.GetCriticalPoint(name, "B");
meltingPoint = bank.GetCriticalPoint(name, "M");
molecularWeight = bank.GetMolecularWeight(name);
molecularFormula = bank.GetMolecularStructure(name);
base.Display();
DesignPattern.FormMain.OutputInfo(" Formula: {0}", molecularFormula);
DesignPattern.FormMain.OutputInfo(" Weight : {0}", molecularWeight);
DesignPattern.FormMain.OutputInfo(" Melting Pt: {0}", meltingPoint);
DesignPattern.FormMain.OutputInfo(" Boiling Pt: {0}", boilingPoint);
}
}
// "Adaptee"
class ChemicalDatabank
{
// The Databank 'legacy API'
public float GetCriticalPoint(string compound, string point)
{
float temperature = 0.0F;
// Melting Point
if (point == "M")
{
switch (compound.ToLower())
{
case "water" : temperature = 0.0F; break;
case "benzene" : temperature = 5.5F; break;
case "alcohol" : temperature = -114.1F; break;
}
}
// Boiling Point
else
{
switch (compound.ToLower())
{
case "water" : temperature = 100.0F; break;
case "benzene" : temperature = 80.1F; break;
case "alcohol" : temperature = 78.3F; break;
}
}
return temperature;
}
public string GetMolecularStructure(string compound)
{
string structure = "";
switch (compound.ToLower())
{
case "water" : structure = "H20"; break;
case "benzene" : structure = "C6H6"; break;
case "alcohol" : structure = "C2H6O2"; break;
}
return structure;
}
public double GetMolecularWeight(string compound)
{
double weight = 0.0;
switch (compound.ToLower())
{
case "water": weight = 18.015; break;
case "benzene": weight = 78.1134; break;
case "alcohol": weight = 46.0688; break;
}
return weight;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -