✨コンストラクタとシングルトン - Ki-Kobayashi/flutter_wiki GitHub Wiki
🟩 Dartにおけるコンストラクタの種類
-
通常のコンストラクタ
-
名前付きコンストラクタ
👉 名前を通じてコンストラクタの目的や動作を明確に示せる -
factoryコンストラクタ
👉 シングルトンの仕組みを作るのに使用できる
👉 インスタンス生成時に特別なロジックが必要な場合に利用
👉 共通のインターフェイスとなり、if-elseコードを削減できる
👉 名前付きにすることも可能
.
🟡 通常のコンストラクタ
今回は、Personという人を形作るクラスを例に挙げる 引用:https://pryogram.com/flutter/constructor-summary/#toc7
class Person {
String? name;
int? age;
Person(this.name, this.age); //この行がコンストラクタ
}
void main() {
Person _personInstance = Person('pRyogram', 25);
print('${_personInstance.name}は${_personInstance.age}歳です');
}
.
🟡 名前付きコンストラクタ
👉 名前を通じてコンストラクタの目的や動作を明確に示せる
class Rectangle {
int width;
int height;
int left;
int top;
// 通常のコンストラクタ
Rectangle(this.left, this.top, this.width, this.height);
// 名前付きコンストラクタ: 正方形を生成
Rectangle.square(int size)
: width = size,
height = size,
left = 0,
top = 0;
// 名前付きコンストラクタ: 2つの点からRectangleを生成
Rectangle.fromPoints(Point a, Point b)
: left = a.x,
top = a.y,
width = b.x - a.x,
height = b.y - a.y;
}
var square = Rectangle.square(4); // 👈 これは正方形であることが明確
var rect = Rectangle.fromPoints(Point(0, 0), Point(2, 4)); // 👈 2つの点から生成されることが明確
.
🟡 factoryコンストラクタ
※引用:https://zenn.dev/10_tofu_01/articles/cb0c21f18f78da
👉 シングルトンの仕組みを作るのに使用できる(※後述)
👉 インスタンス生成時に特別なロジックが必要な場合に利用
👉 共通のインターフェイスとなり、if-elseコードを削減できる
👉 名前付きにすることも可能
class Drink{
int ammount;
Drink(this.ammount);
factory Drink.createDrink({required int ammount, bool breakTime = false})
=> breakTime ? Coffee(ammount) : Water(ammount);
}
class Water extends Drink {
Water(int ammount) : super(ammount);
}
class Coffee extends Drink {
Coffee(int ammount) : super(ammount);
}
void main() {
// 👇Drink型として、まとめられる(Coffee(or Water)のインスタンスを Drink 型で返しているのであって、
// Coffee(or Water)として返しているわけではない【共通インターフェース】)
Drink myBreakDrink = Drink.createDrink(ammount: 200, breakTime: true);
// Instance of 'Coffee'
Drink mySportDrink = Drink.createDrink(ammount: 500);
// Instance of 'Water'
}
.
もし、factory を使用しない場合は、以下のように if-else
が各所で多用されてしまう・・・
void main() {
Drink drink;
int ammount = 250;
bool isBreak = true;
isBreak ? drink = Coffee(ammount) : drink = Water(ammount);
// Instance of 'Coffee'
}
.
🟩 factoryコンストラクタによるシングルトン
🟡 変更"なし"のシングルトン例
static 変数箇所に**「final」は 必須 **
class MySingleton {
static final MySingleton _instance = MySingleton._internal();
factory MySingleton() {
return _instance;
}
MySingleton._internal();
}
.
🟡 変更"有り"のシングルトン例
static 変数箇所に**「final」は いらない **
class Person {
static Person? _personInstance;
String? name;
int? age;
Person._(this.name, this.age);
factory Person(String name, int age) {
if(_personInstance == null) {
_personInstance = Person._(name, age);
}
return _personInstance!;
}
}
void main() {
Person _personInstance1 = Person('pRyogram', 25);
Person _personInstance2 = Person('a', 20);
print('${_personInstance1.name}は${_personInstance1.age}歳です');
print('${_personInstance2.name}は${_personInstance2.age}歳です');
}
.
🟩 Dart3の新機能
https://zenn.dev/yumemi_inc/articles/dart3-class-modifiers
◆公式
https://dart.dev/language/constructors
🟡
.
🟩
🟡
.
🟩
🟡
.
🟩
🟡
.
🟩
🟡
.
🟩
🟡
.
🟩
🟡
.
🟩
🟡
.