My Adapter in C#

类别:.NET开发 点击:0 评论:0 推荐:

//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){}
 }
};

本文地址:http://com.8s8s.com/it/it44564.htm