Adapter pattern 适配器模式
七月 31, 2007 | 软件工程/编程技巧/设计模式 | RSS 2.0
using System;
using System.Collections.Generic;
using System.Text;
/*****************************************************
* Copyright : [url]www.blog.163.com/liufabin66688[/url]
* FileName : 适配器模式
* Author : 刘发宾
* NickName : 小和尚
* Version : v1.0
* Description :
* CreatDate : 2007-7-31
* HistoryEdit : 历史修改记录
———- 第一次修改 ——————-
Author :
Date :
Summary :
————– ————————
*****************************************************/
namespace ConsoleApplication1
{
class ChemicalCompound
{
protected string name;
protected float boilingPoint;
protected float meltingPoint;
protected double molecularWeight;
protected string molecularFormula;
//构造函数
public ChemicalCompound(string name)
{
this.name = name;
}
//属性
public float BoilingPoint
{
get { return boilingPoint; }
}
public float MeltingPoint
{
get { return meltingPoint; }
}
public double MolecularWeight
{
get { return molecularWeight; }
}
public string MolecularFormula
{
get { return molecularFormula; }
}
}
//适配器,母接口-化学数据银行
class ChemicalDatabank
{
//取得临界点
public float GetCriticalPoint(string compound,string point)
{
float temperature=0.0F;
//熔点
if(point==”M”)
{
switch(compound.ToLower())
{
case “water”:temperature=0.0F;break;
case “benzene”:temperature=5.5F;break;
case “alcohol”:temperature=-114.1F;break;
}
}
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=”C2H602″;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 “alcolor”:weight=46.0688;break;
}
return weight;
}
}
//公接口
class Compound : ChemicalCompound
{
private ChemicalDatabank bank;
public Compound(string name)
: base(name)
{
//适配器,母接口化学数据银行
bank = new ChemicalDatabank();
boilingPoint = bank.GetCriticalPoint(name,”B”);
meltingPoint = bank.GetCriticalPoint(name,”M”);
molecularWeight = bank.GetMolecularWeight(name);
molecularFormula=bank.GetMolecularStructure(name);
}
public void Dispaly()
{
Console.WriteLine(”化合物:{0}”,name);
Console.WriteLine(”分子式:{0}”,MolecularFormula);
Console.WriteLine(”分子式:{0}”,MolecularWeight);
Console.WriteLine(”熔点:{0}”,MeltingPoint);
Console.WriteLine(”妇点:{0}”, BoilingPoint);
}
}
class Program
{
static void Main(string[] args)
{ //显示取得水的特性
Compound water = new Compound(”Water”);
water.Dispaly();
//显示取得笨的特性
Compound benzene = new Compound(”Benzene”);
benzene.Dispaly();
Compound alcohol = new Compound(”Alcohol”);
alcohol.Dispaly();
Console.Read();
}
}
}
using System;
…{
class Program
…{
}
}
