CHAPTER 8 – Design Principles

--Originally published at Newbie Programmer

The design principles are basic tools or techniques that can we apply to design our code, and make it maintainable, flexible or extensive as we seen in the first chapters with the object-oriented principles like encapsulation, implementation, behavior, interfaces, and in this chapter, we can find these four new design principles to improve the code.

First principle, The open-closed principle, this principle explain that we can made our code closed for modification but open for extension, because we don’t want to make modifications from other people because its working well, but we can open it for extension and the people who need this can make subclasses or override the methods.

Second principle, The don’t repeat yourself principle, this is helpful to make the things without redundancy or to write the same thing two times, and this help to maintain the code and reusable, for example if we have the method of a class that does the same thing that we want to do in another class can we make a reference to this method instead of making that again, and this maintains each functionality in a place.

Third principle, The single responsibility principle,  each object of the design only have a responsibility, and for example to know if we have more than one responsibility we can check with the name of the class, and if the name make sense with the responsibility assigned, for example in a car, we have the class Automobile that is responsible for start the engine, stop and to check that we have oil, but the responsibility for driving is part of the driver, in this case we need to make two classes Automobile and Driver.

Fourth principle, The liskov substitution principle, is to design a well inheritance, this means that we substitute a subclass from their base class, this principle is important to make the code clear, because it hard to understand code that don’t have good inheritance,  because a subclass gets all of the methods from the superclass and to avoid this we can use alternatives like Delegation, to pass the task to another class without changing the behavior of the class currently working, Composition, to choose more than one behavior from other class, because we have objects that can be a grouped and use the same functions, Aggregation, the different from composition is that in this case the object exist outside the other class, is to have the same things as composition but outside the object.