那么,这个解决方案遵守了开闭原则么?
在遵循开闭原则的过程中,我期望能够写出一个类或者方法,在以后我回过头读它的时候,会很舒服地看到它能完成它的工作并且我也不需要再修改它。
In following the Open Closed Principle, I want to be able to write a class or a method and then turn my back on it, comfortable that it does its job and I won't have to go back and change it.
如果在某一部分中,一个抽象了的类必须向下转换为特定的子类,那么你就没有遵循开闭原则。
If a class that uses an abstraction has to downcast to a specific subclass in one section, you're not following the Open Closed Principle.
我建议你将开闭原则作为一个设计方向而非一个完全的目标。
I would recommend that you treat the Open Closed Principle as a design vector rather than an outright goal.
这就是开闭原则一展身手的地方了。
通过在订单处理规则上遵循开闭原则,我使得在系统中添加新类型的订单处理逻辑容易得多。
By following the Open Closed Principle with the order processing rules, I've made it much easier to add new types of order handling logic to the system.
这违反了开闭原则。
最后,还有一种更靠近开闭原则的方式需要说明。
There is another way to move closer to the Open Closed Principle ideal that might be cleaner in the end.
你永远也达不到真正的开闭天堂,但是通过严格地遵循与之相关的单一责任原则:一个类应该有并且只有一个更改的理由,你可以非常靠近地接近它。
You'll never reach true Open Closed nirvana, but you can inch much closer by rigorously following the related Single Responsibility Principle: a class should have one, and only one, reason to change
如果你可以通过实现IDataSource对这个系统进行扩展并且不对BusinessProcess类做任何的修改,那么这个设计就遵循了开闭原则。
The design follows the Open Closed Principle if you can extend the system by swapping out implementations of IDataSource without making any change to the BusinessProcess class.
我喜欢以讨论开闭原则和其他由robertC.M artin在其著作《敏捷软件开发,原则,模式和实践》中所倡导的相关主题作为开始。
I'd like to start with a discussion of the Open Closed Principle and other related ideas popularized by Robert C. Martin in his book, Agile Software Development, Principles, Patterns, and Practices.
如果我前面所说的,使用开闭原则最通常的做法就是使用多态去用一个全新的类替换程序中现存的一部分。
As I said earlier, the most common manifestation of the Open Closed Principle is using polymorphism to substitute an existing part of the application with a brand new class.
你可能并非总是试图写一些在各个方面都满足开闭原则的代码,但是即使只进行到中途也是非常有益的。
You may not always manage to write code that satisfies the Open Closed Principle in every aspect, but even moving partway there can be beneficial.
你可能并非总是试图写一些在各个方面都满足开闭原则的代码,但是即使只进行到中途也是非常有益的。
You may not always manage to write code that satisfies the Open Closed Principle in every aspect, but even moving partway there can be beneficial.
应用推荐