Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 根据客户的需求对内部状态进行不同的解释_Oop_Design Patterns_Ooad - Fatal编程技术网

Oop 根据客户的需求对内部状态进行不同的解释

Oop 根据客户的需求对内部状态进行不同的解释,oop,design-patterns,ooad,Oop,Design Patterns,Ooad,具有某些私有内部状态的模型对象。此状态的组件将向客户端公开。但其中一个客户端希望公开内部状态的不同组件。这应该如何处理?一个例子 public GarageModel { private Vehicle car; private Vehicle truck; public Vehicle getMostInterestingVehicle() { //exposes car as the most interesting vehicle but

具有某些私有内部状态的模型对象。此状态的组件将向客户端公开。但其中一个客户端希望公开内部状态的不同组件。这应该如何处理?一个例子

public GarageModel {
    private Vehicle car;
    private Vehicle truck;

    public Vehicle getMostInterestingVehicle() {
        //exposes car as the most interesting vehicle but
        //one of the client wants this to return a truck
        return car;
    }
}

您可以为方法提供参数,这些参数将定义您的客户看到最有趣的车辆的标准

public Vehicle getMostInterestingVehicleByCriteria(VehicleCriteria vehicleCriteria){
    // logic which selects correct vehicle in simple way it will be just
    if(vehicleCriteria.getMostInterestingVehicleType().equals(VehicleType.TRUCK){
        //do your logic
    }
    // In case of multiple objects you should refactor it with simple inheritance/polymorphism or maybe use some structural pattern 
}

public class VehicleCriteria{
    VehicleType mostInterestingVehicle; // enum with desired vehicle type

    public VehicleCriteria(VehicleType mostInterestingVehicle){
        this.mostInterestingVehicle = mostInterestingVehicle;
    }
}

如果客户机知道它想要什么类型,那么让客户机用一个通用参数(C#假定)这样说:

public T getMostInterestingVehicle(),其中T:Vehicle{}
然后你可以使用一本“东西”字典(可能是工厂?),根据返回的类型键入车辆。这可能是在构建时创建的静态集合,也可能是由IoC解决的静态集合:

private Dictionary<T, Vehicle> _things;
private Dictionary\u事物;
然后,您可以使用它来完成以下工作:

public T getMostInterestingVehicle<T>() where T : Vehicle 
{ 
    FactoryThing thing;

    if (_things.TryGetValue(T, out thing))
    {
        return thing.GetVehicle();
    }
}
public T getMostInterestingVehicle(),其中T:Vehicle
{ 
工厂事物;
if(_things.TryGetValue(T,out thing))
{
return thing.GetVehicle();
}
}

抱歉,如果您使用的不是C,并且语法/用法不正确,但我想您会明白我的意思…

很难说,鉴于您的示例,您可以为每个不同的客户机在
GarageModel
类上应用策略模式,并覆盖该单一方法以满足他们的每个需求。但是,只有当您可以为客户提供不同的车库模型时,这才有效

多态性一直是我的一位老师常说的答案

例如

public TruckGarageModel: GarageModel {
    public override Vehicle getMostInterestingVehicle(){
        return truck;
    }
}

public CarGarageModel: GarageModel {
    public override Vehicle getMostInterestingVehicle(){
        return car;
    }
}

你会把你的< GarageModel >代码>的适当修饰版本传递给每一个不同的客户

你可能需要考虑很多事情然后才决定实现,有些问题是-/p>
  • 您是否希望以后添加车辆的更新版本?:如果是,您可能需要提供一个工厂,该工厂可以注册更新的类型,而无需您在工厂中反复进行更改。提供了一个示例。这样你就避免了很多,如果不是的话
  • 您想在运行时决定返回哪种车辆吗?:然后如其他答案中所指出的那样提供帮助

希望这能有所帮助。

为什么不使用不同的构建标志,并将实现置于
\ifdef CUSTUMER\u构建下#否则#endif
谢谢,这种方法的一个问题是,当模型的内部状态不仅包含两种而且包含许多类型的车辆时,我们最终会为每种类型编写if、else语句。您可以通过继承、多态性或某种结构模式来克服这一问题。在两个物体的情况下,两种解决方案都可能是正确的。这个答案似乎是试图说明这个问题。当模型的内部状态包含许多车辆时,可以将它们收集到列表中。
VehicleCriteria
过滤器只是在列表中迭代,直到找到匹配项。对
GarageModel
进行子分类是一个很好的答案,但子类与装饰器不同。Decorator模式更重要。您是对的,它应该传递原始实例作为Decorator。这更像是一种策略。当pcA子类也不是策略时,我将进行编辑。这是基本的多态性。不存在任何设计模式,也不需要任何设计模式。
public TruckGarageModel: GarageModel {
    public override Vehicle getMostInterestingVehicle(){
        return truck;
    }
}

public CarGarageModel: GarageModel {
    public override Vehicle getMostInterestingVehicle(){
        return car;
    }
}