Skip to content

Commit afdcb31

Browse files
author
Jarry
committed
add c for decorator
1 parent 92b4d2e commit afdcb31

File tree

19 files changed

+639
-9
lines changed

19 files changed

+639
-9
lines changed

.vscode/settings.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
"vehicle.h": "c",
1414
"queue": "c",
1515
"stack": "c",
16-
"filesystem": "c"
16+
"filesystem": "c",
17+
"__functional_base": "c",
18+
"__functional_base_03": "c",
19+
"__hash_table": "c",
20+
"__tuple": "c",
21+
"algorithm": "c",
22+
"array": "c",
23+
"chrono": "c",
24+
"functional": "c",
25+
"iterator": "c",
26+
"limits": "c",
27+
"tuple": "c",
28+
"istream": "c",
29+
"ostream": "c",
30+
"__locale": "c"
1731
}
1832
}

decorator-pattern/README.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 【装饰器设计模式详解】Java/JS/Go/Python/TS不同语言实现
1+
# 【装饰器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现
22

33
# 简介
44
装饰器模式(Decorator Pattern)是一种结构型设计模式。将对象放入到一个特殊封装的对象中,为这个对象绑定新的行为,具备新的能力,同时又不改变其原有结构。
@@ -18,7 +18,7 @@
1818
# UML
1919
<img src="../docs/uml/decorator-pattern.png">
2020

21-
# 代码
21+
# Java代码
2222

2323
## 基础形状接口
2424
```java
@@ -138,5 +138,121 @@ public class ShadowShapeDecorator extends ShapeDecorator {
138138
shadowSquare.draw();
139139
```
140140

141+
# Go代码
142+
143+
## 基础形状接口
144+
```go
145+
// Shape.go 基础形状接口
146+
type Shape interface {
147+
Draw()
148+
GetName() string
149+
}
150+
151+
```
152+
153+
## 具体形状实现
154+
```go
155+
// Circle.go 具体形状实现了基础形状接口
156+
type Circle struct {
157+
}
158+
159+
func (c *Circle) Draw() {
160+
fmt.Println("Circle::Draw()")
161+
}
162+
163+
func (c *Circle) GetName() string {
164+
return "Circle"
165+
}
166+
167+
```
168+
169+
```go
170+
// Square.go 具体形状实现了基础形状接口
171+
type Square struct {
172+
}
173+
174+
func (c *Square) Draw() {
175+
fmt.Println("Square::Draw()")
176+
}
177+
178+
func (c *Square) GetName() string {
179+
return "Square"
180+
}
181+
```
182+
183+
## 抽象装饰器
184+
```go
185+
// ShapeDecorator.go 抽象装饰类,是否实现Shape可选
186+
type ShapeDecorator interface {
187+
Draw()
188+
}
189+
```
190+
191+
## 具体装饰器
192+
```go
193+
// RedShapeDecorator.go 具体装饰器1
194+
type RedShapeDecorator struct {
195+
DecoratedShape Shape
196+
}
197+
198+
func (r *RedShapeDecorator) Draw() {
199+
r.DecoratedShape.Draw()
200+
r.SetRedColor(r.DecoratedShape)
201+
}
202+
203+
func (r *RedShapeDecorator) SetRedColor(decoratedShape Shape) {
204+
fmt.Println("RedShapeDecorator::setRedColor() " + decoratedShape.GetName())
205+
}
206+
```
207+
208+
```go
209+
// ShadowShapeDecorator.go 具体装饰器2
210+
type ShadowShapeDecorator struct {
211+
DecoratedShape Shape
212+
}
213+
214+
func (s *ShadowShapeDecorator) Draw() {
215+
// 装饰器根据需要是否调用形状的Draw方法
216+
// s.DecoratedShape.Draw()
217+
s.SetShadow(s.DecoratedShape)
218+
}
219+
220+
func (s *ShadowShapeDecorator) SetShadow(decoratedShape Shape) {
221+
fmt.Println("ShadowShapeDecorator::SetShadow() " + decoratedShape.GetName())
222+
}
223+
```
224+
225+
## 测试调用
226+
```go
227+
/**
228+
* 装饰器模式是将一个对象放到一个装饰器对象中,执行装饰器类里的方法时,对象的行为能力得到增强。
229+
* 先声明具体对象,然后放到装饰器,得到一个带有装饰器的新对象,该对象具备了新的能力。
230+
*/
231+
232+
// 声明形状
233+
var circle = new(src.Circle)
234+
var square = new(src.Square)
235+
236+
// 增加红色装饰
237+
var redCircle = &src.RedShapeDecorator{
238+
DecoratedShape: circle,
239+
}
240+
var redSquare = &src.RedShapeDecorator{
241+
DecoratedShape: square,
242+
}
243+
circle.Draw()
244+
redCircle.Draw()
245+
redSquare.Draw()
246+
247+
// 增加影子装饰
248+
var shadowCircle = &src.ShadowShapeDecorator{
249+
DecoratedShape: circle,
250+
}
251+
var shadowSquare = &src.ShadowShapeDecorator{
252+
DecoratedShape: square,
253+
}
254+
shadowCircle.Draw()
255+
shadowSquare.Draw()
256+
```
141257
## 更多语言版本
142258
不同语言实现设计模式:[https://github.com/microwind/design-pattern](https://github.com/microwind/design-pattern)

decorator-pattern/c/src/circle.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "func.h"
2+
3+
// 具体形状实现了基础形状接口
4+
void circle_draw(Circle *circle)
5+
{
6+
printf("\r\n Circle::draw() [name=%s]", circle->name);
7+
}
8+
9+
// 创建Circle对象
10+
Circle *circle_constructor(char *name)
11+
{
12+
Shape *shape = (Shape *)malloc(sizeof(Shape));
13+
strncpy(shape->name, name, 50);
14+
// 转为Circle
15+
Circle *circle = (Circle *)shape;
16+
circle->draw = &circle_draw;
17+
return circle;
18+
}

decorator-pattern/c/src/func.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
// 基础形状类,可定义公共方法
7+
typedef struct Shape
8+
{
9+
char name[50];
10+
void (*draw)(struct Shape *);
11+
} Shape;
12+
13+
// 具体形状实现了基础形状接口
14+
typedef struct Circle
15+
{
16+
char name[50];
17+
void (*draw)(struct Circle *);
18+
} Circle;
19+
Circle *circle_constructor(char *name);
20+
21+
// 具体形状实现了基础形状接口
22+
typedef struct Square
23+
{
24+
char name[50];
25+
void (*draw)(struct Square *);
26+
} Square;
27+
Square *square_constructor(char *name);
28+
29+
// 抽象装饰器接口
30+
typedef struct ShapeDecorator
31+
{
32+
char name[50];
33+
void (*draw)(struct ShapeDecorator *);
34+
struct ShapeDecorator *decorated_shape;
35+
// void (*set_red_color)(struct ShapeDecorator *);
36+
// void (*set_shadow)(struct ShapeDecorator *);
37+
} ShapeDecorator;
38+
39+
// 具体装饰器1,实现基础装饰器接口
40+
typedef struct RedShapeDecorator
41+
{
42+
char name[50];
43+
void (*draw)(struct RedShapeDecorator *);
44+
// 聚合具体形状对象
45+
struct ShapeDecorator *decorated_shape;
46+
void (*set_red_color)(struct RedShapeDecorator *);
47+
} RedShapeDecorator;
48+
RedShapeDecorator *red_shape_decorator_constructor(char *, ShapeDecorator *);
49+
50+
// 具体装饰器2,实现基础装饰器接口
51+
typedef struct ShadowShapeDecorator
52+
{
53+
char name[50];
54+
void (*draw)(struct ShadowShapeDecorator *);
55+
// 聚合具体形状对象
56+
struct ShapeDecorator *decorated_shape;
57+
void (*set_shadow)(struct ShadowShapeDecorator *);
58+
} ShadowShapeDecorator;
59+
ShadowShapeDecorator *shadow_shape_decorator_constructor(char *, ShapeDecorator *);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "func.h"
2+
3+
// 具体装饰器1,实现基础装饰器接口
4+
void red_shape_decorator_draw(RedShapeDecorator *decorator)
5+
{
6+
printf("\r\n RedShapeDecorator::draw() [name=%s]", decorator->name);
7+
decorator->decorated_shape->draw((ShapeDecorator *)decorator);
8+
decorator->set_red_color(decorator);
9+
}
10+
11+
void decorator_set_red_color(RedShapeDecorator *decorator)
12+
{
13+
printf("\r\n RedShapeDecorator::set_red_color() [name=%s]", decorator->name);
14+
}
15+
16+
// 创建RedShapeDecorator对象
17+
RedShapeDecorator *red_shape_decorator_constructor(char *name, ShapeDecorator *decorated_shape)
18+
{
19+
ShapeDecorator *decorator = (ShapeDecorator *)malloc(sizeof(ShapeDecorator));
20+
strncpy(decorator->name, name, 50);
21+
// 转为RedShapeDecorator
22+
RedShapeDecorator *red_shape_decorator = (RedShapeDecorator *)decorator;
23+
red_shape_decorator->draw = &red_shape_decorator_draw;
24+
red_shape_decorator->decorated_shape = decorated_shape;
25+
red_shape_decorator->set_red_color = &decorator_set_red_color;
26+
return red_shape_decorator;
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "func.h"
2+
3+
// 具体装饰器2,实现基础装饰器接口
4+
void shadow_shape_decorator_draw(ShadowShapeDecorator *decorator)
5+
{
6+
printf("\r\n ShadowShapeDecorator::draw() [name=%s]", decorator->name);
7+
// 装饰器根据需要是否也调用形状的draw方法
8+
// decorator->decorated_shape->draw((ShapeDecorator *)decorator);
9+
decorator->set_shadow(decorator);
10+
}
11+
12+
void decorator_set_shadow(ShadowShapeDecorator *decorator)
13+
{
14+
printf("\r\n ShadowShapeDecorator::set_shadow() [name=%s]", decorator->name);
15+
}
16+
17+
// 创建ShadowShapeDecorator对象
18+
ShadowShapeDecorator *shadow_shape_decorator_constructor(char *name, ShapeDecorator *decorated_shape)
19+
{
20+
ShapeDecorator *decorator = (ShapeDecorator *)malloc(sizeof(ShapeDecorator));
21+
strncpy(decorator->name, name, 50);
22+
// 转为ShadowShapeDecorator
23+
ShadowShapeDecorator *shadow_shape_decorator = (ShadowShapeDecorator *)decorator;
24+
shadow_shape_decorator->decorated_shape = decorated_shape;
25+
shadow_shape_decorator->draw = &shadow_shape_decorator_draw;
26+
shadow_shape_decorator->set_shadow = &decorator_set_shadow;
27+
return shadow_shape_decorator;
28+
}

decorator-pattern/c/src/shape.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "func.h"
2+
3+
// 基础形状类,定义在head
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "func.h"
2+
3+
// 抽象装饰器接口,定义在head

decorator-pattern/c/src/square.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "func.h"
2+
3+
// 具体形状实现了基础形状接口
4+
void square_draw(Square *square)
5+
{
6+
printf("\r\n Square::draw() [name=%s]", square->name);
7+
}
8+
9+
// 创建Square对象
10+
Square *square_constructor(char *name)
11+
{
12+
Shape *shape = (Shape *)malloc(sizeof(Shape));
13+
strncpy(shape->name, name, 50);
14+
// 转为Square
15+
Square *square = (Square *)shape;
16+
square->draw = &square_draw;
17+
return square;
18+
}

decorator-pattern/c/test/test.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "../src/func.h"
2+
3+
int main(void)
4+
{
5+
printf("test start:\r\n");
6+
/**
7+
* 装饰器模式是将一个对象放到一个装饰器对象中,执行装饰器类里的方法时,对象的行为能力得到增强。
8+
* 先声明具体对象,然后放到装饰器,得到一个带有装饰器的新对象,该对象具备了新的能力。
9+
*/
10+
11+
// 声明两个形状对象
12+
Shape *circle = (Shape *)circle_constructor("circle");
13+
Square *square = square_constructor("square");
14+
15+
// 增加红色装饰器
16+
RedShapeDecorator *red_circle = red_shape_decorator_constructor("red_circle", (ShapeDecorator *)circle);
17+
ShapeDecorator *red_square = (ShapeDecorator *)red_shape_decorator_constructor("red_square", (ShapeDecorator *)square);
18+
circle->draw(circle);
19+
red_circle->draw(red_circle);
20+
red_square->draw(red_square);
21+
22+
// 增加影子装饰器
23+
ShapeDecorator *shadow_circle = (ShapeDecorator *)shadow_shape_decorator_constructor("shadow_circle", (ShapeDecorator *)circle);
24+
ShadowShapeDecorator *shadow_square = shadow_shape_decorator_constructor("shadow_square", (ShapeDecorator *)square);
25+
shadow_circle->draw(shadow_circle);
26+
shadow_square->draw(shadow_square);
27+
28+
free(circle);
29+
free(square);
30+
free(red_circle);
31+
free(red_square);
32+
free(shadow_circle);
33+
free(shadow_square);
34+
35+
return 0;
36+
}
37+
38+
/**
39+
jarry@jarrys-MacBook-Pro c % gcc test/test.c src/*.c
40+
jarry@jarrys-MacBook-Pro c % ./a.out
41+
test start:
42+
43+
Circle::draw() [name=circle]
44+
RedShapeDecorator::draw() [name=red_circle]
45+
Circle::draw() [name=red_circle]
46+
RedShapeDecorator::set_red_color() [name=red_circle]
47+
RedShapeDecorator::draw() [name=red_square]
48+
Square::draw() [name=red_square]
49+
RedShapeDecorator::set_red_color() [name=red_square]
50+
ShadowShapeDecorator::draw() [name=shadow_circle]
51+
ShadowShapeDecorator::set_shadow() [name=shadow_circle]
52+
ShadowShapeDecorator::draw() [name=shadow_square]
53+
ShadowShapeDecorator::set_shadow() [name=shadow_square]%
54+
*/

0 commit comments

Comments
 (0)