Skip to content

Commit 612a191

Browse files
committed
feat: finish two creational design pattern
0 parents  commit 612a191

17 files changed

+1005
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Dart_Packages.xml

+388
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Dart_SDK.xml

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A sample command-line application with an entrypoint in `bin/`, library code
2+
in `lib/`, and example unit test in `test/`.

analysis_options.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/// 有两个<接口>
2+
/// 1. 一个是 被工厂生产的 Object 的接口
3+
/// 2. 一个是工厂的 接口
4+
5+
6+
/// 被工厂生产的 Object 的 接口
7+
abstract class Product{
8+
void produce();
9+
}
10+
11+
class ConcreteProductA implements Product{
12+
@override
13+
void produce() {
14+
// TODO: implement produce
15+
print('Product A is producing');
16+
}
17+
}
18+
19+
20+
class ConcreteProductB implements Product{
21+
@override
22+
void produce() {
23+
// TODO: implement produce
24+
print('Product B is producing');
25+
}
26+
}
27+
28+
/// 生产 Object 的工厂的接口
29+
abstract class ProductFactory{
30+
Product createProduct();
31+
}
32+
33+
class ConcreteProductAFactory implements ProductFactory{
34+
@override
35+
Product createProduct() {
36+
// TODO: implement createProduct
37+
return ConcreteProductA();
38+
}
39+
}
40+
41+
class ConcreteProductBFactory implements ProductFactory{
42+
@override
43+
Product createProduct() {
44+
// TODO: implement createProduct
45+
return ConcreteProductB();
46+
}
47+
}
48+
49+
void main(){
50+
ProductFactory productAFactory = ConcreteProductAFactory();
51+
Product productA = productAFactory.createProduct();
52+
productA.produce();
53+
54+
ProductFactory productBFactory = ConcreteProductBFactory();
55+
Product productB = productBFactory.createProduct();
56+
productB.produce();
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// dart 语言本身的 factory 关键字就是工厂模式的体现
2+
/// 仅仅一个被生产 Object 的接口
3+
abstract class Animal {
4+
void run();
5+
}
6+
7+
class Dog implements Animal {
8+
@override
9+
void run() {
10+
// TODO: implement run
11+
print("Dog run");
12+
}
13+
}
14+
15+
class Cat implements Animal {
16+
@override
17+
void run() {
18+
// TODO: implement run
19+
print('Cat run');
20+
}
21+
}
22+
/// 在不可以函数编程的编程语言里面使用静态内部类实现
23+
Animal? animalFactory(String animalName) {
24+
if (animalName.toLowerCase() == 'dog') {
25+
return Dog();
26+
}else if(animalName.toLowerCase() == 'cat'){
27+
return Cat();
28+
}else{
29+
throw Exception("illegal arg");
30+
}
31+
}
32+
33+
void main() {
34+
/// 下面两个创建的可以使用
35+
/// 1、可以为 null 使用 ?
36+
/// 2、强制指定非空 使用 !
37+
// 生产猫
38+
Animal? cat = animalFactory('cat');
39+
// cat run
40+
cat?.run();
41+
42+
// 生产狗
43+
Animal dog = animalFactory('dog')!;
44+
// dog run
45+
dog.run();
46+
47+
/// 测试不存在的
48+
// 测试 error
49+
Animal other = animalFactory('other')!;
50+
other.run();
51+
}

bin/design_pattern.dart

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:design_pattern/design_pattern.dart' as design_pattern;
2+
3+
void main(List<String> arguments) {
4+
assert(1 != 1 );
5+
}

design_pattern.iml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="WEB_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
7+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
8+
<excludeFolder url="file://$MODULE_DIR$/build" />
9+
</content>
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="Dart SDK" level="project" />
12+
<orderEntry type="library" name="Dart Packages" level="project" />
13+
</component>
14+
</module>

lib/design_pattern.dart

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int calculate() {
2+
return 6 * 7;
3+
}

0 commit comments

Comments
 (0)