User Tools

Site Tools


principles:open-closed_principle

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
principles:open-closed_principle [2013-01-24 12:46] – external edit 127.0.0.1principles:open-closed_principle [2022-03-02 23:55] (current) – [Principle Statement] tory
Line 1: Line 1:
-====== Open-closed Principle [see ECV] ======+====== Open-Closed Principle (OCP) ====== 
 + 
 +===== Variants and Alternative Names ===== 
 + 
 + 
 +===== Context ===== 
 +/* fill in contexts here: */ 
 +  * [[contexts:Object-Oriented Design]] 
 +  * [[contexts:API Design]] 
 + 
 + 
 + 
 +===== Principle Statement ===== 
 + 
 +Modules should be open for extension but closed for modification. 
 + 
 + 
 +===== Description ===== 
 + 
 + 
 +===== Rationale ===== 
 + 
 + 
 +===== Strategies ===== 
 + 
 + 
 +===== Caveats ===== 
 + 
 +Beware that wrong application of OCP may lead to the anti-pattern [[anti-patterns:onion]]. 
 + 
 +See also section [[#contrary principles]]. 
 + 
 + 
 +===== Origin ===== 
 + 
 +Bertrand Meyer: //[[http://en.wikipedia.org/wiki/Object-Oriented%20Software%20Construction|Object-Oriented Software Construction]]//, p. 57pp. 
 + 
 +===== Evidence ===== 
 +/* Comment out what is not applicable and explain the rest: */ 
 +/* 
 +  * [[wiki:Proposed]] 
 +  * [[wiki:Examined]] 
 +  * [[wiki:Accepted]] 
 +  * [[wiki:Questioned]] 
 +*/ 
 + 
 +===== Relations to Other Principles ===== 
 + 
 +==== Generalizations ==== 
 + 
 +  * [[Encapsulate the Concept that Varies]] (ECV): The OCP demands encapsulating abstract concepts in base classes (or interfaces) in order to be able to enhance the module by subclassing which is possible without changing the previously written code. In this case several variations of a concept may exist in the code at the same time. There is always the abstract base class plus one or usually more concrete subclasses. So the OCP is about encapsulating abstract concepts that vary "in space"
 + 
 +==== Specializations ==== 
 + 
 +==== Contrary Principles ==== 
 + 
 +  * [[more Is More Complex]] (MIMC): The OCP demands introducing abstract base classes or interfaces. 
 +  * [[Keep It Simple Stupid]] (KISS): The OCP demands introducing abstract base classes or interfaces. This increases complexity. 
 +  * [[Model Principle]] (MP): OCP sometimes results in the introduction of artificial classes that do not correspond to a real-world concept. 
 + 
 +==== Complementary Principles ==== 
 + 
 +  * [[Dependency Inversion Principle]] (DIP): OCP results in the introduction of abstract classes or interfaces and descendant concrete classes. DIP now tells that other classes should only depend on the abstractions. 
 +  * [[Liskov Substitution Principle]] (LSP): OCP may results in the introduction of abstract classes or interfaces. Here it is important to get the abstraction right. Otherwise LSP may be violated. 
 + 
 +==== Principle Collections ==== 
 + 
 +{{page>collections:SOLID#Box}} 
 +{{page>collections:Principles in "Object-Oriented Software Construction"#Box}} 
 + 
 +===== Examples ===== 
 + 
 +===Client Repository=== 
 +Let's say the high-level module (your business logic), wants to be able to add or remove clients to the database. Instead of it talking to the database directly, it defines an interface called ClientRepository which contains the methods the business logic needs ([[dependency_inversion_principle|DIP]]). Now you go along and implement a MySQLClientRepository. Some time in the future, you are asked to switch to an oracle database. You can now, without modifying any code from your business logic, switch to the oracle database: by extending ClientRepository to implement OracleClientRepository. You just need to wire an OracleClientRepository instance to the business logic and you have made the switch without modifying any business logic code. 
 +===== Description Status ===== 
 +/* Choose one of the following and comment out the rest: */ 
 +[[wiki:Stub]] 
 +/*[[wiki:Incomplete]]*/ 
 +/*[[wiki:Complete]]*/ 
 + 
 +===== Further Reading ===== 
 + 
 +  * Robert C. Martin: //Agile Software Development, Principles, Patterns, and Practices//, p. 99pp. 
 +  * [[http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod|ButUncleBob: Principles of OOD]] 
 + 
 +===== Discussion ===== 
 + 
 +Discuss this wiki article and the principle on the corresponding [[talk:principles:Open-Closed Principle|talk page]].
  
-~~REDIRECT>principles:Encapsulate the Concept that Varies~~ 
principles/open-closed_principle.1359027973.txt.gz · Last modified: 2013-05-19 22:10 (external edit)