Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 412617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:06:10+00:00 2026-05-12T18:06:10+00:00

OCP (Open/Closed Principle) is one of the SOLID principles. Which is says: Software Entities

  • 0

OCP (Open/Closed Principle) is one of the SOLID principles. Which is says:

”Software Entities should be Open for Extension, but Closed for Modification.”

It take me while to understand the above sentence about OCP. And when I start read more about it, I found it make sense and so useful, but in the mean time I noticed it cause duplicated code.

How such an important principle “OCP” will be the reason of massive code duplication practice?

namespace SOLIDPrinciples
 {  
    public class ReportFormatter {
        public virtual void FormatReport() {
            Console.WriteLine("Formatting Report for 8-1/2 X 11 ....");
        }
    }

    public class TabloidReportFormatter : ReportFormatter {
        public override void FormatReport() {
            Console.WriteLine("Formatting Report for 11 X 17 ....");
        }   
    }
 }

Am I missing something here? Is there another way for OCP to be explained?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:06:11+00:00Added an answer on May 12, 2026 at 6:06 pm

    Code duplication refers to the significant repeating of blocks, statements, or even groupings of common member declarations. It doesn’t refer to the repeating of language keywords, identifiers, patterns, etc. You wouldn’t be able to achieve polymorphism otherwise.

    The example you provide doesn’t really demonstrate the Open/Closed Principle because you haven’t demonstrated how a given class’s behavior can be extended without modification. The Open/Closed principle is about creating new classes each time variant behavior is desired. This can be achieved using inheritance along with the Template Method pattern (i.e. calling abstract or virtual methods in a base class which are overridden in subclasses to achieve the desired/new behavior), but it’s more often demonstrated using composition using the Strategy pattern (i.e. encapsulating the variant behavior in class and passing it to the closed type to be used in determining the overall behavior achieved).

    From your example, it appears you were thinking more along the lines of trying to achieving OCP through inheritance, but starting with a base report formatter to establish the interface with subtypes to indicate different types of formats for a given report is actually a good start toward showing OCP through composition. What’s needed to demonstrate OCP with a composition-based approach is a class which consumes the formatters … say a ReportWriter.

    public class ReportWriter : IReportWriter
    {
        Write(IReportData data, IReportFormatter reportFormatter)
        {
             // ...
    
             var formattedStream = reportFormatter.Format(data, stream);
    
            // ...
        }
    }
    

    Just for the sake of demonstration, I’ve made some assumptions about how our new formatter might behave, so don’t focus too heavily on that part. The thing to focus on is that ReportWriter is open for extension by allowing new formatters to be passed in, thus affecting how reports are ultimately written, but closed for modification of code needed to achieve this new behavior (i.e. the presence of if statements, switch statements, etc. to achieve multiple ways to format).

    Not only does the Open/Closed principle not cause code duplication, when achieved through the use of composition over inheritance it actually can help reduce duplication. If true code duplication occurs in the course of creating your inheritance hierarchy or strategy classes then that points to a factoring issue unrelated to the fact that it might exist in the context of you trying to achieve OCP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been trying to learn about basic design principles and the OCP
Fragile base class is one of the most common point that gets popped up
I have a couple of ideas but I wanted to see what the SO
I have a main object which has some properties and methods. This object can
Using Visual Studio 2010 .Net Framework 4 C# Linq to Entities Issue I would
I'd like to: Make commonly required services visible to all classes that need them,
I am a developer having 4 years of .Net coding experience, And never cared
If you're in a team and a programmer gives you an interface with create,
I need to analyze 1 TB+ of web access logs, and in particular I
This small SQL error is bugging me. It doesn't seem to be a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.