Design Patterns
Design patterns are proven solutions to common problems in software development. They are not rigid rules, but guidelines.
Classification
- Creational Patterns: Focus on how objects are created (e.g.,
Singleton,Factory). - Structural Patterns: How objects are organized and composed (e.g.,
Adapter,Composite). - Behavioral Patterns: How objects interact and communicate with each other.
State Pattern
Allows an object to change its behavior when its internal state changes. Ideal for avoiding many complex if/else statements.
Example: A music player with states Playing, Paused, Stopped.
Template Method Pattern
Defines the skeleton of an algorithm in a superclass, but lets subclasses override specific steps without changing the overall structure.
Example: A board game where all games have the steps start(), play() and end(), but each game implements them differently.