Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Design patterns from 《大话设计模式》 and some samples implemented by C#

设计模式大体上可分为三类:

> **UML 类图**:本项目包含了所有设计模式的 UML 类图,位于 [uml](./uml) 文件夹中,使用 PlantUML 格式,可在线渲染或本地查看。

- [创建型模式(Create)](./CreatePattern)
1. [简单工厂(SimpleFactory)](./CreatePattern/SimpleFactoryPattern)
1. [抽象工厂(AbstractFactory)](./CreatePattern/AbstractFactoryPattern)
Expand Down
55 changes: 55 additions & 0 deletions uml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# UML Diagrams for Design Patterns

This folder contains UML diagrams for all design patterns implemented in this repository.

## Structure

- `creational/` - UML diagrams for creational patterns
- `structural/` - UML diagrams for structural patterns
- `behavioral/` - UML diagrams for behavioral patterns

## Format

All diagrams are provided in PlantUML format (.puml files) which can be rendered as images using various tools.

## Usage

To render these diagrams:
1. **Online**: Use online renderer at http://www.plantuml.com/plantuml/uml/
2. **Local Installation**: Install PlantUML locally
3. **VS Code**: Install the PlantUML extension for VS Code for inline preview and rendering
4. **IDE Plugins**: Use PlantUML plugins available for IntelliJ IDEA, Eclipse, and other IDEs

Open the .puml files and render them as PNG/SVG images

## Patterns Included

### Creational Patterns (6)
- Simple Factory Pattern
- Abstract Factory Pattern
- Factory Method Pattern
- Builder Pattern
- Prototype Pattern
- Singleton Pattern

### Structural Patterns (7)
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern

### Behavioral Patterns (11)
- Observer Pattern
- Template Method Pattern
- Command Pattern
- State Pattern
- Chain of Responsibility Pattern
- Interpreter Pattern
- Mediator Pattern
- Visitor Pattern
- Memento Pattern
- Iterator Pattern
- Strategy Pattern
55 changes: 55 additions & 0 deletions uml/README.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 设计模式 UML 类图

本文件夹包含了本仓库中实现的所有设计模式的 UML 类图。

## 目录结构

- `creational/` - 创建型模式的 UML 类图
- `structural/` - 结构型模式的 UML 类图
- `behavioral/` - 行为型模式的 UML 类图

## 格式

所有类图均采用 PlantUML 格式(.puml 文件),可使用各种工具渲染为图像。

## 使用方法

渲染这些类图的方法:
1. **在线工具**:使用在线渲染器 http://www.plantuml.com/plantuml/uml/
2. **本地安装**:本地安装 PlantUML
3. **VS Code**:安装 PlantUML 扩展,支持内联预览和渲染
4. **IDE 插件**:使用适用于 IntelliJ IDEA、Eclipse 和其他 IDE 的 PlantUML 插件

打开 .puml 文件并将其渲染为 PNG/SVG 图像

## 包含的模式

### 创建型模式 (6种)
- 简单工厂模式 (Simple Factory Pattern)
- 抽象工厂模式 (Abstract Factory Pattern)
- 工厂方法模式 (Factory Method Pattern)
- 建造者模式 (Builder Pattern)
- 原型模式 (Prototype Pattern)
- 单例模式 (Singleton Pattern)

### 结构型模式 (7种)
- 适配器模式 (Adapter Pattern)
- 桥接模式 (Bridge Pattern)
- 组合模式 (Composite Pattern)
- 装饰器模式 (Decorator Pattern)
- 外观模式 (Facade Pattern)
- 享元模式 (Flyweight Pattern)
- 代理模式 (Proxy Pattern)

### 行为型模式 (11种)
- 观察者模式 (Observer Pattern)
- 模板方法模式 (Template Method Pattern)
- 命令模式 (Command Pattern)
- 状态模式 (State Pattern)
- 职责链模式 (Chain of Responsibility Pattern)
- 解释器模式 (Interpreter Pattern)
- 中介者模式 (Mediator Pattern)
- 访问者模式 (Visitor Pattern)
- 备忘录模式 (Memento Pattern)
- 迭代器模式 (Iterator Pattern)
- 策略模式 (Strategy Pattern)
79 changes: 79 additions & 0 deletions uml/behavioral/chain-of-responsibility.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@startuml Chain of Responsibility Pattern
!theme plain

title Chain of Responsibility Pattern

abstract class Manager {
#ManagerName: string
#Superior: Manager
+Manager(managerName: string)
+SetSuperior(superior: Manager): void
+{abstract} RequestApplications(request: Request): void
}

class CommonManager {
+CommonManager(managerName: string)
+RequestApplications(request: Request): void
}

class Majordomo {
+Majordomo(managerName: string)
+RequestApplications(request: Request): void
}

