Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 398dc157d2 |
+35
-54
@@ -20,49 +20,32 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ListStrategy<T> {
|
||||
List<T> compose(List<T> items);
|
||||
abstract class ShapeFactory {
|
||||
Widget build(Size size, Color color);
|
||||
}
|
||||
|
||||
class Monkey {
|
||||
Monkey({required this.name, required this.power});
|
||||
final String name;
|
||||
final int power;
|
||||
|
||||
class SquareFactory extends ShapeFactory {
|
||||
@override
|
||||
String toString() => '$name - Power: $power';
|
||||
}
|
||||
|
||||
class GreatestMonkeyStrategy implements ListStrategy<Monkey> {
|
||||
@override
|
||||
List<Monkey> compose(List<Monkey> items) {
|
||||
return [
|
||||
items.fold<Monkey>(
|
||||
Monkey(name: '', power: 0), (aMonkey, bMonkey) => aMonkey.power > bMonkey.power ? aMonkey : bMonkey)
|
||||
];
|
||||
Widget build(Size size, Color color) {
|
||||
return SizedBox.fromSize(
|
||||
size: size,
|
||||
child: Container(color: color),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class WeakMonkeysStrategy implements ListStrategy<Monkey> {
|
||||
class CircleFactory extends ShapeFactory {
|
||||
@override
|
||||
List<Monkey> compose(List<Monkey> items) {
|
||||
return items
|
||||
.where(
|
||||
(element) => element.power <= 200,
|
||||
)
|
||||
.toList();
|
||||
Widget build(Size size, Color color) {
|
||||
return SizedBox.fromSize(
|
||||
size: size,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
List<Monkey> monkeys = [
|
||||
Monkey(name: 'Mortimer', power: 25),
|
||||
Monkey(name: 'ApeMan', power: 354),
|
||||
Monkey(name: 'Spider', power: 65),
|
||||
Monkey(name: 'DK', power: 500),
|
||||
Monkey(name: 'Tim', power: 120),
|
||||
Monkey(name: 'Kong', power: 25400),
|
||||
];
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
const MyHomePage({super.key});
|
||||
|
||||
@@ -71,14 +54,16 @@ class MyHomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
ListStrategy listStrat = WeakMonkeysStrategy();
|
||||
final List<ShapeFactory> shapeFactories = [CircleFactory(), SquareFactory()];
|
||||
int factoryIndex = 0;
|
||||
ShapeFactory get factory => shapeFactories[factoryIndex];
|
||||
|
||||
void swapStrategies() {
|
||||
if (listStrat.runtimeType == WeakMonkeysStrategy) {
|
||||
setState(() => listStrat = GreatestMonkeyStrategy());
|
||||
void swapFactories() {
|
||||
if (factoryIndex == 0) {
|
||||
setState(() => factoryIndex = 1);
|
||||
return;
|
||||
}
|
||||
setState(() => listStrat = WeakMonkeysStrategy());
|
||||
setState(() => factoryIndex = 0);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -92,22 +77,18 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text('All monkeys:'),
|
||||
...monkeys
|
||||
.map(
|
||||
(e) => Text(e.toString()),
|
||||
)
|
||||
.toList(),
|
||||
const SizedBox(height: 50),
|
||||
Text(listStrat.runtimeType.toString()),
|
||||
...listStrat
|
||||
.compose(monkeys)
|
||||
.map(
|
||||
(e) => Text(e.toString()),
|
||||
)
|
||||
.toList(),
|
||||
const SizedBox(height: 100),
|
||||
ElevatedButton(onPressed: () => swapStrategies(), child: const Text('Change Strats')),
|
||||
const Text('Abstract Factories!!'),
|
||||
Text('Using ${shapeFactories[factoryIndex].runtimeType.toString()}'),
|
||||
...List.generate(
|
||||
3,
|
||||
(index) => factory.build(
|
||||
Size(20.0 * (index + 1), 30.0 * (index + 1)),
|
||||
[Colors.red, Colors.green, Colors.blue][index],
|
||||
)),
|
||||
ElevatedButton(
|
||||
onPressed: () => swapFactories(),
|
||||
child: const Text('Swap Factories'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user