-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
162 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
lib/creational_6_types/singleton/lazy_singleton_with_factory.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1. constructor | ||
2. identical hashCode identicalHashcode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |