Skip to content

Commit

Permalink
feat: finish creational pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmw committed Feb 6, 2024
1 parent 41bdb69 commit 4c08264
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 34 deletions.
77 changes: 60 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.

<!-- TOC -->

- [1. 软件设计模式](#1-软件设计模式)
- [行为](#行为)
- [结构](#结构)
- [创造(6)](#创造6)
- [simple factory(简单工厂)](#simple-factory简单工厂)
- [factory method(工厂方法)](#factory-method工厂方法)
- [参考](#参考)

<!-- /TOC -->

# 1. 软件设计模式
---
<img src="./lib/assets/images/three_type.png" style="width:500px;background:white"/>
Expand Down Expand Up @@ -40,17 +28,16 @@ in `lib/`, and example unit test in `test/`.
- 实例一个或者一组对象
- 当创造Object的时候频率比较高且需要一定的逻辑导致任务繁忙的时候,就需要看是否能够控制Object的创建解决创建的问题

#### simple factory(简单工厂)
#### 1. simple factory(简单工厂)

- explanation
- 要什么直接给给什么,屏蔽内部细节
- 组成
- 产品接口
- 工厂
- 工厂根据传来的标识返回生成出对应的产品


#### factory method(工厂方法)
#### 2. factory method(工厂方法)

- explanation
- 将自己的大量任务delegate(分派)出去
Expand All @@ -61,7 +48,7 @@ in `lib/`, and example unit test in `test/`.
- 工厂接口
- 一个工厂对应一个产品

#### abstract factory(抽象工厂模式)
#### 3. abstract factory(抽象工厂模式)

- explanation
- 抽象工厂模式的主要目的是创建一系列相关或相互依赖的对象(意思是仅仅生产subclass)
Expand All @@ -73,7 +60,7 @@ in `lib/`, and example unit test in `test/`.
- 具体的产品,具体的工厂
- 一个工厂里面可以生产多种产品

#### builder 模式
#### 4. builder 模式

- explanation
- Builder 模式是一种创建型设计模式,主要用于创建一个复杂对象,通过一步一步的构建,最终得到完整的对象。它将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。
Expand All @@ -85,6 +72,62 @@ in `lib/`, and example unit test in `test/`.
*实体类*</span>)
- 3.Director, 根据传来的 builder 构造对应的实体类

#### 5. prototype 模式

- explanation
- Create object based on an existing object through cloning.
```dart
import 'dart:convert';

/// 根据原型进行深拷贝
/// 通过克隆在现有对象的基础上创建对象。
/// dart 标准库没有提供 copy 相关的功能,需要使用第三方库或者自己实现
abstract class Cloneable<T> {
T clone();
}

class ProductPrototype implements Cloneable<ProductPrototype> {
String? typeName;
List infoList = <dynamic>[];

ProductPrototype();

ProductPrototype.setAll(this.typeName, this.infoList);

@override
ProductPrototype clone() {
// TODO: implement clone
List infoListCloned = [];
infoListCloned.addAll(infoList);
return ProductPrototype.setAll(typeName, infoListCloned);
}

Map<String, dynamic> toJson() => {'typeName': typeName, 'infoList': infoList};

@override
String toString() => jsonEncode(toJson());
}

void main() {
ProductPrototype productPrototype =
ProductPrototype.setAll("typeA", [1, 2, 3, 4, 5, 6]);
print(productPrototype); // {"typeName":"typeA","infoList":[1,2,3,4,5,6]}

ProductPrototype clone = productPrototype.clone();
clone.infoList[0] = 0;
print(productPrototype); // {"typeName":"typeA","infoList":[1,2,3,4,5,6]}
print(clone); //{"typeName":"typeA","infoList":[0,2,3,4,5,6]}
}

```

#### 6. singleton 模式

- explanation
- Ensures that only one object of a particular class is ever created.
- 可以看成一个班级里面的班主任,学生有事情都需要向班主任汇报,班主任始终是一个班主任
- 可以有5种实现方法

# 参考以及资源

- [github](https://github.com/kamranahmedse/design-patterns-for-humans?tab=readme-ov-file#creational-design-patterns)
Expand Down
13 changes: 1 addition & 12 deletions bin/design_pattern.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'package:design_pattern/design_pattern.dart' as design_pattern;

int fib(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}

void main(List<String> arguments) {
int tStart = DateTime.timestamp().millisecondsSinceEpoch;
print(fib(40));
print(DateTime.timestamp().millisecondsSinceEpoch - tStart);
print(arguments);
}
File renamed without changes.
41 changes: 41 additions & 0 deletions lib/creational_6_types/prototype/prototype.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:convert';

/// 根据原型进行深拷贝
/// 通过克隆在现有对象的基础上创建对象。
/// dart 标准库没有提供 copy 相关的功能,需要使用第三方库或者自己实现
abstract class Cloneable<T> {
T clone();
}

class ProductPrototype implements Cloneable<ProductPrototype> {
String? typeName;
List infoList = <dynamic>[];

ProductPrototype();

ProductPrototype.setAll(this.typeName, this.infoList);

@override
ProductPrototype clone() {
// TODO: implement clone
List infoListCloned = [];
infoListCloned.addAll(infoList);
return ProductPrototype.setAll(typeName, infoListCloned);
}

Map<String, dynamic> toJson() => {'typeName': typeName, 'infoList': infoList};

@override
String toString() => jsonEncode(toJson());
}

void main() {
ProductPrototype productPrototype =
ProductPrototype.setAll("typeA", [1, 2, 3, 4, 5, 6]);
print(productPrototype); // {"typeName":"typeA","infoList":[1,2,3,4,5,6]}

ProductPrototype clone = productPrototype.clone();
clone.infoList[0] = 0;
print(productPrototype); // {"typeName":"typeA","infoList":[1,2,3,4,5,6]}
print(clone); //{"typeName":"typeA","infoList":[0,2,3,4,5,6]}
}
16 changes: 16 additions & 0 deletions lib/creational_6_types/singleton/eager_singleton.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class EagerSingleton {
static final EagerSingleton _eagerSingleton = EagerSingleton._single();

static getEagerSingleton() => _eagerSingleton;

EagerSingleton._single();
}

void main() {
EagerSingleton singleton1 = EagerSingleton.getEagerSingleton();
EagerSingleton singleton2 = EagerSingleton.getEagerSingleton();

print(identical(singleton1, singleton2));
print('${identityHashCode(singleton1)}, ${singleton1.hashCode}');
print(identityHashCode(singleton2));
}
22 changes: 22 additions & 0 deletions lib/creational_6_types/singleton/lazy_singleton.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class LazySingleton {
static LazySingleton? _lazySingleton;

LazySingleton._();

static LazySingleton getLazySingleton() {
_lazySingleton ??= LazySingleton._();
return _lazySingleton!;
}
}


void main() {
LazySingleton singleton1 = LazySingleton.getLazySingleton();
LazySingleton singleton2 = LazySingleton.getLazySingleton();
LazySingleton singleton3 = LazySingleton.getLazySingleton();
print(singleton1 == singleton2);
print(singleton2 == singleton3);
print(singleton1 == singleton3);

print(identical(singleton1, singleton2));
}
18 changes: 18 additions & 0 deletions lib/creational_6_types/singleton/lazy_singleton_with_factory.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// 使用懒汉模式
class FactorySingleton {
static FactorySingleton? _instance;

FactorySingleton._();

factory FactorySingleton() {
_instance ??= FactorySingleton._();
return _instance!;
}
}

void main() {
FactorySingleton factorySingleton1 = FactorySingleton();
FactorySingleton factorySingleton2 = FactorySingleton();

print(identical(factorySingleton1, factorySingleton2));
}
4 changes: 1 addition & 3 deletions lib/design_pattern.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
int calculate() {
return 6 * 7;
}
export './creational_6_types/singleton/lazy_singleton_with_factory.dart';
2 changes: 2 additions & 0 deletions question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1. constructor
2. identical hashCode identicalHashcode
3 changes: 1 addition & 2 deletions test/design_pattern_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:design_pattern/design_pattern.dart';
import 'package:test/test.dart';

void main() {
test('calculate', () {
expect(calculate(), 42);
expect(42, 42);
});
}

0 comments on commit 4c08264

Please sign in to comment.