Classes Flashcards
What is a class?
A Class is a code structure whose main responsibility is to map an object of a certain domain. Within a class scope, one can program behavior through methods.
What is the purpose of a class? Why would you make one?
Classes are implemented to provide a more organized, reusable, and clean code. Especially when you deal with applications with thousands of lines of code and business rules.
What principles would you recommend to design a class?
To maintain good object-oriented design practices, principles such as SOLID are recommended to guide developers on the creation and design of classes.
What is the SOLID principle?
When creating a new class it is important to design objects that are easily maintainable, usable, and can be extended.
Explain what the S letter in the SOLID principle stands for?
S = Single Responsibility Principle = Classes should have only one responsibility. Example: One class should not be responsible for validating business logic and saving data to the database
Explain what the O letter in the SOLID principle stands for?
O = Open/Closed Principle = The class object should be open for extension and closed for modification. Example: with the use of an interface, new objects can be included in the code without having to change the existing code.
Explain what the L letter in the SOLID principle stands for?
L = Liskov Substitution Principle = A base class should be replaceable with derived classes at any moment. Example: A dog can walk, however, an electric dog can only walk if it has batteries.
Explain what the I letter in the SOLID principle stands for?
I = Interface segregation Principle = An interface should have a single responsibility. Example: A person interface should not implement methods regarding animals.
Explain what the D letter in the SOLID principle stands for?
D = Dependency Inversion Principle = The use of classes should depend on abstractions(interfaces, base class) and not concrete classes. Example: The use of logging in an application should depend on an interface, because in the future one can change the log framework easily.