如何设置工厂模式返回的对象的不同属性?

发布于 2024-12-06 12:54:22 字数 276 浏览 6 评论 0原文

我想知道如何设置具体工厂返回的对象的属性。工厂可以返回具有属性A和B的objectA,但它也可以返回具有属性X、Y、Z的objectB。

由于客户端只知道objectA和objectB继承的接口,所以它不知道他正在处理哪个对象。所以,我的问题是,设置这些属性的 OO 方式是什么?我是否需要引入一种设置类,其中包含classA和classB的所有属性?但这不是面向对象,因为当有一个新类时,我也必须更新设置类...

我希望你理解我的问题并能帮助我:)

PS:如果重要的话,我正在使用 C#

I was wondering on how to set properties on which objects which are returned by the concrete factory. The factory can return objectA with properties A and B, but it can also return objectB with properties X, Y, Z.

Since the client only know the interface objectA and objectB inherits, it doesn't know which object he is dealing with. So, my question is, what is the OO way of setting these properties? Do I need to introduce a kind of setting class, which contains all the properties of classA and classB? But this isn't OO, because when there's a new class I have to update the setting class as well...

I hope you undserstand my question and can help me out :)

PS: If it matters, I am working with C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屌丝范 2024-12-13 12:54:22

您可以使用知道要设置哪些属性并可以将其分配给您获得的具体类(在工厂内部)的访问者。现在,该访问者可以以抽象方式设置所需的属性。

class Factory {

..
obj = new ConcreteObject();
obj.accept(new ConcreteObjectVisitor());
}

class ConcreteObject{

accept(Visitor visitor){
 visitor.visit(this);
}

}

class ConcreteObjectVisitor implements Visitor {

visit(ConcretTypeInterface param){

obj = (ConcretType)param;
param.setA()
param.setB()
param.setC()
}
}

You can use a visitor which knows which properties to set and can assign it to the concrete class which you get (inside the factory). Now this visitor can set the desired properties in abstract way.

class Factory {

..
obj = new ConcreteObject();
obj.accept(new ConcreteObjectVisitor());
}

class ConcreteObject{

accept(Visitor visitor){
 visitor.visit(this);
}

}

class ConcreteObjectVisitor implements Visitor {

visit(ConcretTypeInterface param){

obj = (ConcretType)param;
param.setA()
param.setB()
param.setC()
}
}
差↓一点笑了 2024-12-13 12:54:22

如果客户端需要设置公共接口中不存在的属性值,则它必须对工厂创建的对象的具体类型有一定的了解。有几种方法可以实现这一点:

  1. 客户端决定它需要什么类型的对象并调用适当的工厂操作。因此,对于这种情况,工厂将有不同的操作来创建 objectA 和 objectB。要设置的属性值可以作为这些操作的参数传递。

  2. 客户端决定它需要什么样的对象,并将这个决定作为工厂方法参数的值传递给工厂。值本身作为单个数组、集合或字典对象在另一个参数中传递。

  3. 工厂决定实例化哪个类,将新实例传递给客户端,然后客户端发现给定对象的具体类(在 C# 中通过 GetType() 方法)。如果客户端可以访问具体类,它可以执行强制转换,如果不能,它可以使用反射设置属性值。

如果需要示例,只需写评论:-)

If client needs to set values of properties that don't exist in common interface, it has to have some knowledge on concrete types of objects that the factory has created. There are several approaches to this:

  1. Client decides what kind of object it needs and calls appropriate factory operation. So for this scenario, the factory would have a different operations for creating objectA and objectB. Values of properties to be set could be passed as parameters of those operations.

  2. Client decides what kind of object it needs and passes this decision to factory as a value of a parameter of factory method. The values themselves are passed as a single array, collection or dictionary object in another parameter.

  3. Factory decides what class to instantiate, passes new instance to client and then client discovers the concrete class of given object (in C# via GetType() method). If concrete class is accessible to client, it can perform a cast, if not, it can set values of properties using reflection.

If examples are needed, just write a comment :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文