Skip to content

Instantly share code, notes, and snippets.

public class CEO extends ChainHandler implements Employee {
@Override
public void handleRequest(ChainedRequest chainedRequest) {
switch (chainedRequest.getRequestType()) {
case CONFERENCE:
System.out.println("CEO can approve anything");
break;
public class Subway {
//Member Variables
private String bread;
private boolean toast;
private boolean veg;
private boolean nonVeg;
private boolean onion;
private boolean tomato;
@mhtmalpani
mhtmalpani / SubwayMain.java
Created November 26, 2017 08:53
Builder pattern: Main file
public class Main {
public static void main(String[] args) {
Subway subway = Subway.builder("Wheat", "Grilled")
.cheese("Mozzarella")
.onion()
.build();
}
}
@mhtmalpani
mhtmalpani / Subway.java
Last active November 26, 2017 09:09
Builder Pattern: The actual builder class
public class Subway {
private String bread;
private String toast;
private String cheese;
private boolean onion;
private boolean tomato;
//Private constructor to avoid direct instantiation of the Subway class
private Subway(Builder builder) {
@mhtmalpani
mhtmalpani / HelperInterfaces.java
Created November 26, 2017 19:27
The helper interfaces for Builder pattern with Twist
public interface Bread {
Toast bread(String bread);
}
public interface Toast {
Type toast(boolean toast);
}
@mhtmalpani
mhtmalpani / BuilderWithTwist.java
Created November 26, 2017 19:34
The Builder pattern with a twist
public class Subway {
//Member Variables
private String bread;
private boolean toast;
private boolean veg;
private boolean nonVeg;
private boolean onion;
private boolean tomato;
private boolean olives;
@mhtmalpani
mhtmalpani / BuilderWithTwistMain.java
Created November 26, 2017 19:36
The Client code for Builder with Twist.
public class Main {
public static void main(String[] args) {
Subway subwayVegetarian = Subway.builder().bread("Wheat")
.toast(true)
.veg()
.olives().tomato().onion()
.prepare()
.cheese("Mozzarella")
@mhtmalpani
mhtmalpani / Animal.java
Created November 29, 2017 20:16
Animal interface for Factory
public interface Animal {
String makeNoise();
}
public class Cat implements Animal {
@Override
public String makeNoise() {
return "Meow";
}
}
public class Dog implements Animal {
@Override
public String makeNoise() {
return "Woof";
}
}