8.2 KiB
Design Patterns Implementation
中文版 README | This repository contains implementations of the 23 classic Gang of Four (GoF) design patterns in three different programming languages: Go, TypeScript, and Python. Each pattern is implemented with clear examples and explanations to help developers understand and apply these fundamental design principles.
Table of Contents
- Introduction
- Design Patterns Overview
- Project Structure
- Getting Started
- Pattern Categories
- Contributing
- License
Introduction
Design patterns are typical solutions to commonly occurring problems in software design. They represent best practices that seasoned object-oriented software developers have used in their work for decades. This repository provides practical implementations of these patterns in three popular programming languages to demonstrate their usage and benefits.
Design Patterns Overview
This repository includes implementations of all 23 Gang of Four design patterns:
Creational Patterns
- Abstract Factory: Provides an interface for creating families of related objects
- Builder: Separates the construction of a complex object from its representation
- Factory Method: Defines an interface for creating an object, but lets subclasses alter the type of objects that will be created
- Prototype: Specifies the kinds of objects to create using a prototypical instance
- Singleton: Ensures a class has only one instance and provides a global point of access to it
Structural Patterns
- Adapter: Converts the interface of a class into another interface clients expect
- Bridge: Decouples an abstraction from its implementation so that the two can vary independently
- Composite: Composes objects into tree structures to represent part-whole hierarchies
- Decorator: Attaches additional responsibilities to an object dynamically
- Facade: Provides a unified interface to a set of interfaces in a subsystem
- Flyweight: Uses sharing to support large numbers of fine-grained objects efficiently
- Proxy: Provides a surrogate or placeholder for another object to control access to it
Behavioral Patterns
- Chain of Responsibility: Passes a request along a chain of handlers
- Command: Encapsulates a request as an object
- Interpreter: Defines a grammatical representation for a language
- Iterator: Provides a way to access the elements of an aggregate object sequentially
- Mediator: Defines how a set of objects interact with each other
- Memento: Captures and externalizes an object's internal state without violating encapsulation
- Observer: Defines a one-to-many dependency between objects so that when one object changes state, all dependents are notified automatically
- State: Allows an object to alter its behavior when its internal state changes
- Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable
- Template Method: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses
- Visitor: Represents an operation to be performed on the elements of an object structure
Project Structure
design-patterns/
├── go/ # Go implementations
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── go.mod
│ ├── Makefile
│ └── README.md
├── python/ # Python implementations
│ ├── abstract_factory/
│ ├── adapter/
│ ├── bridge/
│ ├── builder/
│ ├── chain_of_responsibility/
│ ├── command/
│ ├── composite/
│ ├── decorator/
│ ├── facade/
│ ├── factory_method/
│ ├── flyweight/
│ ├── interpreter/
│ ├── iterator/
│ ├── mediator/
│ ├── memento/
│ ├── observer/
│ ├── prototype/
│ ├── proxy/
│ ├── singleton/
│ ├── state/
│ ├── strategy/
│ ├── template_method/
│ ├── visitor/
│ ├── Makefile
│ ├── pyproject.toml
│ └── README.md
├── ts/ # TypeScript implementations
│ ├── src/
│ │ ├── abstract_factory/
│ │ ├── adapter/
│ │ ├── bridge/
│ │ ├── builder/
│ │ ├── chain_of_responsibility/
│ │ ├── command/
│ │ ├── composite/
│ │ ├── decorator/
│ │ ├── facade/
│ │ ├── factory_method/
│ │ ├── flyweight/
│ │ ├── interpreter/
│ │ ├── iterator/
│ │ ├── mediator/
│ │ ├── memento/
│ │ ├── observer/
│ │ ├── prototype/
│ │ ├── proxy/
│ │ ├── singleton/
│ │ ├── state/
│ │ ├── strategy/
│ │ ├── template_method/
│ │ ├── visitor/
│ ├── package.json
│ ├── tsconfig.json
│ ├── pnpm-lock.yaml
│ ├── Makefile
│ └── README.md
├── .gitignore
├── README.md # This file
└── README_CN.md # Chinese version
Getting Started
Go Implementation
-
Navigate to the Go directory:
cd go -
Each pattern directory contains:
- Implementation files
- Example usage
- Tests (if applicable)
-
Run examples using:
go run <pattern-name>/main.go
Python Implementation
-
Navigate to the Python directory:
cd python -
Each pattern directory contains:
- Implementation modules
- Example usage
- Tests (if applicable)
-
Run examples using:
python -m <pattern-name>.main
TypeScript Implementation
-
Navigate to the TypeScript directory:
cd ts -
Install dependencies:
pnpm install -
Each pattern directory contains:
- Implementation files
- Example usage
- Tests (if applicable)
-
Run examples using:
pnpm run <pattern-name>
Pattern Categories
Creational Patterns
Creational patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented.
Structural Patterns
Structural patterns deal with the composition of classes or objects. They help ensure that when one part of a system changes, the entire system doesn't need to change.
Behavioral Patterns
Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility. They focus on communication between objects.
Contributing
Contributions are welcome! Here are some ways you can contribute:
- Report bugs and issues
- Suggest new features or improvements
- Add implementations in other languages
- Improve documentation
- Add more comprehensive examples
To contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License.
Acknowledgments
- The Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) for their seminal work on design patterns
- The open-source community for providing inspiration and examples
- All contributors who help maintain and improve this repository