更新:
海因兹是对的。AutoCAD Polyline是参考类型,而不是结构。好点子。但是我已经简化了方案,因为我在实际应用程序中处理的是结构的AutoCAD对象。因此,请同时将它们视为struct而不是引用类型。
我正在寻找在这种情况下采取的正确方法,如果有人可以阐明或帮助我加深理解,我将不胜感激。
数据访问层中有一个接口,其中包含两个实现来处理两个不同的提供程序:AutoCad和Sketchup API。
interface IEntity
{
void object GetPoly();
void void InsertPoly(object poly);
}
class AutocadEntity
{
void object GetPoly()
{
//calling Autocad APIs
return Autocad Polyline object
}
void InsertPoly(object poly){...}
}
GetPoly的Autocad实现将返回Polyline对象,因为这是Autocad API中定义为折线的对象,而Sketchup将返回Face对象。
我已经定义了返回类型(和参数)作为对象来处理这些不同的类型。成本是发生装箱/拆箱的性能问题。它更加大胆地显示了自身,其中返回/参数是object []。
我首先想知道使方法返回/参数类型通用是解决方案,但后来我认为不会,因为实现是特定于类型的。
尝试使用适配器模式将PolyLine和Face类型调整为您希望使用的单一类型。例如:
public abstract class BasePoly
{
public abstract double X { get; set; }
public abstract double Y { get; set; }
public abstract double Width { get; set; }
public abstract double Height { get; set; }
}
public abstract class BasePoly<T> : BasePoly
{
public T poly { get; private set; }
protected BasePoly(T poly) { this.poly = poly; }
}
public class PolyLineAdapter : BasePoly<PolyLine>
{
public PolyLineAdapter(PolyLine poly) : base(poly) {}
// override abstracts and forward to inner PolyLine instance at 'this.poly'
public override double X { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Y { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Width { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Height { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
}
public class FaceAdapter : BasePoly<Face>
{
public FaceAdapter(Face poly) : base(poly) {}
// override abstracts and forward to inner Face instance at 'this.poly'
public override double X { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Y { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Width { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
public override double Height { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
}
interface IEntity
{
BasePoly GetPoly();
void InsertPoly(BasePoly poly);
}
public abstract class Entity<TEntity> : IEntity
where TEntity : BasePoly
{
public BasePoly GetPoly()
{
return this.GetExternalPoly();
}
public abstract TEntity GetExternalPoly();
public void InsertPoly(BasePoly poly)
{
this.InsertExternalPoly((TEntity) poly);
}
public abstract void InsertExternalPoly(TEntity poly);
}
public class AutocadEntity : Entity<PolyLineAdapter>
{
public override PolyLineAdapter GetExternalPoly()
{
throw new NotImplementedException();
}
public override void InsertExternalPoly(PolyLineAdapter poly)
{
throw new NotImplementedException();
}
}
public class SketchupEntity : Entity<FaceAdapter>
{
public override FaceAdapter GetExternalPoly()
{
throw new NotImplementedException();
}
public override void InsertExternalPoly(FaceAdapter poly)
{
throw new NotImplementedException();
}
}
// fills for third party classes
public class PolyLine {}
public class Face {}
使用适配器模式,您可以提供代理层,以使两种第三方类型符合您要使用的类型。
请记住,此设计假设您一次只能使用一种类型的第三方引擎。如果您将同时使用两个引擎,请进行以下更改:
public class BasePoly
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
}
interface IEntity
{
BasePoly GetPoly();
void InsertPoly(BasePoly poly);
}
public abstract class Entity : IEntity
{
public abstract BasePoly GetPoly();
public abstract void InsertPoly(BasePoly poly);
}
public class AutocadEntity : Entity
{
public override BasePoly GetPoly()
{
// retrieve external type, convert it to BasePoly and return that
throw new NotImplementedException();
}
public override void InsertPoly(BasePoly poly)
{
// convert BasePoly to external type and insert that
throw new NotImplementedException();
}
}
public class SketchupEntity : Entity
{
public override BasePoly GetPoly()
{
// retrieve external type, convert it to BasePoly and return that
throw new NotImplementedException();
}
public override void InsertPoly(BasePoly poly)
{
// convert BasePoly to external type and insert that
throw new NotImplementedException();
}
}
// fills for third party classes
public class PolyLine {}
public class Face {}
另外,如果您担心适配器装箱或转换操作的成本(在您实际测量并确定是否需要优化后才能知道),则可以将适配器模式应用于正在消耗呼叫者IEntity
代替PolyLineAdapter / FaceAdapter或AutocadEntity / SketchupEntity类型本身。本质上,构建一个插件引擎。您也许可以使用泛型来抽象两个实现之间的常见用法。
这是一个dotnetfiddle示例:https ://dotnetfiddle.net/UsFPM7
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句