lundi 28 novembre 2016

Open Closed Principle: Different ways / scenarios it can be applied

One of the most common ways of applying the OCP, is through abstractions and inheritance like below. Where the design is closed, but behavior can still be extended by adding new types of Shape abstraction without modifying existing behavior.

class GraphicEditor 
{
    public void drawShape(Shape s) 
    {
        s.draw();
    }
 }

 class Shape 
 {
    abstract void draw();
 }

 class Rectangle : Shape  
 {
    public void draw() 
    {
        ...
    }
 } 

What are the other different ways / scenarios that you are aware of (or have come across), of implementing the open close principle?

Aucun commentaire:

Enregistrer un commentaire