Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Created September 5, 2019 03:58
Show Gist options
  • Save ryanorsinger/d670c3b7a625933da6d0552048b89f53 to your computer and use it in GitHub Desktop.
Save ryanorsinger/d670c3b7a625933da6d0552048b89f53 to your computer and use it in GitHub Desktop.
"A Little Java, A Few Patterns" Starter
abstract class Seasoning {}
class Salt extends Seasoning {
public String toString() {
return "new " + getClass().getName() + "()";
}
}
class Pepper extends Seasoning {
public String toString() {
return "new " + getClass().getName() + "()";
}
}
class Thyme extends Seasoning {
public String toString() {
return "new " + getClass().getName() + "()";
}
}
abstract class Num {}
class Zero extends Num {
public String toString() {
return "new " + getClass().getName() + "()";
}
}
class OneMoreThan extends Num {
Num predecessor;
OneMoreThan(Num _p) {
predecessor = _p;
}
public String toString() {
return "new " + getClass().getName() + "(" + predecessor + ")";
}
}
/**
* To run this Main class, compile from the terminal with javac Main.java
* Once combiled, run on the JVM with java Main
*/
public class Main {
public static void main(String[] args) {
Seasoning seasoning1 = new Salt();
Seasoning seasoning2 = new Pepper();
System.out.println(seasoning2);
System.out.println(seasoning1);
Num a = new Zero();
System.out.println(a);
Num b = new OneMoreThan(a);
System.out.println(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment