monke
This commit is contained in:
parent
73bf1500f9
commit
e8ae9d8cda
|
@ -20,6 +20,49 @@ class MyApp extends StatelessWidget {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abstract class ListStrategy<T> {
|
||||||
|
List<T> compose(List<T> items);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Monkey {
|
||||||
|
Monkey({required this.name, required this.power});
|
||||||
|
final String name;
|
||||||
|
final int power;
|
||||||
|
|
||||||
|
@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)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class WeakMonkeysStrategy implements ListStrategy<Monkey> {
|
||||||
|
@override
|
||||||
|
List<Monkey> compose(List<Monkey> items) {
|
||||||
|
return items
|
||||||
|
.where(
|
||||||
|
(element) => element.power <= 200,
|
||||||
|
)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
class MyHomePage extends StatefulWidget {
|
||||||
const MyHomePage({super.key});
|
const MyHomePage({super.key});
|
||||||
|
|
||||||
|
@ -28,6 +71,16 @@ class MyHomePage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
|
ListStrategy listStrat = WeakMonkeysStrategy();
|
||||||
|
|
||||||
|
void swapStrategies() {
|
||||||
|
if (listStrat.runtimeType == WeakMonkeysStrategy) {
|
||||||
|
setState(() => listStrat = GreatestMonkeyStrategy());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setState(() => listStrat = WeakMonkeysStrategy());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
|
@ -39,13 +92,22 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
const Text(
|
const Text('All monkeys:'),
|
||||||
'You have pushed the button this many times:',
|
...monkeys
|
||||||
),
|
.map(
|
||||||
Text(
|
(e) => Text(e.toString()),
|
||||||
'some text',
|
)
|
||||||
style: Theme.of(context).textTheme.headlineMedium,
|
.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')),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user