using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Design_Patterns
{
//将类的接口转换为客户希望的另一个接口
//目标(Target)角色:定义Client使用的接口。
//被适配(Adaptee)角色:这个角色有一个已存在并使用了的接口,而这个接口是需要我们适配的。
//适配器(Adapter)角色:这个适配器模式的核心。它将被适配角色已有的接口转换为目标角色希望的接口。
//场景:原有系统的接口 发现其实现不能直接使用,在短时间内又无法改造,此时可以使用适配器模式来转换。
//此模式是一种“亡羊补牢”的方法,常用于后期和维护期,也有用于设计期 考虑到三方接口时。
//常用插座接口
public abstract class SocketTarget
{
public abstract void GetNomalSocket();//常用插座
}
//进行适配 特殊插座
class SocketAdapter : SocketTarget
{
private SocketAdaptee adaptee = new SocketAdaptee();
public override void GetNomalSocket()
{
Console.WriteLine("Nomal Socket");
adaptee.GetSpecialSocket();
}
}
//特殊插座
public class SocketAdaptee
{
public void GetSpecialSocket()
{
Console.WriteLine("Special Socket");
}
}
//球员
public abstract class Player
{
private string _name = string.Empty;
//public string Name
//{
// get { return _name; }
// set { _name = value; }
//}
//考虑了下 感觉 使用构造函数比使用属性更符合场景
public Player(string name)
{
this._name = name;
}
//进攻
public abstract void Attack();
//防守
public abstract void Defense();
}
//前锋
class Forwards : Player
{
public Forwards(string name):base(name)
{
}
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//中锋
class CenterForwards : Player
{
public CenterForwards(string name)
: base(name)
{ }
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//后卫
class FullBack : Player
{
public FullBack(string name)
: base(name)
{ }
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//外籍中锋 适配器
class OtherCountryCF : Player
{
public OtherCountryCF(string name)
: base(name)
{ }
ChinaCF chinacf = new ChinaCF();
public override void Attack()
{
chinacf.进攻();
throw new NotImplementedException();
}
public override void Defense()
{
chinacf.防守();
throw new NotImplementedException();
}
}
//中国中锋 被适配
class ChinaCF
{
public void 进攻()
{
throw new NotImplementedException();
}
public void 防守()
{
throw new NotImplementedException();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Design_Patterns
{
//将类的接口转换为客户希望的另一个接口
//目标(Target)角色:定义Client使用的接口。
//被适配(Adaptee)角色:这个角色有一个已存在并使用了的接口,而这个接口是需要我们适配的。
//适配器(Adapter)角色:这个适配器模式的核心。它将被适配角色已有的接口转换为目标角色希望的接口。
//场景:原有系统的接口 发现其实现不能直接使用,在短时间内又无法改造,此时可以使用适配器模式来转换。
//此模式是一种“亡羊补牢”的方法,常用于后期和维护期,也有用于设计期 考虑到三方接口时。
//常用插座接口
public abstract class SocketTarget
{
public abstract void GetNomalSocket();//常用插座
}
//进行适配 特殊插座
class SocketAdapter : SocketTarget
{
private SocketAdaptee adaptee = new SocketAdaptee();
public override void GetNomalSocket()
{
Console.WriteLine("Nomal Socket");
adaptee.GetSpecialSocket();
}
}
//特殊插座
public class SocketAdaptee
{
public void GetSpecialSocket()
{
Console.WriteLine("Special Socket");
}
}
//球员
public abstract class Player
{
private string _name = string.Empty;
//public string Name
//{
// get { return _name; }
// set { _name = value; }
//}
//考虑了下 感觉 使用构造函数比使用属性更符合场景
public Player(string name)
{
this._name = name;
}
//进攻
public abstract void Attack();
//防守
public abstract void Defense();
}
//前锋
class Forwards : Player
{
public Forwards(string name):base(name)
{
}
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//中锋
class CenterForwards : Player
{
public CenterForwards(string name)
: base(name)
{ }
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//后卫
class FullBack : Player
{
public FullBack(string name)
: base(name)
{ }
public override void Attack()
{
throw new NotImplementedException();
}
public override void Defense()
{
throw new NotImplementedException();
}
}
//外籍中锋 适配器
class OtherCountryCF : Player
{
public OtherCountryCF(string name)
: base(name)
{ }
ChinaCF chinacf = new ChinaCF();
public override void Attack()
{
chinacf.进攻();
throw new NotImplementedException();
}
public override void Defense()
{
chinacf.防守();
throw new NotImplementedException();
}
}
//中国中锋 被适配
class ChinaCF
{
public void 进攻()
{
throw new NotImplementedException();
}
public void 防守()
{
throw new NotImplementedException();
}
}
}
适配器模式
本文介绍适配器模式的应用场景及实现方式,通过示例代码展示了如何将不兼容接口转换为客户端期望的形式。适用于解决现有系统接口无法直接使用的问题。
610

被折叠的 条评论
为什么被折叠?



