Skip to content

Instantly share code, notes, and snippets.

@thonglinhma
Last active April 20, 2018 09:02
Show Gist options
  • Save thonglinhma/6d326a92799177aa26170aeb99b4fbeb to your computer and use it in GitHub Desktop.
Save thonglinhma/6d326a92799177aa26170aeb99b4fbeb to your computer and use it in GitHub Desktop.
abstract class PrintStrategy {
String print(String string);
}
class Printer {
PrintStrategy _strategy;
Printer({PrintStrategy strategy}) : _strategy = strategy;
String print(String string) {
return _strategy.print(string);
}
}
class UpperCaseStrategy extends PrintStrategy {
@override
String print(String string) {
return string.toUpperCase();
}
}
class LowerCaseStrategy extends PrintStrategy {
@override
String print(String string) {
return string.toLowerCase();
}
}
main(List<String> arguments) {
var lower = Printer(strategy: new LowerCaseStrategy());
lower.print("O tempora, o mores!");
var upper = Printer(strategy: new UpperCaseStrategy());
upper.print("O tempora, o mores!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment