Skip to content

Commit a00f742

Browse files
committed
Impl conceptual builder pattern.
1 parent 73e5194 commit a00f742

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import '../pattern/builder.dart';
2+
import '../pattern/product.dart';
3+
import '../product/concrete_product_1.dart';
4+
5+
class ConcreteBuilder1 implements Builder {
6+
@override
7+
void buildPart1() {
8+
_product.addLine('one');
9+
}
10+
11+
@override
12+
void buildPart2() {
13+
_product.addLine('two');
14+
}
15+
16+
@override
17+
void buildPart3() {
18+
_product.addLine('three');
19+
}
20+
21+
@override
22+
Product get result => _product;
23+
24+
final _product = ConcreteProduct1('ConcreteBuilder1');
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import '../pattern/builder.dart';
2+
import '../pattern/product.dart';
3+
import '../product/concrete_product_2.dart';
4+
5+
class ConcreteBuilder2 implements Builder {
6+
@override
7+
void buildPart1() {
8+
_product.addLine('first');
9+
}
10+
11+
@override
12+
void buildPart2() {
13+
_product.addLine('second');
14+
}
15+
16+
@override
17+
void buildPart3() {
18+
_product.addLine('third');
19+
}
20+
21+
@override
22+
Product get result => _product;
23+
24+
final _product = ConcreteProduct2('ConcreteBuilder2');
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'concrete_builder/concrete_builder_1.dart';
2+
import 'concrete_builder/concrete_builder_2.dart';
3+
import 'pattern/director.dart';
4+
5+
void main() {
6+
final director = Director();
7+
8+
final product1 = director.construct(ConcreteBuilder1());
9+
print(product1);
10+
11+
final product2 = director.construct(ConcreteBuilder2());
12+
print(product2);
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'product.dart';
2+
3+
abstract class Builder {
4+
void buildPart1();
5+
void buildPart2();
6+
void buildPart3();
7+
8+
Product get result;
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'builder.dart';
2+
import 'product.dart';
3+
4+
class Director {
5+
Product construct(Builder builder) {
6+
builder.buildPart1();
7+
builder.buildPart2();
8+
builder.buildPart3();
9+
10+
return builder.result;
11+
}
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
abstract class Product {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import '../pattern/product.dart';
2+
3+
class ConcreteProduct1 implements Product {
4+
ConcreteProduct1(String name) {
5+
_buff.add(name);
6+
}
7+
8+
void addLine(String name) {
9+
final index = _buff.length.toString().padLeft(3, '0');
10+
_buff.add('$index: $name');
11+
}
12+
13+
@override
14+
String toString() {
15+
return '${_buff.join('\n')}\n';
16+
}
17+
18+
final _buff = <String>[];
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import '../pattern/product.dart';
2+
3+
class ConcreteProduct2 implements Product {
4+
ConcreteProduct2(String name) {
5+
_buff.add(name);
6+
}
7+
8+
void addLine(String name) {
9+
final num = ['1️⃣', '2️⃣', '3️⃣'][_buff.length - 1];
10+
_buff.add('$num: $name');
11+
}
12+
13+
@override
14+
String toString() {
15+
return '${_buff.join('\n')}\n';
16+
}
17+
18+
final _buff = <String>[];
19+
}

0 commit comments

Comments
 (0)