【发布时间】:2009-10-23 14:49:08
【问题描述】:
如何限制对同一个命名空间内的类属性的访问?考虑下面的课程。 Content 类不能发布本身,而是 ContentService 类 在将状态更改为已发布之前会做一些事情。
public class Content : Entity, IContent
{
public string Introduction { get; set; }
public string Body { get; set; }
public IList<Comment> Comments { get; set; }
public IList<Image> Images { get; private set; }
public State Status { get; }
}
public class ContentService
{
public IContent Publish(IContent article)
{
//Perform some biz rules before publishing
article.Status = State.Published;
return article;
}
}
我怎样才能做到只有 ContentService 类可以改变文章的状态?
是否有任何设计模式可以帮助我处理这个问题?
【问题讨论】:
-
我不确定我是否明白这一点:您是否在寻求一种方法来保护实现免受您自己使用不当?我一定是错过了什么......