【发布时间】:2010-12-19 04:25:04
【问题描述】:
我有几个想法,但我想看看 SO 社区会提出什么建议。
我有一个带有抽象计算方法的抽象类。我有 2 个计算方式不同的实现。这对我来说是一种策略模式,但是其中一个实现需要设置一个 selected_type 变量,因为它在 Calculate 方法中使用。我想遵循 OCP,所以我的计算方法不应该包含依赖项。
这个类是通过 NHibernate 从数据库中检索的,并且 selected_type 变量在对象创建之前不会被设置。我试图避免使用 if 语句来设置 selected_type 仅当它具有特定实现时。最好的方法是什么?
这是一个代码示例:
public abstract class TagType
{
public virtual long id { get; protected set; }
public virtual string description { get; protected set; }
protected TagType(){}
protected TagType(string description)
{
this.description = description;
}
public abstract decimal Calculate();
}
public class TagTypeImpl1
{
public virtual int tag_months { get; protected set; }
protected TagType() { }
protected TagType(string description, int tag_months): base(description)
{
this.tag_months = tag_months;
}
public override decimal Calculate()
{
return (12*tag_months);
}
}
public class TagTypeImpl2
{
public virtual int tag_months { get; protected set; }
public virtual TagType selected_tag_type { get; protected set; }
protected TagType() { }
protected TagType(string description, int tag_months, TagType selected_tag_type): base(description)
{
this.tag_months = tag_months;
this.selected_tag_type = selected_tag_type;
}
public override decimal Calculate()
{
return selected_tag_type.Calculate() + (12*tag_months);
}
}
public class ConsumerController
{
private readonly IRepository<TagType> repository;
public ConsumerController(IRepository<TagType> repository)
{
this.repository = repository;
}
public ActionResult Index(long id)
{
var tag_type = repository.get(id);
//If using TagTypeImpl2 then the selected_tag_type variable needs to be set
//I want to avoid if(tag_type.GetType() == typeof(TagTypeImpl2)) set selected_tag_type
var result = tag_type.Calculate();
return Json(new {result});
}
}
我可能试图对这个类做太多事情,也许持久化的实体类是使用 Calculate 方法的错误位置,但它似乎是最好的位置,因为它最了解如何进行计算。
【问题讨论】:
-
您真的是说
selected_type变量与自己的抽象类属于同一类型吗?所以你想基本上把 TagType 的实例链接在一起? -
对于那个实现来说是的,但是会有其他实现不会使用它。该计算只是一个基本示例,它们会更复杂。
-
即使你为它做了一个特殊情况,selected_type 究竟如何被初始化......你从哪里得到它?需要知道才能知道谁拥有该信息以及可以在何处/何时访问。
标签: c# design-patterns