ファクトリコンストラクタ
CoddyのDartジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 14/110。
factory constructorは、常に新しいinstanceを作成するとは限らない特殊なconstructorです。通常のconstructorとは異なり、factory constructorは既存のinstance、サブタイプ、あるいは(nullable の場合)null さえも返すことができます。factoryキーワードを使用して宣言します。
class Logger {
static final Logger _instance = Logger._internal();
// プライベートな名前付きコンストラクタ
Logger._internal();
// ファクトリコンストラクタ
factory Logger() {
return _instance;
}
}
Logger log1 = Logger();
Logger log2 = Logger();
print(identical(log1, log2)); // trueこの例では、factory constructor は常に同じ instance を返します。Logger() を何度 call しても、identical なオブジェクトを取得します。このパターンは、singleton の実装によく使用されます。
Factory constructor は通常の constructor と重要な点で異なります。新しい instance をまったく作成しない場合があるため、this にアクセスできません。その代わり、object を明示的に返す必要があります。
class Shape {
factory Shape(String type) {
if (type == 'circle') return Circle();
if (type == 'square') return Square();
return Rectangle();
}
}
class Circle extends Shape { Circle() : super._(); }
class Square extends Shape { Square() : super._(); }
class Rectangle extends Shape { Rectangle() : super._(); }ファクトリーコンストラクターは、キャッシュされたオブジェクトの返却、オブジェクトプールの実装、入力パラメーターに基づくインスタンス化するサブタイプの決定など、インスタンスの作成を制御する必要がある場合に便利です。
チャレンジ
簡単単純な文字列入力に基づいて、factory constructor を使用してさまざまな種類の通知を Create する通知システムを構築しましょう。
コードを整理するために、2つのファイルを Create します。
notification.dart: すべての通知タイプの base として機能するNotificationclass を Define します。class には次の要素を含めます。String messagefield- message を initializes する private named constructor
Notification._internal typeparameter に基づいて異なる通知サブタイプを返す factory constructorNotification(String type, String message):- type が
'email'の場合、EmailNotificationを返す - type が
'sms'の場合、SMSNotificationを返す - otherwise、
PushNotificationを返す
- type が
Sending: [message]を prints するsend()method
同じファイルで、Notification を extend する3つのサブクラスを Create します。
EmailNotification:send()を Override してEmail: [message]を prints するSMSNotification:send()を Override してSMS: [message]を prints するPushNotification:send()を Override してPush: [message]を prints する
各サブクラスには、message を受け取り、super._internal(message) を使用して親に渡す constructor が必要です。
main.dart: notification class を import し、factory constructor を使用して3つの通知を Create します。'Welcome!'を message とする email 通知'Your code is 1234'を message とする SMS 通知'New update available'を message とする push 通知
send()を Call します。
Expected output:
Email: Welcome!
SMS: Your code is 1234
Push: New update available自分で試してみよう
import 'notification.dart';
void main() {
// TODO: メッセージ 'Welcome!' のメール通知を作成する
// TODO: Create an SMS notification with message 'Your code is 1234'
// TODO: Create a push notification with message 'New update available'
// TODO: 各通知に対して順番に send() を呼び出す
}
このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
オブジェクト指向プログラミングのすべてのレッスン
4Null Safety
Null Safety 入門Nullable と Non-Nullable? 演算子と ! 演算子late キーワードと Null SafetyNull-Aware 演算子クラスにおける Null Safetyまとめ:ユーザープロフィールシステム10コレクションとジェネリクス
List, Set, Map の概要型安全なコレクションジェネリッククラスジェネリックメソッドジェネリクスの制約Iterable と Iteratorまとめ:ジェネリックなストレージ自分で練習してみよう: Dartオンラインコンパイラ