案例介紹
一個(gè)有著基本功能的購物車程序長什么樣子:
//定義商品Item類
class Item {
double price;
String name;
Item(name, price) {
this.name = name;
this.price = price;
}
}
//定義購物車類
class ShoppingCart {
String name;
DateTime date;
String code;
List<Item> bookings;
price() {
double sum = 0.0;
for(var i in bookings) {
sum += i.price;
}
return sum;
}
ShoppingCart(name, code) {
this.name = name;
this.code = code;
this.date = DateTime.now();
}
getInfo() {
return '購物車信息:' +
'\n-----------------------------' +
'\n用戶名: ' + name+
'\n優(yōu)惠碼: ' + code +
'\n總價(jià): ' + price().toString() +
'\n日期: ' + date.toString() +
'\n-----------------------------';
}
}
void main() {
ShoppingCart sc = ShoppingCart('張三', '123456');
sc.bookings = [Item('蘋果',10.0), Item('鴨梨',20.0)];
print(sc.getInfo());
}
類抽象改造
class Item {
double price;
String name;
Item(this.name, this.price);
}
class ShoppingCart {
String name;
DateTime date;
String code;
List<Item> bookings;
price() {...}
//刪掉了構(gòu)造函數(shù)函數(shù)體
ShoppingCart(this.name, this.code) : date = DateTime.now();
...
}
類抽象改造
class Item {
double price;
String name;
Item(this.name, this.price);
}
class ShoppingCart {
String name;
DateTime date;
String code;
List<Item> bookings;
price() {...}
//刪掉了構(gòu)造函數(shù)函數(shù)體
ShoppingCart(this.name, this.code) : date = DateTime.now();
...
}
抽象 name, price:
class Meta {
double price;
String name;
Meta(this.name, this.price);
}
class Item extends Meta{
Item(name, price) : super(name, price);
}
class ShoppingCart extends Meta{
DateTime date;
String code;
List<Item> bookings;
double get price {...}
ShoppingCart(name, this.code) : date = DateTime.now(),super(name,0);
getInfo() {...}
}
方法改造
我們先看看 price 屬性的 get 方法:
double get price {
double sum = 0.0;
for(var i in bookings) {
sum += i.price;
}
return sum;
}
使用 Dart 的箭頭函數(shù)來進(jìn)一步簡化實(shí)現(xiàn)函數(shù):
class Item extends Meta{
...
//重載了+運(yùn)算符,合并商品為套餐商品
Item operator+(Item item) => Item(name + item.name, price + item.price);
}
class ShoppingCart extends Meta{
...
//把迭代求和改寫為歸納合并
double get price => bookings.reduce((value, element) => value + element).price;
...
getInfo() {...}
}
getInfo 方法如何優(yōu)化:
getInfo () => '''
購物車信息:
-----------------------------
用戶名: $name
優(yōu)惠碼: $code
總價(jià): $price
Date: $date
-----------------------------
''';
對(duì)象初始化方式的優(yōu)化
class ShoppingCart extends Meta{
...
//默認(rèn)初始化方法,轉(zhuǎn)發(fā)到withCode里
ShoppingCart({name}) : this.withCode(name:name, code:null);
//withCode初始化方法,使用語法糖和初始化列表進(jìn)行賦值,并調(diào)用父類初始化方法
ShoppingCart.withCode({name, this.code}) : date = DateTime.now(), super(name,0);
//??運(yùn)算符表示為code不為null,則用原值,否則使用默認(rèn)值"沒有"
getInfo () => '''
購物車信息:
-----------------------------
用戶名: $name
優(yōu)惠碼: ${code??"沒有"}
總價(jià): $price
Date: $date
-----------------------------
''';
}
void main() {
ShoppingCart sc = ShoppingCart.withCode(name:'張三', code:'123456');
sc.bookings = [Item('蘋果',10.0), Item('鴨梨',20.0)];
print(sc.getInfo());
ShoppingCart sc2 = ShoppingCart(name:'李四');
sc2.bookings = [Item('香蕉',15.0), Item('西瓜',40.0)];
print(sc2.getInfo());
}
增加 PrintHelper 類,并調(diào)整 ShoppingCart 的聲明:
abstract class PrintHelper {
printInfo() => print(getInfo());
getInfo();
}
class ShoppingCart extends Meta with PrintHelper{
...
}
class Meta {
double price;
String name;
//成員變量初始化語法糖
Meta(this.name, this.price);
}
class Item extends Meta{
Item(name, price) : super(name, price);
//重載+運(yùn)算符,將商品對(duì)象合并為套餐商品
Item operator+(Item item) => Item(name + item.name, price + item.price);
}
abstract class PrintHelper {
printInfo() => print(getInfo());
getInfo();
}
//with表示以非繼承的方式復(fù)用了另一個(gè)類的成員變量及函數(shù)
class ShoppingCart extends Meta with PrintHelper{
DateTime date;
String code;
List<Item> bookings;
//以歸納合并方式求和
double get price => bookings.reduce((value, element) => value + element).price;
//默認(rèn)初始化函數(shù),轉(zhuǎn)發(fā)至withCode函數(shù)
ShoppingCart({name}) : this.withCode(name:name, code:null);
//withCode初始化方法,使用語法糖和初始化列表進(jìn)行賦值,并調(diào)用父類初始化方法
ShoppingCart.withCode({name, this.code}) : date = DateTime.now(), super(name,0);
//??運(yùn)算符表示為code不為null,則用原值,否則使用默認(rèn)值"沒有"
@override
getInfo() => '''
購物車信息:
-----------------------------
用戶名: $name
優(yōu)惠碼: ${code??"沒有"}
總價(jià): $price
Date: $date
-----------------------------
''';
}
void main() {
ShoppingCart.withCode(name:'張三', code:'123456')
..bookings = [Item('蘋果',10.0), Item('鴨梨',20.0)]
..printInfo();
ShoppingCart(name:'李四')
..bookings = [Item('香蕉',15.0), Item('西瓜',40.0)]
..printInfo();
}

公眾號(hào):碼農(nóng)架構(gòu)