被包装的对象是否有可能“成为”对象?相同类型的对象?
我有一个包装的 XmlDocument 类,在其中,我想检查是否存在具有相同名称的缓存 XmlDocument 对象,然后“成为”该对象。有更好的方法吗?
namespace myXmlUtilities {
class SpecificAutoLoadingCmsXmlDocument : System.Xml.XmlDocument {
private string documentName = "joiseyMike.xml";
public void loadFromCms() {
if (cache[documentName] != null)
LoadXml(((XmlDocument)cache[documentName]).OuterXml);
else
// ... load from the CMS's database.
}
public SpecificAutoLoadingCmsXmlDocument() {
loadFromCms();
}
}
编辑:我让这个例子变得更加真实。对早期的快速而肮脏的版本表示歉意。
I have a wrapped XmlDocument class, and in it, I'd like to check to see if there's a cached XmlDocument object with the same name, and then "become" that object. Is there a better way to do this?
namespace myXmlUtilities {
class SpecificAutoLoadingCmsXmlDocument : System.Xml.XmlDocument {
private string documentName = "joiseyMike.xml";
public void loadFromCms() {
if (cache[documentName] != null)
LoadXml(((XmlDocument)cache[documentName]).OuterXml);
else
// ... load from the CMS's database.
}
public SpecificAutoLoadingCmsXmlDocument() {
loadFromCms();
}
}
Edited: I made the example a little more true-to life. Apologies for the earlier quick-and-dirty version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用工厂模式,它允许您将此逻辑放入工厂方法中。
因此,您最终会得到:
因此,您将调用静态 GetNewDocument() 方法,而不是执行简单的 new XmlDocument();。
You should use a factory pattern instead which would allow you to put this logic into the factory method(s).
So you'd end up with:
So instead of doing a simple new XmlDocument();, you'd make a call to the static GetNewDocument() method.
我会在这里重做拱门。您缺少关注点的分离。为什么不使用工厂来检查缓存是否具有该名称并将该对象返回给您?对我来说,试图构建自身的对象似乎很混乱。
I would rework the arch here. You are missing a seperation of concerns. Why not use a factory to check if the cache has that name and give you that object back? An object trying to construct itself seems messy to me.