Last active
January 7, 2021 15:11
-
-
Save irfansofyana/f1834e5d17b1df7c2621c13e122d93fe to your computer and use it in GitHub Desktop.
Example of non-executable code for creational design pattern
This file contains 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
// Formally, the abstract factory pattern is defined as defining an interface to create families of related or dependent objects | |
// without specifying their concrete classes. | |
public interface IEngine { | |
void start(); | |
} | |
public class F16Engine implements IEngine { | |
@Override | |
public void start() { | |
System.out.println("F16 engine on"); | |
} | |
} | |
public class F16Factory { | |
public IEngine createEngine() { | |
return new F16Engine(); | |
} | |
public IWings createWings() { | |
return new F16Wings(); | |
} | |
public ICockpit createCockpit() { | |
return new F16Cockpit(); | |
} | |
} | |
public interface IAircraftFactory { | |
IEngine createEngine(); | |
IWings createWings(); | |
ICockpit createCockpit(); | |
} | |
public class F16Factory implements IAircraftFactory { | |
@Override | |
public IEngine createEngine() { | |
return new F16Engine(); | |
} | |
@Override | |
public IWings createWings() { | |
return new F16Wings(); | |
} | |
@Override | |
public ICockpit createCockpit() { | |
return new F16Cockpit(); | |
} | |
} | |
public class Boeing747Factory implements IAircraftFactory { | |
@Override | |
public IEngine createEngine() { | |
return new Boeing747Engine(); | |
} | |
@Override | |
public IWings createWings() { | |
return new Boeing747Wings(); | |
} | |
@Override | |
public ICockpit createCockpit() { | |
return new Boeing747Cockpit(); | |
} | |
} | |
public class Aircraft { | |
IEngine engine; | |
ICockpit cockpit; | |
IWings wings; | |
IAircraftFactory factory; | |
public Aircraft(IAircraftFactory factory) { | |
this.factory = factory; | |
} | |
protected Aircraft makeAircraft() { | |
engine = factory.createEngine(); | |
cockpit = factory.createCockpit(); | |
wings = factory.createWings(); | |
return this; | |
} | |
private void taxi() { | |
System.out.println("Taxing on runway"); | |
} | |
public void fly() { | |
Aircraft aircraft = makeAircraft(); | |
aircraft.taxi(); | |
System.out.println("Flying"); | |
} | |
} | |
public class Client { | |
public void main() { | |
// Instantiate a concrete factory for F-16 | |
F16Factory f16Factory = new F16Factory(); | |
// Instantiate a concrete factory for Boeing-747 | |
Boeing747Factory boeing747Factory = new Boeing747Factory(); | |
// Lets create a list of all our airplanes | |
Collection<Aircraft> myPlanes = new ArrayList<>(); | |
// Create a new F-16 by passing in the f16 factory | |
myPlanes.add(new Aircraft(f16Factory)); | |
// Create a new Boeing-747 by passing in the boeing factory | |
myPlanes.add(new Aircraft(boeing747Factory)); | |
// Fly all your planes | |
for (Aircraft aircraft : myPlanes) { | |
aircraft.fly(); | |
} | |
} | |
} |
This file contains 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
// Builder pattern encapsulates or hides the process of building a complex object | |
// and separates the representation of the object and its construction. | |
// The separation allows us to construct different representations using the same construction process | |
public abstract class AircraftBuilder { | |
public void buildEngine() { | |
} | |
public void buildWings() { | |
} | |
public void buildCockpit() { | |
} | |
public void buildBathrooms() { | |
} | |
abstract public IAircraft getResult(); | |
} | |
public class Boeing747Builder extends AircraftBuilder { | |
Boeing747 boeing747; | |
@Override | |
public void buildCockpit() { | |
} | |
@Override | |
public void buildEngine() { | |
} | |
@Override | |
public void buildBathrooms() { | |
} | |
@Override | |
public void buildWings() { | |
} | |
public IAircraft getResult() { | |
return boeing747; | |
} | |
} | |
public class F16Builder extends AircraftBuilder { | |
F16 f16; | |
@Override | |
public void buildEngine() { | |
// get F-16 an engine | |
// f16.engine = new F16Engine(); | |
} | |
@Override | |
public void buildWings() { | |
// get F-16 wings | |
// f16.wings = new F16Wings(); | |
} | |
@Override | |
public void buildCockpit() { | |
f16 = new F16(); | |
// get F-16 a cockpit | |
// f16.cockpit = new F16Cockpit(); | |
} | |
public IAircraft getResult() { | |
return f16; | |
} | |
} | |
public class Director { | |
AircraftBuilder aircraftBuilder; | |
public Director(AircraftBuilder aircraftBuilder) { | |
this.aircraftBuilder = aircraftBuilder; | |
} | |
public void construct(boolean isPassenger) { | |
aircraftBuilder.buildCockpit(); | |
aircraftBuilder.buildEngine(); | |
aircraftBuilder.buildWings(); | |
if (isPassenger) | |
aircraftBuilder.buildBathrooms(); | |
} | |
} | |
public class Client { | |
public void main() { | |
F16Builder f16Builder = new F16Builder(); | |
Director director = new Director(f16Builder); | |
director.construct(false); | |
IAircraft f16 = f16Builder.getResult(); | |
} | |
} |
This file contains 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
// Formally, the factory method is defined as providing an interface for object creation | |
// but delegating the actual instantiation of objects to subclasses. | |
public class F16 { | |
IEngine engine; | |
ICockpit cockpit; | |
protected F16 makeF16() { | |
engine = new F16Engine(); | |
cockpit = new F16Cockpit(); | |
return this; | |
} | |
public void taxi() { | |
System.out.println("F16 is taxing on the runway !"); | |
} | |
public void fly() { | |
// Note here carefully, the superclass F16 doesn't know | |
// what type of F-16 variant it was returned. | |
F16 f16 = makeF16(); | |
f16.taxi(); | |
System.out.println("F16 is in the air !"); | |
} | |
} | |
public class F16A extends F16 { | |
@Override | |
public F16 makeF16() { | |
super.makeF16(); | |
engine = new F16AEngine(); | |
return this; | |
} | |
} | |
public class F16B extends F16 { | |
@Override | |
public F16 makeF16() { | |
super.makeF16(); | |
engine = new F16BEngine(); | |
return this; | |
} | |
} | |
public class Client { | |
public void main() { | |
Collection<F16> myAirForce = new ArrayList<F16>(); | |
F16 f16A = new F16A(); | |
F16 f16B = new F16B(); | |
myAirForce.add(f16A); | |
myAirForce.add(f16B); | |
for (F16 f16 : myAirForce) { | |
f16.fly(); | |
} | |
} | |
} |
This file contains 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
//Prototype pattern involves creating new objects by copying existing objects. The object whose copies are made is called the prototype. | |
public interface IAircraftPrototype { | |
void fly(); | |
IAircraftPrototype clone(); | |
void setEngine(F16Engine f16Engine); | |
} | |
public class F16 implements IAircraftPrototype { | |
// default engine | |
F16Engine f16Engine = new F16Engine(); | |
@Override | |
public void fly() { | |
System.out.println("F-16 flying..."); | |
} | |
@Override | |
public IAircraftPrototype clone() { | |
// Deep clone self and return the product | |
return new F16(); | |
} | |
public void setEngine(F16Engine f16Engine) { | |
this.f16Engine = f16Engine; | |
} | |
} | |
public class Client { | |
public void main() { | |
IAircraftPrototype prototype = new F16(); | |
// Create F16-A | |
IAircraftPrototype f16A = prototype.clone(); | |
f16A.setEngine(new F16AEngine()); | |
// Create F16-B | |
IAircraftPrototype f16B = prototype.clone(); | |
f16B.setEngine(new F16BEngine()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment