Search code examples
oopdesign-patternsarchitecturesolid-principles

Are there SOLID principle exceptions?


I try to apply SOLID principles in my project's class design. Are there any exceptions of SOLID principles? Do we HAVE to apply these principle DEFINITELY. For example I prepared a factory class.

class XAdderFactory
{
    private Person _person;

    public bool PersonHasNoRecords
    {
        get
        {
            return string.IsNullOrEmpty(_person.HasXRecords);
        }
    }

    public XAdderFactory(Person person)
    {
        this._person = person;
        if (PersonHasNoRecords)
        {
            new XListMakerAFactory(person);
        }
        else
        {
            new XListMakerB(person);
        }
    }
}

This class never conforms to the OCP.
New type list makers may be required in the future and I must add a new else if block.
Is my design bad?
Or are there exceptions of SOLID principles that are not mentioned too often?
I am not sure but my example complies with "Strategic Closure" of OCP? If you have another examples about SOLID exceptions,i think it would be helpful for designers.


Solution

  • The Open-Closed principle is important and useful, but it is not something that should be applied blindly to all classes and all modules. In some cases, creating the abstractions that enable extensibility is just not worth it, because no extensions are expected. In other cases, we can anticipate that requirements will change, but we're not sure what kind of changes to expect, or we're not sure about the other requirements, so we prefer to postpone the decision, and begin with a simpler module implementation that does not respect OCP.