将老接口适配到新接口上时可用
using System;
namespace 适配器模式
{
class Program
{
static void Main(string[] args)
{
MssqlImpl mi = new MssqlImpl();
mi.Read();
mi.Write();
Adapter adapter = new Adapter();
adapter.Read();
adapter.Write();
Console.Read();
}
}
abstract class DBImpl //新接口
{
public abstract void Read();
public abstract void Write();
}
class MssqlImpl : DBImpl
{
public override void Read()
{
Console.WriteLine("MSSQL 读取数据库");
}
public override void Write()
{
Console.WriteLine("MSSQL 写入数据库");
}
}
class AccessImpl //老接口
{
public void AccessRead()
{
Console.WriteLine("Access 读取数据库");
}
public void AccessWrite()
{
Console.WriteLine("Access 写入数据库");
}
}
class Adapter : DBImpl
{
private AccessImpl ai = new AccessImpl();
public override void Read()
{
ai.AccessRead();
}
public override void Write()
{
ai.AccessWrite();
}
}
}