日期:2009-07-11  浏览次数:20365 次

//MyAdapter
using System;
//Class1
class SimpleDrawer
{

//Methods
virtual public void SetColor(string name){}
};
//Adapter Class which enables class1 to use class2 methods
class AssistantDrawer:SimpleDrawer
{
//use the Adaptee
ProfessionalDrawer drawer = new ProfessionalDrawer();
//Methods
override public void SetColor(string name)
{
//colors
switch(name)
{
case "white":drawer.SetColor(255,255,255);break;
case "black":drawer.SetColor(0,0,0);break;
default:Console.WriteLine("I haven't seen this color!");break;
}
}
};
//Class2,Adaptee
class ProfessionalDrawer
{
//Methods
public void SetColor(int a,int b,int c)
{
Console.WriteLine("set the color to RGB({0},{1},{2})",a,b,c);
}
};

//TestApp
class TestApp
{
public static void Main(string []args)
{
AssistantDrawer drawer=new AssistantDrawer();
drawer.SetColor("red");
drawer.SetColor("black");
drawer.SetColor("white");
while(true){}
}
};