단일 책임과 개방-폐쇄 원칙 완벽 이해
SOLID의 처음 두 원칙을 깊이 익히고, 책임의 경계를 식별하며 기존 코드를 수정하지 않고 동작을 확장하는 방법을 배우세요.
단일 책임과 개방-폐쇄 원칙 완벽 이해은(는) CoddyKit의 무료 Clean Architecture & Design Patterns in Practice 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Clean Architecture & Design Patterns in Practice 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Clean Architecture & Design Patterns in Practice 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Back to the Foundations
You have explored Dependency Inversion and Interface Segregation. This lesson masters the remaining pair:
- Single Responsibility Principle (SRP)
- Open-Closed Principle (OCP)
These two drive most everyday refactoring decisions.
SRP Defined Precisely
SRP says a class should have one reason to change. A reason to change maps to a single actor or stakeholder.
If billing rules and report formatting can change independently, they belong in different classes.
Spotting an SRP Violation
This class mixes calculation, persistence, and presentation.
class Employee {
double calculatePay() { return 0; }
void save() { /* DB code */ }
String reportHtml() { return "<html>"; }
}Refactoring Toward SRP
Split responsibilities so each changes for one reason.
class PayCalculator { double calculate(Employee e) { return 0; } }
class EmployeeRepository { void save(Employee e) {} }
class EmployeeReporter { String html(Employee e) { return "<html>"; } }The Cohesion Payoff
After the split, each class is more cohesive: everything inside relates to one job.
Changes are localized, tests are focused, and accidental coupling between unrelated concerns disappears.
OCP Defined
The Open-Closed Principle: software entities should be open for extension but closed for modification.
You should be able to add new behavior by writing new code, not editing existing, tested code.
An OCP Violation
Adding a shape forces editing this method every time.
double area(Shape s) {
if (s.type.equals("circle")) return 3.14 * s.r * s.r;
else if (s.type.equals("square")) return s.side * s.side;
return 0;
}Closing It With Polymorphism
Make each shape compute its own area. New shapes require no edits to existing code.
interface Shape { double area(); }
class Circle implements Shape {
double r;
public double area() { return 3.14 * r * r; }
}
class Square implements Shape {
double side;
public double area() { return side * side; }
}OCP Through Strategy and Plugins
Common OCP-enabling techniques:
- Polymorphism over conditionals.
- The Strategy pattern to inject varying behavior.
- Plugin or registry mechanisms for adding handlers.
All let you extend by adding, not editing.
How SRP and OCP Reinforce Each Other
A class with a single responsibility is much easier to keep closed for modification, because there is only one axis of change.
When you cleanly separate responsibilities, extension points emerge naturally.
Pragmatic Limits
Do not over-apply. Premature abstraction for variation that never comes adds needless complexity.
Apply OCP at the points your domain actually varies; let the rest stay simple until change demands it.
Quick Check
Test your grasp of SRP and OCP.
Recap
You mastered the first two SOLID principles.
- SRP: one reason to change per class.
- OCP: extend by adding, not editing.
- They reinforce each other and guide most refactorings, applied where variation truly exists.
자주 묻는 질문
“단일 책임과 개방-폐쇄 원칙 완벽 이해” 강의는 무료인가요?
네 — “단일 책임과 개방-폐쇄 원칙 완벽 이해” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Clean Architecture & Design Patterns in Practice 강의 전체를 잠금 해제할 수 있습니다. Clean Architecture & Design Patterns in Practice 강의에는 총 4개의 강의가 포함되어 있습니다.
“단일 책임과 개방-폐쇄 원칙 완벽 이해”에서 뭘 배우나요?
SOLID의 처음 두 원칙을 깊이 익히고, 책임의 경계를 식별하며 기존 코드를 수정하지 않고 동작을 확장하는 방법을 배우세요. 브라우저에서 직접 실행하는 실습 코드로 Clean Architecture & Design Patterns in Practice을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Clean Architecture & Design Patterns in Practice을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Clean Architecture & Design Patterns in Practice은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“단일 책임과 개방-폐쇄 원칙 완벽 이해” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Clean Architecture & Design Patterns in Practice 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Clean Architecture & Design Patterns in Practice 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 의존성 역전 심층 학습
- 인터페이스 분리 원칙 실습
- 디자인 패턴을 활용한 리팩터링
- 단일 책임과 개방-폐쇄 원칙 완벽 이해