# Design Patterns Implementation [中文版 README](README_CN.md) | 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](#introduction) - [Design Patterns Overview](#design-patterns-overview) - [Project Structure](#project-structure) - [Getting Started](#getting-started) - [Pattern Categories](#pattern-categories) - [Contributing](#contributing) - [License](#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 1. Navigate to the Go directory: ```bash cd go ``` 2. Run examples using: ```bash make ``` ### Python Implementation 1. Navigate to the Python directory: ```bash cd python ``` 2. Run examples using: ```bash make ``` ### TypeScript Implementation 1. Navigate to the TypeScript directory: ```bash cd ts ``` 2. Install dependencies: ```bash pnpm install ``` 3. Run examples using: ```bash make ``` ## Contributing Contributions are welcome! Here are some ways you can contribute: 1. Report bugs and issues 2. Suggest new features or improvements 3. Add implementations in other languages 4. Improve documentation 5. Add more comprehensive examples To contribute: 1. Fork the repository 2. Create a feature branch (`git checkout -b feature/amazing-feature`) 3. Commit your changes (`git commit -m 'Add some amazing feature'`) 4. Push to the branch (`git push origin feature/amazing-feature`) 5. 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