Last active
March 10, 2024 17:15
-
-
Save fredgrott/3f7c2c0c30bdd8c0d32e246c52d59b55 to your computer and use it in GitHub Desktop.
dart oop behavioral, command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:command/light.dart'; | |
import 'package:command/light_switch.dart'; | |
void main() { | |
var myFavoriteLamp = Light(); | |
var iotLightSwitch = LightSwitch(myFavoriteLamp); | |
iotLightSwitch.perform("on"); | |
iotLightSwitch.perform("off"); | |
iotLightSwitch.perform("blink"); | |
iotLightSwitch.perform("on"); | |
print("\r\n*** Fancy IoT Switch Logs ***\r\n${iotLightSwitch.history}"); | |
/* | |
Light on! | |
Light off! | |
Uh...wait, wut? | |
Light on! | |
*** Fancy IoT Switch Logs *** | |
[2019-06-20 08:00:38.880050] Executed Turn on | |
[2019-06-20 08:00:38.883495] Executed Turn off | |
[2019-06-20 08:00:38.883702] Executed Turn on | |
*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:command/receiver.dart'; | |
abstract class Command { | |
late Receiver receiver; | |
late String name; | |
Command(this.receiver); | |
@override | |
String toString() => name; | |
void execute(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:command/command.dart'; | |
class Invoker { | |
List<String> history = []; | |
void execute(Command cmd) { | |
cmd.execute(); | |
history.add("[${DateTime.now()}] Executed $cmd"); | |
} | |
@override | |
String toString() => history.fold("", (events, event) => "$events$event\r\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:command/receiver.dart'; | |
class Light implements Receiver { | |
void turnOff() => print("Light off!"); | |
void turnOn() => print("Light on!"); | |
@override | |
Set<String> get actions => {"off", "on"}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
import 'package:command/invoker.dart'; | |
import 'package:command/light.dart'; | |
import 'package:command/turn_off_command.dart'; | |
import 'package:command/turn_on_command.dart'; | |
class LightSwitch { | |
final Invoker _switch = Invoker(); | |
Light light; | |
LightSwitch(this.light); | |
String get history => _switch.toString(); | |
void perform(String action) { | |
if (!light.actions.contains(action)) { | |
return print("Uh...wait, wut?"); | |
} | |
switch (action) { | |
case "on": | |
return _switch.execute(TurnOnCommand(light)); | |
case "off": | |
return _switch.execute(TurnOffCommand(light)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
abstract class Receiver { | |
Set<String> get actions; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
// ignore_for_file: overridden_fields | |
import 'package:command/command.dart'; | |
import 'package:command/light.dart'; | |
class TurnOnCommand extends Command { | |
@override | |
String name = "Turn on"; | |
TurnOnCommand(Light super.light); | |
@override | |
void execute() { | |
(receiver as Light).turnOn(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2024 Fredrick Allan Grott. All rights reserved. | |
// Use of this source code is governed by a BSD-style | |
// license that can be found in the LICENSE file. | |
// ignore_for_file: overridden_fields | |
import 'package:command/command.dart'; | |
import 'package:command/light.dart'; | |
class TurnOffCommand extends Command { | |
@override | |
String name = "Turn off"; | |
TurnOffCommand(Light super.light); | |
@override | |
void execute() { | |
(receiver as Light).turnOff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment