You are on page 1of 31

The Open/Closed Principle

Elias Fofanov

http://engineerspock.com
Outline

• Two definitions of the OCP


• Why OCP?
• Single Choice principle related to the OCP
• Classic example of OCP violation
• How to refactor code which violates the OCP
• “Template Method” and “Strategy” patterns
• Difference between abstract classes and interfaces
Problem Statement
Software entities should be open for
extension, but closed for modification.
We should be able to introduce a change
by adding new code, not by changing the
existing.
Polymorphism is the Answer.
Why OCP?

• There is a high chance of introducing bugs


during the modification process
• It’s hard to modify the behavior of an API
which is already in use by many clients
When customers ask for a new feature they think that features
will be added, they don’t think that developers will modify
anything.
We must modify the existing code if it contains a bug.
The Protected Variation pattern means the following:
Identify points of predicted variation and create a stable
interface around them.
Meyer’s definition is more about backward
compatibility at the API level.
Single Choice Principle

public class BankTerminalFactory


{
public static IBankTerminal CreateBankTerminal(BankTerminalModel model)
{
switch (model)
{
case BankTerminalModel.Brp:
return new BrpTerminal();
case BankTerminalModel.Dcp:
return new DcpTerminal();
default:
throw new ArgumentException("Unknown model");
}
}
}
Dependency Injection

• “Mastering Ninject for Dependency Injection” by Packt


• “Dependency Injection in .NET” by Mark Seemann
Whenever a software system must support a set of alternatives,
one and only one module in the system should know their
exhaustive list.
Next Video
OCP Violation Demo
We can’t achieve a super-supple design which allows to
introduce any possible features.
Development Methodologies

• Waterfall
- Try to foresee all the possible details
• Agile
- Iterative development process with high involvement of a customer
Next Video
Related Patterns
OCP-Related Patterns
OCP-Related Patterns

• Template Method
• Strategy
Template Method Definition

It defines the skeleton of an algorithm in an operation, deferring some steps


to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm
without changing the algorithm's structure.
Strategy Pattern
Strategy Definition

Strategy enables an algorithm's behavior to be selected at runtime.


The strategy pattern:
• defines a family of algorithms
• encapsulates each algorithm
• makes the algorithms interchangeable within that family
Interfaces

• Interfaces can’t be easily changed without breaking existing clients


• Interfaces are easily extendable by clients

Corollaries:
• An interface is suppler from the client’s perspective: any class can
implement as many interfaces as it wants to
• An interface is more rigid from the developer’s perspective: it can’t be
easily changed and it does not support any kind of reusability
Abstract Classes

• Supports reusability
• Supports encapsulation
• Can be extended easily without breaking existing clients
Corollaries:
• An abstract class is supple from the developer’s perspective
• An abstract class is rigid from the client’s perspective
Interfaces VS Abstract Classes

• Use abstract classes for building internal APIs


• Use interfaces for providing external points of extension

Corollaries:
• An abstract class is supple from the developer’s perspective
• An abstract class is rigid from the client’s perspective
Visitor Pattern
Common Smells

Many conditional branches with if\else or switch\case statements.


Adhering to OCP

• Parameterization with delegates. “Chain of Responsibility” design pattern.


• Classic Inheritance or “Visitor” design pattern
• Composition VS Inheritance. “Strategy” design pattern.
Next Video
Conclusion
Conclusion

• Design should be open for extensions and closed for modification


• Isolate a responsibility for creating objects in a single module
(Single Choice Principle)
• Related patterns: “Template Method” and “Strategy”
• Interface is suppler from the client’s perspective
Abstract class is suppler from the developer’s perspective
• To overcome the problem of predicting the future, we rely on “agile design”

You might also like