Skip to content

Instantly share code, notes, and snippets.

@MattMoony
Created May 15, 2021 15:05
Show Gist options
  • Save MattMoony/db92a28cdbc9d6b10fa28f704b674f64 to your computer and use it in GitHub Desktop.
Save MattMoony/db92a28cdbc9d6b10fa28f704b674f64 to your computer and use it in GitHub Desktop.
syp-design-pattern-strategy
/*
* Ü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();
}
}
@MattMoony
Copy link
Author

A possible solution would look like the following:

// Strategy-Interface
interface FahrGeräusche {
	public void fahrGeräusche();
}

// Strategy
class LeiseGeräusche implements FahrGeräusche {
    // Algorithmus
	public void fahrGeräusche() {
		System.out.println("brrrrrrrrrrrrrrrrrrrrrrrrrrrmmm");
	}
}

// Strategy
class LauteGeräusche implements FahrGeräusche {
    // Algorithmus
	public void fahrGeräusche() {
		System.out.println("BRRRRRUUUUUUUUUUUUUUUUUUUUUMMMMM");
	}
}

// Context
abstract class Auto {
    private String name;
	private FahrGeräusche fahrGeräusche;
	
	public Auto(String name) {
        this.name = name;
    }
		
    public String getName() {
        return name;
    }
		
    public FahrGeräusche getFahrGeräusche() {
        return fahrGeräusche;
    }
	
	public void setFahrGeräusche(FahrGeräusche fahrGeräusche){
		this.fahrGeräusche = fahrGeräusche;
	}
	
	public void fahrGeräusche() {
		fahrGeräusche.fahrGeräusche();
	}
}

class PorscheCarrera extends Auto {
    public PorscheCarrera(String name) {
        super(name);
    }
}

class VWGolf extends Auto {
    private float secretEmissions = (float) Math.pow(2, 32);

    public VWGolf(String name) {
        super(name);
    }
}

public class AutoStrategyTest {
    public static void main(String[] args) {
        // Client
        VWGolf betsy = new VWGolf("VW Golf VII - 'Betsy'");
        
		betsy.setFahrGeräusche(new LeiseGeräusche());
        System.out.printf("Auto \"%s\" fährt nun los!\n", betsy.getName());
        betsy.fahrGeräusche(); // --> brrrrrrrrrrrrrrrrrrrrrrrrrrrmmm

		betsy.setFahrGeräusche(new LauteGeräusche());
		System.out.printf("Auto \"%s\" fährt nun los!\n", betsy.getName());
        betsy.fahrGeräusche(); // --> BRRRRRUUUUUUUUUUUUUUUUUUUUUMMMMM
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment