Skip to content

Commit 30325a7

Browse files
author
Jarry
committed
add c for builder-pattern
1 parent b7e09fe commit 30325a7

File tree

11 files changed

+21
-9
lines changed

11 files changed

+21
-9
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"files.associations": {
3-
"stdlib.h": "c"
3+
"stdlib.h": "c",
4+
"stdio.h": "c",
5+
"string.h": "c",
6+
"memory": "c",
7+
"utility": "c",
8+
"type_traits": "c"
49
}
510
}

builder-pattern/c/a.out

-50.2 KB
Binary file not shown.

builder-pattern/c/src/application.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ void make_iphone()
4646
// 根据建造者获取对象
4747
printf("iphone: %s | ", phone->name);
4848
print_phone(phone);
49+
free_phone_builder(phone_builder);
4950

5051
// 创建手册建造者
5152
ManualBuilder *manual_builder = create_manual_builder();
@@ -55,6 +56,8 @@ void make_iphone()
5556
Manual *manual = get_manual_product(manual_builder);
5657
printf("iphone manual: %s | ", manual->name);
5758
print_manual(manual);
59+
free_manual_builder(manual_builder);
60+
5861
}
5962

6063
void make_huawei_phone()
@@ -67,6 +70,7 @@ void make_huawei_phone()
6770
// 根据建造者获取对象
6871
printf("huawei phone: %s", phone->name);
6972
print_phone(phone);
73+
free_phone_builder(phone_builder);
7074

7175
// 创建手册建造者
7276
ManualBuilder *manual_builder = create_manual_builder();
@@ -76,4 +80,5 @@ void make_huawei_phone()
7680
Manual *manual = get_manual_product(manual_builder);
7781
printf("huawei manual: %s", manual->name);
7882
print_manual(manual);
83+
free_manual_builder(manual_builder);
7984
}

builder-pattern/c/src/func.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ typedef struct
1414
int gpu_type;
1515
} Manual;
1616

17-
// 定义手册创建者结构体,用来创建Manual对象
17+
// 定义手册创建者结构体,聚合了Manual对象
1818
typedef struct
1919
{
2020
Phone *phone;
2121
} PhoneBuilder;
2222

23-
// 定义手册创建者结构体,用来创建Manual对象
23+
// 定义手册创建者结构体,聚合了Manual对象
2424
typedef struct
2525
{
2626
Manual *manual;

builder-pattern/c/src/manual_builder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ManualBuilder *create_manual_builder()
1515

1616
void reset_manual(ManualBuilder *builder)
1717
{
18-
// 初始化Phone对象
18+
// 初始化Manual对象
1919
Manual *manual = (Manual *)malloc(sizeof(Manual));
2020
manual->name[0] = '\0';
2121
int screen_len = (int)sizeof(manual->screen) / sizeof(manual->screen[0]);

builder-pattern/c/test/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ int main()
2323
Phone *phone = get_phone_product(phone_builder);
2424
printf("mi phone: %s | ", phone->name);
2525
print_phone(phone);
26+
free_phone_builder(phone_builder);
2627

2728
// 创建手册
2829
ManualBuilder *manual_builder = create_manual_builder();
2930
build_mi_manual(manual_builder);
3031
Manual *manual = get_manual_product(manual_builder);
3132
printf("mi manual: %s | ", manual->name);
3233
print_manual(manual);
34+
free_manual_builder(manual_builder);
3335
}
3436

3537
/**

chain-responsibility/go/src/RequestHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *RequestHandler) GetNext() Handler {
2323
// 如果检查不通过则返回失败,否则继续下一个检查
2424
func (r *RequestHandler) Check(uid int) bool {
2525
fmt.Println("RequestHandler::Check() [uid = " + strconv.Itoa(uid) + "]")
26-
if uid%1 != 0 {
26+
if uid%5 == 0 {
2727
return false
2828
}
2929
// 通过聚合类来调用下一步检查

chain-responsibility/java/src/RequestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class RequestHandler extends AbstractHandler {
55
// 如果检查不通过则返回失败,否则继续下一个检查
66
public boolean check(int uid) {
77
System.out.println(this.getClass().getName() + "::check() [uid = " + uid + "]");
8-
if (uid % 1 != 0) {
8+
if (uid % 5 == 0) {
99
return false;
1010
}
1111
return checkNext(uid);

chain-responsibility/js/src/RequestHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class RequestHandler extends AbstractHandler {
55
// 如果检查不通过则返回失败,否则继续下一个检查
66
check(uid) {
77
console.log(this.constructor.name + '::check() [uid = ' + uid + ']')
8-
if (uid % 1 !== 0) {
8+
if (uid % 3 === 0) {
99
return false
1010
}
1111
return this.checkNext(uid)

chain-responsibility/python/src/RequestHandler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class RequestHandler(AbstractHandler):
1010
# 如果检查不通过则返回失败,否则继续下一个检查
1111
def check(self, uid):
1212
print(self.__class__.__name__ + '::check() [uid = ' + str(uid) + ']')
13-
if (uid % 1 != 0):
13+
if (uid % 5 == 0):
1414
return False
1515

1616
return self.checkNext(uid)

chain-responsibility/ts/src/RequestHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class RequestHandler extends AbstractHandler {
55
// 如果检查不通过则返回失败,否则继续下一个检查
66
check(uid: number) {
77
console.log(this.constructor.name + '::check() [uid = ' + uid + ']')
8-
if (uid % 1 !== 0) {
8+
if (uid % 5 === 0) {
99
return false
1010
}
1111
return this.checkNext(uid)

0 commit comments

Comments
 (0)