概要
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
类图
需求
生产坦克,要求A型号的坦克10量,B型号的坦克10量。目前仓库有A型部件15,B型部件5件。
如何将A型部件应用到B型坦克上呢,用适配器解决这个问题。
运行
代码
using System;
using System.Collections.Generic;
namespace 适配器模式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Client client = new Client();
client.mian();
Console.ReadLine();
}
}
static class config
{
public static int functionNo = 0;
}
interface IAFunction
{
void execute();
}
interface IBFunction
{
void operation();
}
interface ITank
{
void sort();
void run();
string getName();
}
abstract class BaseTank : ITank
{
static int indexNo = 0;
private int id;
protected BaseTank()
{
id = indexNo++;
}
public string getName()
{
return "name" + id;
}
public abstract void run();
public abstract void sort();
}
class ATank : BaseTank
{
private IAFunction aSortFunction;
private IAFunction aRunFunction;
public ATank(IAFunction aSortFunction, IAFunction aRunFunction)
{
this.aSortFunction = aSortFunction;
this.aRunFunction = aRunFunction;
}
public override void run()
{
this.aRunFunction.execute();
}
public override void sort()
{
Console.WriteLine(getName());
this.aSortFunction.execute();
}
}
class BTank : BaseTank
{
private IBFunction bSortFunction;
private IBFunction bRunFunction;
public BTank(IBFunction bSortFunction, IBFunction bRunFunction)
{
this.bSortFunction = bSortFunction;
this.bRunFunction = bRunFunction;
}
public override void run()
{
this.bRunFunction.operation();
}
public override void sort()
{
Console.WriteLine(getName());
this.bSortFunction.operation();
}
}
abstract class BaseAFunction : IAFunction
{
protected int id;
protected BaseAFunction()
{
id = config.functionNo++;
}
public abstract void execute();
}
class ASortFunction : BaseAFunction
{
public override void execute()
{
Console.WriteLine($"{this.id}射击");
}
}
class ARunFunction : BaseAFunction
{
public override void execute()
{
Console.WriteLine($"{this.id}跑");
}
}
abstract class BbaseFunction : IBFunction
{
protected int id;
protected BbaseFunction()
{
id = config.functionNo++;
}
public abstract void operation();
}
class BSortFunction : BbaseFunction
{
public override void operation()
{
Console.WriteLine($"{this.id}射击");
}
}
class BRunFunction : BbaseFunction
{
public override void operation()
{
Console.WriteLine($"{this.id}跑");
}
}
class Adapter : IAFunction, IBFunction
{
private IAFunction aFunction;
public Adapter(IAFunction aFunction)
{
this.aFunction = aFunction;
}
public void execute()
{
aFunction.execute();
}
public void operation()
{
execute();
}
}
class AFunBox
{
public AFunBox(ASortFunction sort, ARunFunction run)
{
this.sort = sort;
this.run = run;
}
public ASortFunction sort;
public ARunFunction run;
}
class BFunBox
{
public BFunBox(BSortFunction sort, BRunFunction run)
{
this.sort = sort;
this.run = run;
}
public BSortFunction sort;
public BRunFunction run;
}
class FunctionBase
{
//目前只有A型坦克配件15套和B型坦克10套
public List<AFunBox> aFunBoxes = new List<AFunBox>();
public List<BFunBox> bFunBoxes = new List<BFunBox>();
public FunctionBase()
{
for(int i = 0; i < 15; i++)
{
aFunBoxes.Add(new AFunBox(new ASortFunction(), new ARunFunction()) );
}
for(int i = 0; i < 5; i++)
{
bFunBoxes.Add(new BFunBox(new BSortFunction(), new BRunFunction()));
}
}
}
class TankFactory
{
List<ITank> tanks = new List<ITank>();
FunctionBase functionBase = new FunctionBase();
//创建A型坦克10量
//创建B型坦克10量
public void createAtank()
{
int indexSum = 0;
foreach(AFunBox aFunBox in functionBase.aFunBoxes)
{
indexSum++;
if(indexSum<10)
{
ITank tank = new ATank(aFunBox.sort, aFunBox.run);
//functionBase.aFunBoxes.Remove(aFunBox);
tanks.Add(tank);
}
else
{
break;
}
}
}
public void createBtank()
{
int indexSum = 0;
foreach (BFunBox bFunBox in functionBase.bFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new BTank(bFunBox.sort, bFunBox.run);
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
}
else
{
break;
}
indexSum++;
}
}
public void diaplay()
{
foreach(ITank tank in tanks)
{
tank.sort();
tank.run();
}
Console.WriteLine($"生产坦克数量{tanks.Count}");
}
}
class TankFactoryByAdapter
{
List<ITank> tanks = new List<ITank>();
FunctionBase functionBase = new FunctionBase();
//创建A型坦克10量
//创建B型坦克10量
public void createAtank()
{
int indexSum = 0;
foreach (AFunBox aFunBox in functionBase.aFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new ATank(aFunBox.sort, aFunBox.run);
//functionBase.aFunBoxes.Remove(aFunBox);
tanks.Add(tank);
}
else
{
ITank tank = new BTank(new Adapter(aFunBox.sort),new Adapter(aFunBox.run));
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
}
indexSum++;
}
}
public void createBtank()
{
int indexSum = 0;
foreach (BFunBox bFunBox in functionBase.bFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new BTank(bFunBox.sort, bFunBox.run);
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
}
else
{
break;
}
indexSum++;
}
}
public void diaplay()
{
foreach (ITank tank in tanks)
{
tank.sort();
tank.run();
}
Console.WriteLine($"生产坦克数量{tanks.Count}");
}
}
class Client
{
public void mian()
{
//创建A型坦克10量
//创建B型坦克10量
diaplay1();
diaplay2();
}
private void diaplay1()
{
Console.WriteLine("未使用适配器");
TankFactory tankFactory = new TankFactory();
tankFactory.createAtank();
tankFactory.createBtank();
tankFactory.diaplay();
}
private void diaplay2()
{
Console.WriteLine("使用适配器");
TankFactoryByAdapter tankFactory = new TankFactoryByAdapter();
tankFactory.createAtank();
tankFactory.createBtank();
tankFactory.diaplay();
}
}
}
代码优化
消除冗余
概述
使用了泛型是技术对代码进行了优化,因为关于机能部分IAFunction,IBFunction有很多实现部分的代码是冗余的。但因接口不同,无法用面向对象的方法进行封装,所以采取了泛型的方式。
运行
代码
using System;
using System.Collections.Generic;
namespace 适配器模式
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Client client = new Client();
client.mian();
Test test = new Test();
//test.main();
Console.ReadLine();
}
}
// 接口层-----------------------------------------------------
interface IAFunction
{
void execute();
}
interface IBFunction
{
void operation();
}
interface ITank
{
void sort();
void run();
string getName();
}
// 抽象层-----------------------------------------------------
abstract class BaseTank<T> : ITank
{
protected T aSortFunction;
protected T aRunFunction;
public BaseTank(T aSortFunction, T aRunFunction)
{
this.aSortFunction = aSortFunction;
this.aRunFunction = aRunFunction;
}
static int indexNo = 0;
private int id;
protected BaseTank()
{
id = indexNo++;
}
public string getName()
{
return "name" + id;
}
public abstract void sort();
public abstract void run();
}
class ATank : BaseTank<IAFunction>
{
public ATank(IAFunction aSortFunction, IAFunction aRunFunction):base(aSortFunction, aRunFunction)
{
}
public override void run()
{
this.aRunFunction.execute();
}
public override void sort()
{
Console.WriteLine(getName());
this.aSortFunction.execute();
}
}
class BTank : BaseTank<IBFunction>
{
public BTank(IBFunction bSortFunction, IBFunction bRunFunction):base(bSortFunction,bRunFunction){ }
public override void run()
{
this.aRunFunction.operation();
}
public override void sort()
{
Console.WriteLine(getName());
this.aSortFunction.operation();
}
}
abstract class BaseAFunction : IAFunction
{
static int functionNo = 0;
protected int id;
protected BaseAFunction()
{
id = functionNo++;
}
public abstract void execute();
}
abstract class BbaseFunction : IBFunction
{
static int functionNo = 0;
protected int id;
protected BbaseFunction()
{
id = functionNo++;
}
public abstract void operation();
}
// 实现层
class ASortFunction : BaseAFunction
{
public override void execute()
{
Console.WriteLine($"{this.id}射击");
}
}
class ARunFunction : BaseAFunction
{
public override void execute()
{
Console.WriteLine($"{this.id}跑");
}
}
class BSortFunction : BbaseFunction
{
public override void operation()
{
Console.WriteLine($"{this.id}射击");
}
}
class BRunFunction : BbaseFunction
{
public override void operation()
{
Console.WriteLine($"{this.id}跑");
}
}
class Adapter : IAFunction, IBFunction
{
private IAFunction aFunction;
public Adapter(IAFunction aFunction)
{
this.aFunction = aFunction;
}
public void execute()
{
aFunction.execute();
}
public void operation()
{
execute();
}
}
// 客户需求层(坦克工厂)
class FunBox<T>
{
public FunBox(T sort, T run)
{
this.sort = sort;
this.run = run;
}
public T sort;
public T run;
}
class FunctionBase
{
//目前只有A型坦克配件15套和B型坦克10套
public List<FunBox<IAFunction>> aFunBoxes = new List<FunBox<IAFunction>>();
public List<FunBox<IBFunction>> bFunBoxes = new List<FunBox<IBFunction>>();
public FunctionBase()
{
for(int i = 0; i < 15; i++)
{
aFunBoxes.Add(new FunBox<IAFunction>(new ASortFunction(), new ARunFunction()) );
}
for(int i = 0; i < 5; i++)
{
bFunBoxes.Add(new FunBox<IBFunction>(new BSortFunction(), new BRunFunction()));
}
}
public void display()
{
Console.WriteLine($"剩余A部件{aFunBoxes.Count}");
Console.WriteLine($"剩余B部件{bFunBoxes.Count}");
}
}
class TankFactory
{
protected List<ITank> tanks = new List<ITank>();
protected FunctionBase functionBase = new FunctionBase();
//创建A型坦克10量
//创建B型坦克10量
public virtual void createAtank()
{
List<FunBox<IAFunction>> aDelete = new List<FunBox<IAFunction>>();
int indexSum = 0;
foreach(FunBox<IAFunction> aFunBox in functionBase.aFunBoxes)
{
if(indexSum<10)
{
ITank tank = new ATank(aFunBox.sort, aFunBox.run);
//functionBase.aFunBoxes.Remove(aFunBox);
tanks.Add(tank);
aDelete.Add(aFunBox);
}
else
{
break;
}
indexSum++;
}
foreach(FunBox<IAFunction> aFunBox in aDelete)
{
functionBase.aFunBoxes.Remove(aFunBox);
}
}
public virtual void createBtank()
{
List<FunBox<IBFunction>> bDeletes = new List<FunBox<IBFunction>>();
int indexSum = 0;
foreach (FunBox<IBFunction> bFunBox in functionBase.bFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new BTank(bFunBox.sort, bFunBox.run);
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
bDeletes.Add(bFunBox);
}
else
{
break;
}
indexSum++;
}
foreach(FunBox<IBFunction> bFunBox in bDeletes)
{
functionBase.bFunBoxes.Remove(bFunBox);
}
}
public void diaplay()
{
foreach(ITank tank in tanks)
{
tank.sort();
tank.run();
}
Console.WriteLine($"生产坦克数量{tanks.Count}");
functionBase.display();
}
}
class TankFactoryByAdapter: TankFactory
{
//List<ITank> tanks = new List<ITank>();
//FunctionBase functionBase = new FunctionBase();
//创建A型坦克10量
//创建B型坦克10量
public override void createAtank()
{
List<FunBox<IAFunction>> aDelete = new List<FunBox<IAFunction>>();
int indexSum = 0;
foreach (FunBox<IAFunction> aFunBox in functionBase.aFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new ATank(aFunBox.sort, aFunBox.run);
//functionBase.aFunBoxes.Remove(aFunBox);
tanks.Add(tank);
aDelete.Add(aFunBox);
}
else
{
ITank tank = new BTank(new Adapter(aFunBox.sort),new Adapter(aFunBox.run));
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
aDelete.Add(aFunBox);
}
indexSum++;
}
foreach (FunBox<IAFunction> aFunBox in aDelete)
{
functionBase.aFunBoxes.Remove(aFunBox);
}
}
public override void createBtank()
{
List<FunBox<IBFunction>> bDeletes = new List<FunBox<IBFunction>>();
int indexSum = 0;
foreach (FunBox<IBFunction> bFunBox in functionBase.bFunBoxes)
{
if (indexSum < 10)
{
ITank tank = new BTank(bFunBox.sort, bFunBox.run);
//functionBase.bFunBoxes.Remove(bFunBox);
tanks.Add(tank);
bDeletes.Add(bFunBox);
}
else
{
break;
}
indexSum++;
}
foreach (FunBox<IBFunction> bFunBox in bDeletes)
{
functionBase.bFunBoxes.Remove(bFunBox);
}
}
}
class Client
{
public void mian()
{
//创建A型坦克10量
//创建B型坦克10量
diaplay1();
diaplay2();
}
private void diaplay1()
{
Console.WriteLine("未使用适配器");
TankFactory tankFactory = new TankFactory();
tankFactory.createAtank();
tankFactory.createBtank();
tankFactory.diaplay();
}
private void diaplay2()
{
Console.WriteLine("使用适配器");
TankFactoryByAdapter tankFactory = new TankFactoryByAdapter();
tankFactory.createAtank();
tankFactory.createBtank();
tankFactory.diaplay();
}
}
class Test
{
class T
{
static int index = 0;
public T()
{
id = T.index++;
name = "name" + id;
}
public int id;
public string name;
}
public void main()
{
List<T> ts = new List<T>();
for(int i = 0; i < 10; i++)
{
ts.Add(new T());
}
List<T> tdelete = new List<T>();
foreach(T t in ts)
{
Console.WriteLine(t.name);
tdelete.Add(t);
}
foreach(T t in tdelete)
{
ts.Remove(t);
}
Console.WriteLine(ts.Count);
Console.WriteLine(tdelete.Count);
}
}
}