12.07.2015 Views

Podsumowanie wzorców projektowych GoF

Podsumowanie wzorców projektowych GoF

Podsumowanie wzorców projektowych GoF

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}}System.out.println("First person returning the spoon");SingleSpoon.returnTheSpoon();spoonForFirstPerson = null;System.out.println("The spoon was returned");System.out.println("");System.out.println("Second person getting a spoon");spoonForSecondPerson = SingleSpoon.getTheSpoon();if (spoonForSecondPerson != null)System.out.println(spoonForSecondPerson);elseSystem.out.println("No spoon was available");Klasa wzorca:SingleSpoonpackage pk.dydakt.to.dp.c.gof.singleton;import pk.dydakt.to.dp.c.gof.soups.*;public class SingleSpoon{private Soup soupLastUsedWith;private static SingleSpoon theSpoon;private static boolean theSpoonIsAvailable = true;private SingleSpoon() {}public static SingleSpoon getTheSpoon() {if (theSpoonIsAvailable) {if (theSpoon == null) {theSpoon = new SingleSpoon();}theSpoonIsAvailable = false;return theSpoon;} else {return null;//spoon not available,// could throw error or return null (as shown)}}public String toString() {return "Behold the glorious single spoon!";}public static void returnTheSpoon() {theSpoon.cleanSpoon();theSpoonIsAvailable = true;}public Soup getSoupLastUsedWith() {return this.soupLastUsedWith;}public void setSoupLastUsedWith(Soup soup) {this.soupLastUsedWith = soup;8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!