简体   繁体   English

我需要如何更改我的程序以使用打开/关闭原则?

[英]How do I need to change my program to use Open/close Principle?

What is the best way to rewrite this code so that the open/close principle is used?重写此代码以使用打开/关闭原则的最佳方法是什么? It's a method of my class:这是我的 class 的一种方法:

void Entity::insertPropertyIfExists(std::string& name, std::string& value)
{
    else if (name == "Age")
        set_property("Age", std::make_shared<Age>(std::atoi(value.c_str())));
    else if (name == "City")
        set_property("City", std::make_shared<City>(value));
    //...
    else if (name == "Interests")
        set_property("Interests", std::make_shared<InterestList>(value));
}

And this is an example of a class for which you need to implement the open/close principle这是一个 class 示例,您需要为其实现打开/关闭原则

class Property {
public:
    virtual int compare(Property& p) = 0;
    virtual ~Property() = default;
};

class City final : public Property {
public:
    City(std::string new_city);
    int compare(Property& p) override;
private:
    std::string city;
};

Please, show me some example on my code.请给我看一些关于我的代码的例子。

This question is not specific to c++, it would be better to be tagged as design question.这个问题不是c++特有的,最好标记为设计问题。

Nevertheless, regarding the question itself.然而,关于问题本身。 Open/Close principal suggests that our design will be easy to extend but close modification. Open/Close 原则表明我们的设计将易于扩展,但可以密切修改。 In other words, you want to extend your code functionality without effecting exiting functionality and code.换句话说,您希望在不影响现有功能和代码的情况下扩展您的代码功能。

A good question to ask before suggesting modifications, is what is expected to be changed?在提出修改建议之前要问一个好问题,预计会改变什么? Then, we will make sure our design encapsulates the parts the is changing/extending.然后,我们将确保我们的设计封装了正在更改/扩展的部分。

As stated above by @Mahesh Attarde, it seems that you want to make sure you can add new "property" without effecting existing properties functionality.正如@Mahesh Attarde 所述,您似乎想确保可以在不影响现有属性功能的情况下添加新的“属性”。

using factory method is indeed the right direction, for each string you should have a factory method that creates the proper property, that why when you want to add new property you don't need to change existing factory methods, but create a new one.使用工厂方法确实是正确的方向,对于每个字符串,您应该有一个创建正确属性的工厂方法,这就是为什么当您想要添加新属性时,您不需要更改现有的工厂方法,而是创建一个新的。

In my opinion, there is no going around the fact that some "HUB" will be needed to direct to the proper factory method according to the string identifier.在我看来,需要一些“HUB”来根据字符串标识符定向到正确的工厂方法这一事实是无可避免的。 However, you can make the code more closed to modification by using a "registration" method.但是,您可以使用“注册”方法使代码更接近于修改。 Meaning registering string id and matching factory dynamically in some structure so that insertPropertyIfExists doesn't need to change for every new property.这意味着在某些结构中动态注册字符串 id 和匹配工厂,以便 insertPropertyIfExists 不需要为每个新属性进行更改。 Still you will need to add registration for every new property, but that can be done in the "main" context, so that "entity" code is not effected and doesn't need even recompilation.您仍然需要为每个新属性添加注册,但这可以在“主”上下文中完成,这样“实体”代码不会受到影响,甚至不需要重新编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM