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 6355389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:45:21+00:00 2026-05-24T22:45:21+00:00

I have been looking into the open closed principle and it sounds good and

  • 0

I have been looking into the open closed principle and it sounds good and so I wanted to exercise it’s teachings.
I looked at applying my new found knowledge to an existing project and have become a little stuck right away.

If a new UserType comes along (And this is Very likely), this will need to be changed, it is not yet closed to modification. How could one get round this?

From what I have read, it sounds like I should be implementing a factory here instead of applying the OCP?

Factory which breaks Open-closed principle

 private void BuildUserTree(User user)
    {
        switch (user.UserType)
        {
            case UserType.FreeLoader:
                BuildFreeLoaderTree();
                break;
            case UserType.Premium:
                BuildPremiumTree();
                break;
            case UserType.Unlimited:
                BuildUnlimitedTree();
                break;
            default:
                throw new Exception("No UserType set");

        }
    }

Thanks,
Kohan

  • 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-24T22:45:22+00:00Added an answer on May 24, 2026 at 10:45 pm

    Like any ‘principle’ OCP is not a rule that you must obey on all occasions.

    We’re told to ‘Favour composition over inheritance’, and yet patterns like decorator and composite openly promote inheritance.

    Similarly, we are told to ‘Program to an interface, not an implemenation, and yet, at some point in our application we’re going to have to instantiate a concrete object of some description.

    Your solution is a classic factory idiom (if not quite the full factory method or abstract factory pattern). This is what it is intended to do. Trying to apply OCP to it doesn’t make sense.

    In fact, by creating this method, you may actually facilitate OCP in some other part of your code-base. Some other class or classes in your app could now obey OCP now you have separated the creation.

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

Sidebar

Related Questions

I have been looking into Open GL ES, Quartz 2D, a framework called cocos2D
I have been looking into AWS spot instances for some jobs however instead of
I have been looking into backbone.js and I can't seem to figure out how
I have been looking into CruiseControl configuration recently (I'm a complete CC noob) and
I have been looking into different systems for creating a fast cache in a
I have been looking into MVVM design patterns with WPF for a project. I
I have been looking into HTML5 manifest but I am unclear as to whether
I have been looking into coloring objects like ellipses with code such as SolidBrush
I have been looking into packages that would enable me to have tabs in
I have been looking into integrating testing into my app based on RequireJS. I

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.