5

Let's say in first version of my hypothetical software I have a simple class like this:

public Class Version1
{
    public void Method1()
    {
       Console.WriteLine("Hello");
    }
}

In second version, I have an upgrade that requires the Method 1 to be modified like this:

public Class Version1
{
    public void Method1()
    {
       Console.WriteLine("Hello");
       Console.WriteLine("World");
    }
}

And in third version, I have an upgrade which requires adding another method to this class like this:

public Class Version1
{
    public void Method1()
    {
       Console.WriteLine("Hello");
       Console.WriteLine("World");
    }

    public int Method2()
    {
        return 7;
    }    
}

Now, to my understanding of Open-Closed principle, in both upgrades, I have violated this principle because I modified the class that was doing desired work in the first version of my software.

The way I think it should be done, but not sure if correct, is like this:

public virtual Class Version1
{
    public virtual void Method1()
    {
       Console.WriteLine("Hello");
    }
}

public virtual Class Version2 : Version1
{
    public override void Method1()
    {
       Console.WriteLine("Hello");
       Console.WriteLine("World");
    }
}  

public Class Version3 : Version2
{      
    public int Method2()
    {
        return 7;
    }

}

How wrong/right is this?

spender
  • 117,338
  • 33
  • 229
  • 351
Ivan
  • 1,081
  • 2
  • 17
  • 43

1 Answers1

4

Yes, both methods violate the principle.

The first method changes the meaning of Method1, not only showing Hello any more. Your second method extends the first version, which is allowed, but you should have used inheritance to extend the functionality.

There are multiple ways to achieve the 'being closed or open'. Some use inheritance to allow modifications on an entity, others use interfaces. If you'd use interfaces, the second method could also been seen as a violation since it would require a refinement of the interface definition.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    Adding a method to the original class counts as modication in my opinion, not only for interfaces like you say – Alexander Derck Oct 24 '16 at 13:29
  • 1
    Well, it doesn't need existing code depending on it to be revised, so it is merely an extension, right? – Patrick Hofman Oct 24 '16 at 13:30
  • Yes, this is the part that also confuses me the most in the whole story...because the way people interpret this principle is that the calss should not change, but adding another method to the class in my opinion counts as modification. – Ivan Oct 24 '16 at 13:31
  • 1
    @AlexanderDerck "But it is also open, since any new class may use it as parent, adding new features. When a descendant class is defined, there is no need to change the original or to disturb its clients." – Patrick Hofman Oct 24 '16 at 13:31
  • 1
    Yeah, that's what I mean, if you inherit from it and you add the extra method, the base class wasn't changed -> "there is no need to change the original". So strictly speaking I think adding it to the base class is a violation of the o/c principle – Alexander Derck Oct 24 '16 at 13:36
  • 1
    @AlexanderDerck Sorry, you are totally right. Updated. – Patrick Hofman Oct 24 '16 at 13:38