AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

從Flutter范兒的單例來看Dart的構(gòu)造函數(shù)

來源: 責編: 時間:2024-05-07 09:03:56 156觀看
導讀單例模式單例模式應(yīng)該是設(shè)計模式中使用的最廣泛的一種設(shè)計模式了,在Kotlin中,甚至為它單獨創(chuàng)建了一個語法糖——object類,來快速實現(xiàn)單例模式,而在Dart中,并沒有像Kotlin這樣的語法糖,所以,參考單例的一般實現(xiàn),我們可以很容易

單例模式

單例模式應(yīng)該是設(shè)計模式中使用的最廣泛的一種設(shè)計模式了,在Kotlin中,甚至為它單獨創(chuàng)建了一個語法糖——object類,來快速實現(xiàn)單例模式,而在Dart中,并沒有像Kotlin這樣的語法糖,所以,參考單例的一般實現(xiàn),我們可以很容易的實現(xiàn)下面這樣一個單例。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Singleton {  static Singleton? _instance;  // 私有的命名構(gòu)造函數(shù)  Singleton._private() {    // TODO  }  static Singleton getInstance() {    if (_instance == null) {      _instance = Singleton._private();    }    return _instance!;  }}

上面的代碼與大部分編程語言的代碼都差不多,不外乎就是單例的幾個特點:B6a28資訊網(wǎng)——每日最新資訊28at.com

  • 私有構(gòu)造函數(shù)
  • 靜態(tài)instance訪問

在Dart中,變量和函數(shù)前面加上「_」就代表私有,但這個私有實際上的含義是「只能在當前文件中訪問」,所以,如果在當前文件中,你依然是可以訪問這個私有變量或者函數(shù)的。另外,由于Dart是單線程模型,所以也不存在線程安全的問題,不用做線程控制。B6a28資訊網(wǎng)——每日最新資訊28at.com

上面的代碼,作為一個Dart初學者來說,是無可厚非的,但是對于老司機來說,明顯沒有Flutter范兒,所以,我們借助Dart的語法糖,來改造下上面的單例代碼。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Singleton {  static Singleton? _instance;  // 私有的命名構(gòu)造函數(shù)  Singleton._private() {    // TODO  }  static Singleton get instance => _instance ??= Singleton._private();}

首先,通過「??=」來簡化空判斷,其次,通過get函數(shù)來獲取實例,將instance函數(shù)變成了instance變量。這樣一來,代碼簡化了不少,而且也更加簡單了。B6a28資訊網(wǎng)——每日最新資訊28at.com

不過,這依然不是最具Flutter范兒的單例寫法,在Dart中,它提供了一個factory關(guān)鍵字,與Kotlin中的object關(guān)鍵字,有異曲同工之妙,我們來看看官方推薦的單例寫法。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Singleton {  static final Singleton _singleton = Singleton._internal();  factory Singleton() => _singleton;  Singleton._internal() {    // TODO  }}

?所謂的factory constructor,它的作用是「僅在第一次創(chuàng)建實例,后續(xù)都返回相同的實例」,這不就是天然的單例嗎,所以,借助factory constructor,我們可以很方便的寫出一個Flutter范兒的單例。B6a28資訊網(wǎng)——每日最新資訊28at.com

構(gòu)造函數(shù)

構(gòu)造函數(shù)是一個類在初始化時,主動調(diào)用的函數(shù),在Dart中,有多種不同的構(gòu)造函數(shù),它們在不同的場景下使用,可以極大的簡化我們的代碼,同時也讓我們的代碼更加具有Flutter范兒。B6a28資訊網(wǎng)——每日最新資訊28at.com

默認構(gòu)造函數(shù)

缺省構(gòu)造函數(shù)不用自己創(chuàng)建,如果一個類沒有構(gòu)造函數(shù),那么它會自動添加一個,它什么都不做。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Default Constructorclass Test {  String name = 'xys';  Test();}

在構(gòu)造函數(shù)中初始化變量

Dart提供了多種不同的方式在構(gòu)造函數(shù)中未變量賦值,其中最簡單的,就是在構(gòu)造時初始變量。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with parametersclass Test {  String name;  Test(this.name);}

其實Test(this.name)實際上就是Test(String name){this.name = name}的簡化寫法。B6a28資訊網(wǎng)——每日最新資訊28at.com

同時,構(gòu)造函數(shù)也可以增加方法體,進行一些初始化邏輯。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with the initial methodclass Test {  String name;  Test(this.name) {    // TODO  }}

?當你需要在構(gòu)造函數(shù)初始化時給變量賦值時,可以通過initializer list來實現(xiàn)。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with initializerclass Test {  String name;    Test(name) : name = handleSth(name);    static String handleSth(String e) => e.toUpperCase();}

initializer list可以初始化多個變量,它們之間可以使用「,」進行分隔,如果有super構(gòu)造器,那么它一般放在最后。B6a28資訊網(wǎng)——每日最新資訊28at.com

如果你要override基類的變量,那么可以通過super關(guān)鍵字來覆寫。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with super()class Base {  String id;  Base(this.id);}class Test extends Base {  String name;  Test(this.name, String id) : super(id);}

另外,構(gòu)造函數(shù)中,還支持通過Asserts?來做一些檢查。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with assertionclass Test {  String name;  Test(this.name) : assert(name.length > 3);}

對于Dart的參數(shù)來說,通常我們設(shè)置的都是必選參數(shù),就是類似我們上面的這些參數(shù),而在Dart中,還可以設(shè)置可選參數(shù)。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Test {  String name;  Test(this.name, [int sex = 0]);}Test('xys', 1);

或者你覺得可選參數(shù)在使用時的語義不太明確,那么你可以使用具名參數(shù)。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Test {  String name;  Test(this.name, {int sex = 0});}Test('xys', sex: 1);

這樣在使用時,語義會更加明確。B6a28資訊網(wǎng)——每日最新資訊28at.com

私有構(gòu)造函數(shù)

私有構(gòu)造函數(shù),除了我們前面提到的單例使用場景外,下面這個場景,也使用的很多。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Utils {  Utils._();  static void log(String message) => print(message);}

通過私有構(gòu)造函數(shù),我們可以避免使用者創(chuàng)建工具類的實例,而是讓使用者直接調(diào)用靜態(tài)函數(shù)。B6a28資訊網(wǎng)——每日最新資訊28at.com

具名構(gòu)造函數(shù)

具名構(gòu)造函數(shù)可以給當前的構(gòu)造邏輯起一個別名,方便調(diào)用者通過語義來進行調(diào)用。B6a28資訊網(wǎng)——每日最新資訊28at.com

// Constructor with this()class Test {  String name;  int sex;  Test(this.name, this.sex);  Test.boy(String name) : this(name, 1);  Test.girl(String name) : this(name, 0);}

const構(gòu)造函數(shù)

const構(gòu)造函數(shù)在Flutter中使用的非常多,因為一個const構(gòu)造函數(shù)是不可變的,const構(gòu)造函數(shù)在運行時會指向內(nèi)存空間的同一個對象,從而提高代碼執(zhí)行的效率,所以,在Flutter中,如果一個Widget是可以定義為const的,那就把它定義為const吧。B6a28資訊網(wǎng)——每日最新資訊28at.com

factory構(gòu)造函數(shù)

factory constructor前面我們已經(jīng)講解過了,它可以從另一個構(gòu)造函數(shù),或者是其它類,返回一個唯一的實例。最常用的場景就是單例的使用,我們來看下它的另一個使用場景,即從緩存中返回唯一實例。B6a28資訊網(wǎng)——每日最新資訊28at.com

class Test {  final String name;  static final _cache = <String, Test>{};  Test._(this.name);  factory Test(name) => _cache[name] ??= Test._(name);}

factory構(gòu)造函數(shù)與static method的區(qū)別

在大部分時間,這兩者都是非常類似的,甚至是可以混用的,但是它們之間,還是有一些區(qū)別的。B6a28資訊網(wǎng)——每日最新資訊28at.com

對于factory constructor來說,它不需要命名,也不用指定通用參數(shù),這樣可以減少很多模板代碼,我們來看下面這個例子。B6a28資訊網(wǎng)——每日最新資訊28at.com

class ComplexClass<Value, Notifier extends ValueNotifier<Value>> {}

在這個例子中,它包含一個比較復雜的泛型,如果我們要創(chuàng)建一個靜態(tài)工廠,那么就需要這樣:B6a28資訊網(wǎng)——每日最新資訊28at.com

class ComplexClass<Value, Notifier extends ValueNotifier<Value>> {  static ComplexClass<Value, Notifier> someFactory<ComplexClass<Value, Notifier extends ValueNotifier<Value>>() {    // TODO: return a ComplexClass instance  }}

我們需要創(chuàng)建很復雜的參數(shù)類型,但是使用factory constructor,則可以避免這些模板代碼。B6a28資訊網(wǎng)——每日最新資訊28at.com

class ComplexClass<Value, Notifier extends ValueNotifier<Value>> {  factory ComplexClass.someFactory() {    // TODO: return a ComplexClass instance  }}


B6a28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-86976-0.html從Flutter范兒的單例來看Dart的構(gòu)造函數(shù)

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 用Go語言做了一個分布式限流器, 看看實現(xiàn)的方法與步驟

下一篇: C++強制類型轉(zhuǎn)換詳解:四種操作符解析與實例演示

標簽:
  • 熱門焦點
Top