我需要知道这个例子是否符合适配器模式的意图。
此示例旨在满足可插拔适配器模式,但它适应了两个接口:
interface ITargetOld {
double Precise(double i);
}
interface ITargetNew {
string RoundEstimate(int i);
}
public class Adapter : ITargetOld, ITargetNew {
public double Precise(double i) {
return i / 3;
}
public string RoundEstimate(int i) {
return Math.Round(Precise(i)).ToString();
}
public string NewPrecise(int i) {
return Precise(Convert.ToInt64(i)).ToString();
}
}
客户将是:
class Program {
static void Main(string[] args) {
ITargetOld adapter1 = new Adapter();
Console.WriteLine("Old division format (Precise) " + adapter1.Precise(5));
ITargetNew adapter2 = new Adapter();
Console.WriteLine("New division format (Round Estimate) " + adapter2.RoundEstimate(5));
Adapter adapter3 = new Adapter();
Console.WriteLine("New Precise format " + adapter3.NewPrecise(5));
Console.ReadKey();
}
}
拉斐尔