SOLID Class Design
This document organizes SOLID, which presents five principles when designing Classes.
1. SOLID
SOLID is a term that presents five principles when designing Classes in object-oriented programming. The term SOLID was created by taking the initials of Single Responsibility, Open/closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.
1.1. Single Responsibility
A single Class has a single responsibility. That is, there should be only one reason for a Class to change.
| |
Text Class has two responsibilities: changing Text and outputting Text.
| |
By defining Printer Class and delegating output responsibility that Text Class had to Printer Class, Text Class and Printer Class can each have only one responsibility.
1.2. Open/closed
This principle states that it should be open for extension but closed for modification of existing Classes. That is, it means that new functionality should be freely addable while minimizing Class changes.
| |
ClaimApprovalManager Class has the disadvantage that a Method for ClaimApprovalManager must be added for each Surveyor Class whenever Surveyor Class is added.
| |
ClaimApprovalManager can now accommodate various Surveyor Classes without code changes through InsuranceSurveyor Interface.
1.3. Liskov Substitution
This principle states that Subclasses must always be able to replace their Superclasses. That is, it means that Superclass’s Method functionality should not be arbitrarily changed or modified to cause errors in Subclasses.
| |
Since a square is also a rectangle, Square Class was implemented by inheriting Rectangle Class. In Rectangle Class, Width and Height could be set separately, but in Square Class, Width and Height are set to the same value simultaneously. Therefore, this is a Class design that violates the Liskov Substitution principle.
1.4. Interface Segregation
This principle states that when configuring Classes using Interfaces, Interfaces should not be made to define methods unnecessary for Class configuration. That is, it means to split Interfaces into small functional units and have Classes select and implement necessary Interfaces.
| |
Toy Interface of [Code 6] defines three types of methods: color, movement, and flight. The problem is that since not all toys have movement and flight functions, move and fly Methods of Toy Classes without movement and flight functions become dummy Methods.
| |
Toy Interface was separated to create Movable and Flyable Interfaces. When configuring Toy Classes, only necessary Interfaces can be selected and configured.
1.5. Dependency Inversion
This principle states that dependencies between Classes should maintain loose relationships through Interfaces. When Instance A references Instance B through Interface B, Instance A depends on Instance B without knowing exactly what action Instance B performs. This term Dependency Inversion is used because the called Instance determines the action of the calling Instance.
| |
ElectricSwitch Class directly references and uses LightBulb Class. ElectricSwitch Class must continue to change whenever new electronic products are added.
| |
ElectricSwitch Class depends only on Switchable Class. And the action of Switchable Class differs depending on whether LightBulb or Fan is injected into Switchable Class.