Skip to content

Commit 437ae15

Browse files
author
聂彬
committed
fix(): 修改装饰模式
1 parent c435017 commit 437ae15

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

lib/page/decorator/decorator_mode.dart

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,53 @@ import 'package:flutter/cupertino.dart';
33
/// Created by NieBin on 2020-03-24
44
/// Github: https://github.com/nb312
55
6-
abstract class Building {
7-
final IPaint paint;
8-
9-
Building(this.paint);
6+
///
107
8+
abstract class Building {
119
String decorate();
1210
}
1311

14-
abstract class IPaint {
15-
String decoration();
12+
class Paint implements Building {
13+
final Building building;
14+
15+
Paint(this.building);
16+
17+
@override
18+
String decorate() {
19+
return "1.${building.decorate()}";
20+
}
1621
}
1722

1823
///商业建筑
1924
class BusinessBuilding extends Building {
20-
BusinessBuilding(IPaint paint) : super(paint);
21-
2225
@override
2326
String decorate() {
24-
return "装修商业楼\n${paint.decoration()}";
27+
return "装修商业楼";
2528
}
2629
}
2730

2831
///居民楼
2932
class PersonBuilding extends Building {
30-
PersonBuilding(IPaint paint) : super(paint);
31-
3233
@override
3334
String decorate() {
34-
return "装修居民楼\n${paint.decoration()}";
35+
return "装修居民楼";
3536
}
3637
}
3738

38-
class RedPaint extends IPaint {
39+
class RedPaint extends Paint {
40+
RedPaint(Building building) : super(building);
41+
3942
@override
40-
String decoration() {
41-
return "粉刷为红色";
43+
String decorate() {
44+
return "${super.decorate()}\n 2.粉刷为红色";
4245
}
4346
}
4447

45-
class GreenPaint extends IPaint {
48+
class GreenPaint extends Paint {
49+
GreenPaint(Building building) : super(building);
50+
4651
@override
47-
String decoration() {
48-
return "粉刷为绿色";
52+
String decorate() {
53+
return "${super.decorate()}\n 2.粉刷为绿色";
4954
}
5055
}

lib/page/decorator/decorator_page.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class _DecoratorPageState extends State<DecoratorPage> {
4848
onPressed: () {
4949
setState(() {
5050
isBusinessEven = !isBusinessEven;
51-
Building building = BusinessBuilding(
52-
isBusinessEven ? RedPaint() : GreenPaint());
53-
businessStr = building.decorate();
51+
Paint p = isBusinessEven
52+
? RedPaint(BusinessBuilding())
53+
: GreenPaint(BusinessBuilding());
54+
businessStr = p.decorate();
5455
});
5556
},
5657
child: Text(
@@ -81,9 +82,10 @@ class _DecoratorPageState extends State<DecoratorPage> {
8182
onPressed: () {
8283
setState(() {
8384
isPersonEven = !isPersonEven;
84-
Building building = PersonBuilding(
85-
isPersonEven ? RedPaint() : GreenPaint());
86-
personStr = building.decorate();
85+
Paint p = isPersonEven
86+
? RedPaint(PersonBuilding())
87+
: GreenPaint(PersonBuilding());
88+
personStr = p.decorate();
8789
});
8890
},
8991
child: Text(

0 commit comments

Comments
 (0)