class GeneralManager {
+GeneralManager(managerName: string)
+RequestApplications(request: Request): void
}

class Request {
+RequestType: string
+RequestContent: string
+RequestNum: int
}

class Client {
+Main(): void
}

Manager <|-- CommonManager
Manager <|-- Majordomo
Manager <|-- GeneralManager

Manager -> Manager : successor
Manager -> Request : handles

Client -> Manager : uses
Client -> Request : creates

note right of Manager
Handler defines interface
for handling requests and
implements successor link
end note

note right of CommonManager
CommonManager handles requests
for leave <= 2 days, otherwise
passes to successor
end note

note right of Majordomo
Majordomo handles requests
for leave <= 5 days, otherwise
passes to successor
end note

note right of GeneralManager
GeneralManager handles all
leave requests and salary
increase <= 500
end note

note top of Manager
Chain of Responsibility passes
request along chain of handlers
until one handles it
end note

@enduml
95 changes: 95 additions & 0 deletions uml/behavioral/command.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
@startuml Command Pattern
!theme plain

title Command Pattern

abstract class Command {
#Receiver: Receiver
+Command(receiver: Receiver)
+{abstract} Execute(): void
}

class ConcreteCommand {
+ConcreteCommand(receiver: Receiver)
+Execute(): void
}

class Receiver {
+Action(): void
}

class Invoker {
-command: Command
+SetCommand(command: Command): void
+ExecuteCommand(): void
}

abstract class OrderCommand {
#Receiver: Barbecuer
+OrderCommand(receiver: Barbecuer)
+{abstract} ExecuteCommand(): void
}

class BakeMuttonCommand {
+BakeMuttonCommand(receiver: Barbecuer)
+ExecuteCommand(): void
+ToString(): string
}

class BakeChickenWingCommand {
+BakeChickenWingCommand(receiver: Barbecuer)
+ExecuteCommand(): void
+ToString(): string
}

class Barbecuer {
+BakeMutton(): void
+BakeChickenWing(): void
}

class Waiter {
-orders: ICollection<OrderCommand>
+SetOrder(order: OrderCommand): void
+CancelOrder(order: OrderCommand): void
+Notify(): void
}

class Client {
+Main(): void
}

Command <|-- ConcreteCommand
Command -> Receiver
Invoker -> Command

OrderCommand <|-- BakeMuttonCommand
OrderCommand <|-- BakeChickenWingCommand
OrderCommand -> Barbecuer
Waiter *-- OrderCommand

Client -> Invoker : uses
Client -> Waiter : uses

note right of Command
Command declares interface
for executing an operation
end note

note right of ConcreteCommand
ConcreteCommand defines binding
between Receiver object and action.
Implements Execute by invoking
corresponding operations on Receiver
end note

note right of Invoker
Invoker asks command to
carry out the request
end note

note right of Waiter
Waiter manages multiple orders
and can execute them in batch
end note

@enduml
88 changes: 88 additions & 0 deletions uml/behavioral/interpreter.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
@startuml Interpreter Pattern
!theme plain

title Interpreter Pattern

class Context {
+Input: string
+Output: string
}

abstract class AbstractExpression {
+{abstract} Interpret(context: Context): void
}

class TerminalExpression {
+Interpret(context: Context): void
}

class NoneTerminalExpression {
+Interpret(context: Context): void
}

class PlayContext {
+PlayText: string
}

abstract class MusicalExpression {
+Interpret(context: PlayContext): void
+{abstract} Execute(key: string, value: double): void
}

class MusicalNote {
+Execute(key: string, value: double): void
}

class MusicalScale {
+Execute(key: string, value: double): void
}

class MusicalSpeed {
+Execute(key: string, value: double): void
}

class Client {
+Main(): void
}

AbstractExpression <|-- TerminalExpression
AbstractExpression <|-- NoneTerminalExpression
AbstractExpression -> Context : interprets

MusicalExpression <|-- MusicalNote
MusicalExpression <|-- MusicalScale
MusicalExpression <|-- MusicalSpeed
MusicalExpression -> PlayContext : interprets

Client -> AbstractExpression : uses
Client -> MusicalExpression : uses
Client -> Context : uses
Client -> PlayContext : uses

note right of AbstractExpression
AbstractExpression declares
abstract Interpret operation
that is common to all nodes
in abstract syntax tree
end note

note right of TerminalExpression
TerminalExpression implements
Interpret operation for
terminal symbols in grammar
end note

note right of MusicalExpression
Concrete example: interprets
musical notation including
notes, scales, and speeds
end note

note top of AbstractExpression
Interpreter pattern defines
representation for grammar
and interpreter to interpret
sentences in the language
end note

@enduml
Loading
Loading