OOP design
All principles
-
KISS → Start simple, add complexity only when needed
-
DRY → Reduce duplication, simplify maintenance
-
YAGNI → Build for today, not hypothetical futures
-
Separation of Concerns → Enable independent testing and changes
-
Law of Demeter → Reduce coupling, hide internal structure
-
SRP → Keep classes focused on one responsibility
-
OCP → Support future requirements without modifying existing code
-
LSP → Prevent brittle hierarchies that break at runtime
-
ISP → Keep interfaces clean and focused
-
DIP → Keep code flexible and testable through abstraction
SOLID
1. Single:
This states that there should never be more than one reason for a class to change. In other words, every class should have only one responsibility.
- Maintainability: When classes have a single, well-defined responsibility, they're easier to understand and modify.
- Testability: It's easier to write unit tests for classes with a single focus.
- Flexibility: Changes to one responsibility don't affect unrelated parts of the system.
2.Open–closed principle
The open–closed principle (OCP) states that software entities should be open for extension, but closed for modification.- Extensibility: New features can be added without modifying existing code.
- Stability: Reduces the risk of introducing bugs when making changes.
- Flexibility: Adapts to changing requirements more easily.
3 Liskov substitution principle
The Liskov substitution principle (LSP) states that functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
4 Interface segregation principle
The interface segregation principle (ISP) states that clients should not be forced to depend upon interfaces that they do not use.
Importance:
- Decoupling: Reduces dependencies between classes, making the code more modular and maintainable.
- Flexibility: Allows for more targeted implementations of interfaces.
- Avoids unnecessary dependencies: Clients don't have to depend on methods they don't use.
5 Dependency inversion principle
The dependency inversion principle (DIP) states to depend upon abstractions, not concretes.
Importance:
- Loose coupling: Reduces dependencies between modules, making the code more flexible and easier to test.
- Flexibility: Enables changes to implementations without affecting clients.
- Maintainability: Makes code easier to understand and modify.
SOLID原则的好处
-
易于维护 - 修改一个功能不影响其他部分
-
易于测试 - 可以单独测试每个模块
-
易于扩展 - 添加新功能只需添加新类
-
减少耦合 - 模块之间依赖最小化
-
提高复用 - 小模块可以在多个地方使用
SOLID原则不是必须严格遵循的规则,而是指导我们创建更灵活、可维护软件的设计指南。
Delivery Framework
1 Requirements (~5 minutes)
-
Primary capabilities — What operations must this system support?
-
Rules and completion — What conditions define success, failure, or when the system stops or transitions state?
-
Error handling — How should the system respond when inputs or actions are invalid?
-
Scope boundaries — What areas are in scope (core logic, business rules) and what areas are explicitly out (UI, storage, networking, concurrency, extensibility)?
2 Entities and Relationships (~5 minutes)
Don't overthink this. A simple list of entities and a few arrows showing which objects own or use which others is more than enough.
1) Identify Entities
-
Which entity is the orchestrator — the one driving the main workflow?
-
Which entities own durable state?
-
How do they depend on each other? (has-a, uses, contains)
-
Where should specific rules logically live?
3 Class Design (~10-15 minutes)
-
State - What does this class need to remember to enforce the requirements?
-
Behavior - What does this class need to do, in terms of operations or queries?
-
Which parts of the requirements does this entity own?
-
What information does it need to keep in memory to satisfy those responsibilities?
2) Deriving Behavior from Requirements
Once state is clear, move on to behavior. For each class, ask what operations the outside world needs and which requirements those operations satisfy. You're aiming for a small, focused API where each method corresponds to a real action or question implied by the problem.
4 Implementation (~10 minutes)
-
Happy Path - Walk through the method in a linear way: what inputs it receives, the sequence of steps it performs, the internal calls it makes to other classes, and what it returns or how it changes state. You want your interviewer to see how the system actually moves.
-
Edge Cases - After the happy path, enumerate the failure modes: invalid inputs, illegal operations, out-of-range values, calls that violate the current system state—anything that must be rejected or handled gracefully. This is an important step to demonstrate that you're thinking like someone who writes production code, not just toy logic.
5 Extensibility (~5 minutes, if time and level allow)
General Software Design Principles
1) KISS - Keep It Simple, Stupid:
2) DRY - Don't Repeat Yourself: When you find yourself writing the same logic in multiple places, pull it into one place.
Separation of Concerns
Different parts of your code should handle different responsibilities, and they shouldn't know about each other's internals.
Law of Demeter

浙公网安备 33010602011771号