Commit bf6402c9 authored by ElaFHNW's avatar ElaFHNW
Browse files

factory design pattern

parent f41dcda1
package designPattern.Factory;
public class AlmondCroissant {
}
package designPattern.Factory;
//Baklava
public class Baklava implements Pastry { Baklava(){} @Override
public String getFirstTimeImpression() {
return "Baklava is good but a bit too sweet for me.";
}
}
package designPattern.Factory;
public class BuyPastry {
public static void main(String[] args) {
Pastry baklava = PastryFactory.CreatePastry(PastryType.Baklava);
Pastry cinnamonRoll = PastryFactory.CreatePastry(PastryType.CinnamonRoll);
Pastry puff = PastryFactory.CreatePastry(PastryType.Puff);
Pastry flaky = PastryFactory.CreatePastry(PastryType.Flaky);
System.out.println(baklava.getFirstTimeImpression());
System.out.println(cinnamonRoll.getFirstTimeImpression());
System.out.println(puff.getFirstTimeImpression());
System.out.println(flaky.getFirstTimeImpression());
}
}
package designPattern.Factory;
//CinnamonRoll
public class CinnamonRoll implements Pastry {
CinnamonRoll(){}
@Override
public String getFirstTimeImpression() {
return "CinnamonRoll is one of my favorite pastries.";
}
}
package designPattern.Factory;
//Flaky
public class Flaky implements Pastry {
Flaky(){} @Override
public String getFirstTimeImpression() {
return "Flaky is really good specially while still warm.";
}
}
package designPattern.Factory;
public interface Pastry {
String getFirstTimeImpression();
}
package designPattern.Factory;
public class PastryFactory {
public static Pastry CreatePastry(PastryType type){
switch (type){
case Puff:
return new Puff();
case Baklava:
return new Baklava();
case Flaky:
return new Flaky();
case CinnamonRoll:
return new CinnamonRoll();
default:
System.out.println("we only have Puff left");
return new Puff();
//could throw exception for unsupported PastryType here...
}
}
}
package designPattern.Factory;
public enum PastryType {
Puff,
Baklava,
Flaky,
CinnamonRoll
}
package designPattern.Factory;
//Puff
public class Puff implements Pastry {
Puff(){}
@Override
public String getFirstTimeImpression() {
return "Puff is so so.";
}
}
package designPattern.Factory;
public class SconeWithJam {
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment