说,我有ManYouCanHit
两个类RichMan
和的适配器类TaxiDriver
,它们都派生自Person
. 我只能访问RichMan
or的实例TaxiDriver
public interface IManYouCanHit
{
bool CanIHitThisGuy;
}
public class ManYouCanHit : IManYouCanHit
{
private enum ManType
{
Rich = 0,
TaxiDriver = 1
}
private RichMan richMan;
private TaxiDriver driver;
private ManType type;
public Object ManReference
{
get
{
if(this.type == ManType.Rich)
{
return this.richMan;
}
else
{
return this.driver;
}
}
}
public bool CanIHitThisGuy
{
get{
bool canHit = true;
if(this.type == ManType.RichMan)
{
canHit = false;
}
return canHit;
}
}
public ManYouCanHit(Person someone)
{
if(someone is RichMan)
{
this.richMan = someone;
this.type = ManType.Rich;
}
else if(someone is TaxiDriver)
{
this.driver = someone;
this.type = ManType.TaxiDriver;
}
else
{
// throw an error
}
}
}
真实的逻辑比例子复杂,有很多属性和方法可以实现CanIHitThisGuy
可以将两个对象包装到一个适配器中吗?我还能遵循什么其他模式?
这种设计是好是坏?你能给我一些建议吗?
我应该通过暴露原始对象ManReference
吗?