本文介绍了工厂模式:如何从客户端访问具体产品类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑使用Factory Pattern在Web应用程序中创建对象,但是我无法理解的是如何访问这些对象的属性.

I'm considering using Factory Pattern for creation of objects in a web application, but what I fail to grasp is how do I access the properties of these objects.

简化示例:我有一个由两个具体类TruckCarFactory和PickupCarFactory实现的CarFactory接口,也有一个由具体Pickup和Truck类实现的Car接口.现在,当创建新的Truck时,我的客户会与CarFactory界面交谈,以创建新的Car.

Simplified Example: I have a CarFactory Interface, implemented by two concrete classes TruckCarFactory and PickupCarFactory, also a Car Interface implemented by concrete Pickup and Truck classes.Now when creating a new Truck my client speaks to the CarFactory Interface creating a new Car.

然而,卡车对象应该具有很多属性,在卡车类中,我通常将这些属性作为具有getter/setter的私有成员使用.

Truck objects however are supposed to have lots of properties, which I usually would have as private members with getters/setters in the Truck class.

如何最好地从客户代码访问这些属性?我是否真的应该将所有吸气剂/设置剂放入CarFactory界面中?如果是这样,这是否意味着Trck和Pickup对象必须具有相同的属性集?

How do I best access these properties from my client code? Should I really put all getters/setters into the CarFactory Interface? If so, this would imply that Trck and Pickup object must have identical set of properties?

推荐答案

书写工具和设置工具应该位于Car界面中,是的...最佳实践方式的Truck和Pickup应该具有相同的属性集.他们可以在汽车实施之外拥有其他属性,但是从工厂拉出皮卡/卡车的东西应该与这些无关.

Getters and setters should be in the Car interface, and yes... best practice-wise Truck and Pickup should have the same set of properties. They could have additional properties outside of the car implementation but the thing that pulls out a pickup/truck from the factory should be agnostic of those.

这篇关于工厂模式:如何从客户端访问具体产品类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:22