Created
May 15, 2021 15:05
-
-
Save MattMoony/db92a28cdbc9d6b10fa28f704b674f64 to your computer and use it in GitHub Desktop.
syp-design-pattern-strategy
This file contains hidden or 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
| /* | |
| * Übungsbeispiel: Strategy | |
| * Autoren: Matthias Monschein, Nico Pessnegger | |
| * Datum: Mai 2021 | |
| * | |
| * Beschreibung: Übungsbeispiel zum | |
| * Design-Pattern Strategy. | |
| * | |
| * Aufgabenstellung: Es gilt die Klassen entsprechend | |
| * dem Design-Pattern Strategy umzustrukturieren. Das | |
| * Strategy-Interface "FahrGeräusche" und die Strategies | |
| * "LauteGeräusche" und "LeiseGeräusche" sollen implementiert | |
| * werden. | |
| */ | |
| abstract class Auto { | |
| private String name; | |
| public Auto(String name) { | |
| this.name = name; | |
| } | |
| public abstract void fahrGeräusche(); | |
| public String getName() { | |
| return name; | |
| } | |
| } | |
| class PorscheCarrera extends Auto { | |
| public PorscheCarrera(String name) { | |
| super(name); | |
| } | |
| @Override | |
| public void fahrGeräusche() { | |
| System.out.println("BRRRRRUUUUUUUUUUUUUUUUUUUUUMMMMM"); | |
| } | |
| } | |
| class VWGolf extends Auto { | |
| private float secretEmissions = (float) Math.pow(2, 32); | |
| public VWGolf(String name) { | |
| super(name); | |
| } | |
| @Override | |
| public void fahrGeräusche() { | |
| System.out.println("brrrrrrrrrrrrrrrrrrrrrrrrrrrmmm"); | |
| } | |
| } | |
| public class AutoStrategyTest { | |
| public static void main(String[] args) { | |
| VWGolf betsy = new VWGolf("VW Golf VII - 'Betsy'"); | |
| System.out.printf("Auto \"%s\" fährt nun los!\n", betsy.getName()); | |
| betsy.fahrGeräusche(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A possible solution would look like the following: