1、旧的原始类
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassAdapter
{
public class OldModule
{
public virtual void methodA()
{
System.Console.WriteLine("OldModule methodA");
}
}
}
2、目标接口
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassAdapter
{
interface ITargetModule
{
void methodA();
void methodB();
void methodC();
}
}
3、类的适配器
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassAdapter
{
public class ModuleAdapter : OldModule, ITargetModule
{
public void methodB()
{
System.Console.WriteLine("ModuleAdapter MethodB");
}
public void methodC()
{
System.Console.WriteLine("ModuleAdapter MethodC");
}
}
}
4.测试
using System;
namespace ClassAdapter
{
class Program
{
static void Main(string[] args)
{
ITargetModule moduleAdapter = new ModuleAdapter();
moduleAdapter.methodA();
moduleAdapter.methodB();
moduleAdapter.methodC();
}
}
}