What is the meaning and reasoning behind the Open/

2019-01-10 09:31发布

The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for extension, but closed for modification. What does this mean, and why is it an important principle of good object-oriented design?

11条回答
姐就是有狂的资本
2楼-- · 2019-01-10 10:26

Specifically, it is about a "Holy Grail" of design in OOP of making an entity extensible enough (through its individual design or through its participation in the architecture) to support future unforseen changes without rewriting its code (and sometimes even without re-compiling **).

Some ways to do this include Polymorphism/Inheritance, Composition, Inversion of Control (a.k.a. DIP), Aspect-Oriented Programming, Patterns such as Strategy, Visitor, Template Method, and many other principles, patterns, and techniques of OOAD.

** See the 6 "package principles", REP, CCP, CRP, ADP, SDP, SAP

查看更多
聊天终结者
3楼-- · 2019-01-10 10:26

In Design principle, SOLID – the "O" in "SOLID" stands for the open/closed principle.

Open Closed principle is a design principle which says that a class, modules and functions should be open for extension but closed for modification.

This principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code (tested code). The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.

Benefit of Open Closed Design Principle:

  1. Application will be more robust because we are not changing already tested class.
  2. Flexible because we can easily accommodate new requirements.
  3. Easy to test and less error prone.

My blog post on this:

http://javaexplorer03.blogspot.in/2016/12/open-closed-design-principle.html

查看更多
迷人小祖宗
4楼-- · 2019-01-10 10:30

An additional rule of thumb for conforming to OCP is to make base classes abstract with respect to functionality provided by derived classes. Or as Scott Meyers says 'Make Non-leaf classes abstract'.

This means having unimplemented methods in the base class and only implement these methods in classes which themselves have no sub classes. Then the client of the base class cannot rely on a particular implementation in the base class since there is none.

查看更多
不美不萌又怎样
5楼-- · 2019-01-10 10:36

More specifically than DaveK, it usually means that if you want to add additional functionality, or change the functionality of a class, create a subclass instead of changing the original. This way, anyone using the parent class does not have to worry about it changing later on. Basically, it's all about backwards compatibility.

Another really important principle of object-oriented design is loose coupling through a method interface. If the change you want to make does not affect the existing interface, it really is pretty safe to change. For example, to make an algorithm more efficient. Object-oriented principles need to be tempered by common sense too :)

查看更多
走好不送
6楼-- · 2019-01-10 10:36

The principle means that it should easy to add new functionality without having to change existing, stable, and tested functionality, saving both time and money.

Often, polymorhism, for instance using interfaces, is a good tool for achieving this.

查看更多
登录 后发表回答