# Complete Guide to Design Patterns in the architect-awesome Repository

> Explore 23 GoF design patterns in the architect-awesome repository. Discover structural, creational, and behavioral patterns with Java examples and architectural insights.

- Repository: [xingshaocheng/architect-awesome](https://github.com/xingshaocheng/architect-awesome)
- Tags: tutorial
- Published: 2026-03-05

---

**The architect-awesome repository documents 23 classic Gang of Four (GoF) design patterns organized into structural, creational, and behavioral categories, providing architectural explanations and Java implementation examples for each.**

The **xingshaocheng/architect-awesome** repository serves as a comprehensive knowledge base for software architecture, featuring a dedicated section that enumerates and explains **design patterns** essential for building scalable systems. Located in the [`README.md`](https://github.com/xingshaocheng/architect-awesome/blob/main/README.md) file under the heading **"23种常见设计模式"**, this documentation categorizes patterns into three classic groups and provides source links and implementation details verified against the repository's source.

## Categorized Pattern Inventory

The repository organizes the 23 patterns into the standard GoF taxonomy. Below is the complete reference extracted from the [`README.md`](https://github.com/xingshaocheng/architect-awesome/blob/main/README.md) source, with direct links to each pattern's documentation anchor.

### Structural Patterns (结构型)

Six structural patterns are documented in the repository's structural section. These patterns focus on class and object composition to form larger structures.

- **Adapter (适配器)**: Converts an existing interface into one a client expects, enabling reuse of incompatible classes. See `README.md#适配器`.
- **Bridge (桥接)**: Decouples an abstraction from its implementation so both can vary independently. See `README.md#桥接模式`.
- **Composite (组合)**: Treats individual objects and compositions uniformly, allowing tree-like structures. See `README.md#组合模式`.
- **Decorator (装饰者)**: Dynamically adds responsibilities to objects without altering their structure. See `README.md#装饰者模式`.
- **Flyweight (享元)**: Shares common parts of state between many objects to reduce memory consumption. See `README.md#享元模式`.
- **Proxy (代理)**: Provides a placeholder or surrogate for another object to control access. See `README.md#代理模式`.

### Creational Patterns (创建型)

Five creational patterns handle object instantiation mechanisms, optimizing object creation for specific use cases.

- **Abstract Factory (抽象工厂)**: Supplies families of related objects without specifying concrete classes. See `README.md#抽象工厂模式`.
- **Builder (建造者)**: Separates construction of a complex object from its representation, allowing step-by-step creation. See `README.md#建造模式`.
- **Factory Method (工厂方法)**: Defines an interface for creating an object, letting subclasses decide which class to instantiate. See `README.md#工厂方法`.
- **Prototype (原型)**: Clones existing objects to create new ones, avoiding costly creation. See `README.md#原型模式`.
- **Singleton (单例)**: Guarantees a class has only one instance and provides a global access point. See `README.md#单例模式`.

### Behavioral Patterns (行为型)

Eight behavioral patterns are cataloged, focusing on algorithms and the assignment of responsibilities between objects.

- **Chain of Responsibility (责任链)**: Passes a request along a chain of handlers until one handles it, decoupling sender and receiver. See `README.md#责任链模式`.
- **Command (命令)**: Encapsulates a request as an object, allowing parameterization and queuing of operations. See `README.md#命令模式`.
- **Interpreter (解释器)**: Defines a grammatical representation for a language and an interpreter to evaluate sentences. See `README.md#解释器模式`.
- **Iterator (迭代器)**: Provides a way to access elements of a collection sequentially without exposing its underlying representation. See `README.md#迭代器模式`.
- **Mediator (中介者)**: Centralizes complex communications and control logic between related objects. See `README.md#中介者模式`.
- **Null Object (空对象)**: Supplies a benign object that does nothing, avoiding null-reference checks. See `README.md#空对象模式`.
- **Observer (观察者)**: Defines a one-to-many dependency so when one object changes, all dependents are notified. See `README.md#观察者模式`.
- **Template Method (模板方法)**: Defines the skeleton of an algorithm in a method, letting subclasses override specific steps. See `README.md#模板方法模式`.

## Java Implementation Examples

The repository provides practical Java implementations for each pattern. The following examples demonstrate the structural mechanics and object interactions as implemented in the source documentation.

### Adapter Pattern

```java
// Target interface expected by client code
interface MediaPlayer {
    void play(String audioType, String fileName);
}

// Existing class with an incompatible interface
class AdvancedMediaPlayer {
    void playVlc(String fileName) { System.out.println("Playing vlc: " + fileName); }
    void playMp4(String fileName) { System.out.println("Playing mp4: " + fileName); }
}

// Adapter that makes AdvancedMediaPlayer conform to MediaPlayer
class MediaAdapter implements MediaPlayer {
    private final AdvancedMediaPlayer advancedPlayer = new AdvancedMediaPlayer();

    @Override
    public void play(String audioType, String fileName) {
        if ("vlc".equalsIgnoreCase(audioType)) {
            advancedPlayer.playVlc(fileName);
        } else if ("mp4".equalsIgnoreCase(audioType)) {
            advancedPlayer.playMp4(fileName);
        }
    }
}

```

### Bridge Pattern

```java
// Implementor hierarchy
interface DrawAPI { void drawCircle(int radius, int x, int y); }
class RedCircle implements DrawAPI { public void drawCircle(int r, int x, int y) { System.out.println("Red circle"); } }
class GreenCircle implements DrawAPI { public void drawCircle(int r, int x, int y) { System.out.println("Green circle"); } }

// Abstraction hierarchy
abstract class Shape {
    protected DrawAPI drawAPI;
    protected Shape(DrawAPI api) { this.drawAPI = api; }
    abstract void draw();
}
class Circle extends Shape {
    private int x, y, radius;
    Circle(int x, int y, int r, DrawAPI api) { super(api); this.x = x; this.y = y; this.radius = r; }
    void draw() { drawAPI.drawCircle(radius, x, y); }
}

```

### Composite Pattern

```java
interface Graphic { void draw(); }
class Dot implements Graphic { public void draw() { System.out.println("Dot"); } }
class Circle implements Graphic { public void draw() { System.out.println("Circle"); } }
class CompoundGraphic implements Graphic {
    private final List<Graphic> children = new ArrayList<>();
    void add(Graphic g) { children.add(g); }
    void draw() { children.forEach(Graphic::draw); }
}

```

### Decorator Pattern

```java
interface Coffee { double cost(); }
class SimpleCoffee implements Coffee { public double cost() { return 2.0; } }

abstract class CoffeeDecorator implements Coffee {
    protected final Coffee delegate;
    CoffeeDecorator(Coffee c) { this.delegate = c; }
    public double cost() { return delegate.cost(); }
}
class MilkDecorator extends CoffeeDecorator {
    MilkDecorator(Coffee c) { super(c); }
    public double cost() { return super.cost() + 0.5; }
}
class SugarDecorator extends CoffeeDecorator {
    SugarDecorator(Coffee c) { super(c); }
    public double cost() { return super.cost() + 0.2; }
}

```

### Flyweight Pattern

```java
class CharacterFactory {
    private static final Map<Character, CharacterFlyweight> cache = new HashMap<>();
    static CharacterFlyweight get(char c) {
        return cache.computeIfAbsent(c, k -> new CharacterFlyweight(k));
    }
}
class CharacterFlyweight {
    private final char c;
    CharacterFlyweight(char c) { this.c = c; }
    void display(int fontSize) { System.out.println(c + " size:" + fontSize); }
}

```

### Proxy Pattern

```java
interface Image { void display(); }
class RealImage implements Image {
    private final String fileName;
    RealImage(String name) { this.fileName = name; loadFromDisk(); }
    private void loadFromDisk() { System.out.println("Loading " + fileName); }
    public void display() { System.out.println("Displaying " + fileName); }
}
class ProxyImage implements Image {
    private final String fileName;
    private RealImage realImage;
    ProxyImage(String name) { this.fileName = name; }
    public void display() {
        if (realImage == null) realImage = new RealImage(fileName);
        realImage.display();
    }
}

```

### Abstract Factory Pattern

```java
interface GUIFactory { Button createButton(); Checkbox createCheckbox(); }
class WinFactory implements GUIFactory { public Button createButton() { return new WinButton(); } public Checkbox createCheckbox() { return new WinCheckbox(); } }
class MacFactory implements GUIFactory { public Button createButton() { return new MacButton(); } public Checkbox createCheckbox() { return new MacCheckbox(); } }
// Client
class Application {
    private final Button button;
    private final Checkbox checkbox;
    Application(GUIFactory factory) {
        button = factory.createButton();
        checkbox = factory.createCheckbox();
    }
    void render() { button.paint(); checkbox.paint(); }
}

```

### Builder Pattern

```java
class NutritionFacts {
    private final int calories;
    private final int fat;
    private final int carbs;
    private NutritionFacts(Builder b) {
        this.calories = b.calories;
        this.fat = b.fat;
        this.carbs = b.carbs;
    }
    static class Builder {
        private int calories;
        private int fat;
        private int carbs;
        Builder calories(int c) { this.calories = c; return this; }
        Builder fat(int f) { this.fat = f; return this; }
        Builder carbs(int c) { this.carbs = c; return this; }
        NutritionFacts build() { return new NutritionFacts(this); }
    }
}

```

### Factory Method Pattern

```java
abstract class Logger {
    abstract LogWriter createWriter();
    void log(String msg) { createWriter().write(msg); }
}
class FileLogger extends Logger { LogWriter createWriter() { return new FileWriter(); } }
class ConsoleLogger extends Logger { LogWriter createWriter() { return new ConsoleWriter(); } }

```

### Prototype Pattern

```java
class Prototype implements Cloneable {
    private String value;
    Prototype(String v) { this.value = v; }
    public Prototype clone() { return new Prototype(this.value); }
}

```

### Singleton Pattern

```java
enum Singleton { INSTANCE; 
    void doSomething() { System.out.println("singleton work"); }
}

```

### Chain of Responsibility Pattern

```java
abstract class Handler {
    protected Handler next;
    void setNext(Handler n) { this.next = n; }
    abstract void handle(String request);
}
class AuthHandler extends Handler {
    void handle(String req) { 
        if (req.startsWith("auth")) System.out.println("auth ok");
        else if (next != null) next.handle(req);
    }
}
class LogHandler extends Handler {
    void handle(String req) { System.out.println("log: " + req); if (next != null) next.handle(req); }
}

```

### Command Pattern

```java
interface Command { void execute(); }
class Light { void on() { System.out.println("light on"); } }
class LightOnCommand implements Command {
    private final Light light;
    LightOnCommand(Light l) { this.light = l; }
    public void execute() { light.on(); }
}
class RemoteControl { private Command slot; void setCommand(Command c) { slot = c; } void buttonPressed() { slot.execute(); } }

```

### Interpreter Pattern

```java
interface Expression { boolean interpret(String context); }
class TerminalExpression implements Expression {
    private final String data;
    TerminalExpression(String d) { this.data = d; }
    public boolean interpret(String ctx) { return ctx.contains(data); }
}
class OrExpression implements Expression {
    private final Expression expr1, expr2;
    OrExpression(Expression e1, Expression e2) { this.expr1 = e1; this.expr2 = e2; }
    public boolean interpret(String ctx) { return expr1.interpret(ctx) || expr2.interpret(ctx); }
}

```

### Iterator Pattern

```java
class NameRepository implements Iterable<String> {
    private final List<String> names = Arrays.asList("Alice","Bob","Carol");
    public Iterator<String> iterator() { return names.iterator(); }
}

```

### Mediator Pattern

```java
interface ChatMediator { void showMessage(User user, String msg); }
class ConcreteMediator implements ChatMediator {
    public void showMessage(User user, String msg) { System.out.println(user.getName() + ": " + msg); }
}
abstract class User {
    protected ChatMediator mediator;
    protected String name;
    User(ChatMediator m, String n) { mediator = m; name = n; }
    String getName() { return name; }
    abstract void send(String msg);
    abstract void receive(String msg);
}
class BasicUser extends User {
    BasicUser(ChatMediator m, String n) { super(m,n); }
    void send(String msg) { mediator.showMessage(this, msg); }
    void receive(String msg) { System.out.println(name + " received: " + msg); }
}

```

### Null Object Pattern

```java
interface Animal { void makeSound(); }
class Dog implements Animal { public void makeSound() { System.out.println("Woof!"); } }
class NullAnimal implements Animal { public void makeSound() { /* nothing */ } }

```

### Observer Pattern

```java
interface Observer { void update(String msg); }
interface Subject { void attach(Observer o); void detach(Observer o); void notifyObservers(); }
class NewsAgency implements Subject {
    private final List<Observer> observers = new ArrayList<>();
    private String news;
    public void attach(Observer o){ observers.add(o); }
    public void detach(Observer o){ observers.remove(o); }
    public void setNews(String n){ this.news = n; notifyObservers(); }
    public void notifyObservers(){ observers.forEach(o -> o.update(news)); }
}
class NewsChannel implements Observer {
    private String news;
    public void update(String msg){ this.news = msg; System.out.println("Channel received: " + news); }
}

```

### Template Method Pattern

```java
abstract class Game {
    final void play(){ initialize(); startPlay(); endPlay(); }
    abstract void initialize();
    abstract void startPlay();
    abstract void endPlay();
}
class Chess extends Game {
    void initialize() { System.out.println("Chess Board Setup"); }
    void startPlay() { System.out.println("Chess Game Started"); }
    void endPlay() { System.out.println("Chess Game Over"); }
}

```

## Summary

- The **architect-awesome** repository catalogs **23 classic design patterns** in [`README.md`](https://github.com/xingshaocheng/architect-awesome/blob/main/README.md) under the section **"23种常见设计模式"**.
- Patterns are categorized into **Structural** (6), **Creational** (5), and **Behavioral** (8) groups following the Gang of Four taxonomy.
- Each pattern entry includes architectural explanations and direct source links to specific anchors in the repository.
- The repository provides runnable **Java code examples** demonstrating practical implementation of each pattern.
- Additional architectural concepts like **MVC**, **IOC**, and **AOP** are mentioned separately but are not counted among the 23 classic patterns.

## Frequently Asked Questions

### How many design patterns are covered in the architect-awesome repository?

The repository explicitly documents **23 classic design patterns** as indicated by the "23种常见设计模式" section in the [`README.md`](https://github.com/xingshaocheng/architect-awesome/blob/main/README.md) file. These correspond to the standard Gang of Four (GoF) catalog, divided into structural, creational, and behavioral categories.

### Where are the design patterns documented in the repository?

All patterns are documented in the main **[`README.md`](https://github.com/xingshaocheng/architect-awesome/blob/main/README.md)** file at the repository root. The specific section is titled **"23种常见设计模式"** (23 Common Design Patterns), with each pattern linked to an in-page anchor such as `#适配器` or `#单例模式` for direct navigation.

### Does the repository provide code examples for the design patterns?

Yes, the repository includes **practical Java implementation examples** for each documented pattern. These examples illustrate class structures, interface definitions, and object interactions that demonstrate how to apply the pattern in production code.

### What is the difference between the 23 design patterns and other architectural styles mentioned?

The **23 classic patterns** focus on object-level design solutions for recurring problems. In contrast, architectural styles like **MVC** (Model-View-Controller), **IOC** (Inversion of Control), and **AOP** (Aspect-Oriented Programming) mentioned elsewhere in the repository address higher-level application architecture and are not included in the 23-pattern count.