我有项目呼叫仓库。仓库里有产品。我想写一个选项通知仓库预期的产品。我就是这么想的:
公共密封类ExpectedProductsMediator {私有静态易失性ExpectedProductsMediator _Instance;私有静态对象_SyncRoot =新对象();委托IEnumerable ExpectedProductsGenerator(DateTime startDate,DateTime endDate);私有IList _ExpectedProductsGenerators;私有ExpectedProductsMediator() { _ExpectedProductsGenerators =新列表();}公共静态ExpectedProductsMediator实例{ get { if (_Instance == null) { lock (_SyncRoot) { if (_Instance == null) _Instance =新ExpectedProductsMediator();}返回公共空AddExpectedProductsGenerator(ExpectedProductsGenerator生成器){_ExpectedProductsGenerators.Add(生成器);}公共空RemoveExpectedProductsGenerator(ExpectedProductsGenerator生成器{ _ExpectedProductsGenerators.Remove(generator);} public IEnumerable GetExpectedProducts(DateTime startDate,DateTime endDate) { IEnumerable products = null;if (_ExpectedProductsGenerators.Any()) { products =_ExpectedProductsGenerators.Any endDate);foreach (_ExpectedProductsGenerators.Skip(1)中的ExpectedProductsGenerator生成器){ products =products.Concat(startDate,endDate);}返回产品;} }
该调解员将在Warehouse.Common项目上。现在,如果项目B希望提供预期的产品,它需要使用AddExpectedProductsGenerator()方法注册它的生成器:
public static class ExpectedProductsRegistrar {
static ExpectedProductsRegistrar() {
ExpectedProductsMediator.Instance.AddExpectedProductsGenerator(someGenerator);
}
}
每当仓库需要获得预期的产品时,它所要做的就是调用GetExpectedProducts()方法。
在这个实施过程中,我有两个问题:
它向所有其他项目公开了constractor. ()方法。
你对我的实现有什么看法?是否有更好的解决办法来满足这种需要?我是否正确地使用了设计模式?
发布于 2011-08-21 10:44:59
最后,我使用了这个实现。我没有找到更好的方法,这段代码更容易维护。
发布于 2011-08-16 11:47:01
我会让你的API接受新产品,然后写到存储(文件,DB等)。然后仓库项目从存储中读取预期的产品。然后,您可以摆脱您的单例,并尽可能少地通过API公开。
https://stackoverflow.com/questions/7077656
复制相似问题