Skip to content

Instantly share code, notes, and snippets.

@anildigital
Created July 15, 2014 17:01
Show Gist options
  • Save anildigital/1f3fbb90ac630c802bad to your computer and use it in GitHub Desktop.
Save anildigital/1f3fbb90ac630c802bad to your computer and use it in GitHub Desktop.
Shows how applying SOLID Design principles like Single Responsibility Principle (SRP) leads to many small classes. Furthermore, if you rigorously apply the Interface Segregation Principle (ISP), you'll eventually arrive at the ultimate Role Interface: an interface with a single method.
// Simple Java program
public class Person {
private int age;
private boolean isFemale;
Person(int age, boolean isFemale) {
this.age = age;
this.isFemale = isFemale;
}
public boolean isWise() {
if (isFemale) {
return age > 18;
} else {
return age > 21;
}
}
public boolean canDrink() {
return age > 21;
}
public boolean canMarry() {
return isWise() && canDrink();
}
public static void main(String args[]) {
Person p = new Person(25, false);
System.out.println(p.canMarry());
}
}
// Interface segregation using Role based interfaces. Also follows SRP.
// Role interface Drinkable with single method
public interface Drinkable {
public boolean canDrink();
}
// Role interface Wiseable with single method
public interface Wiseable {
public boolean isWise();
}
// Using role based interfaces in Java
public class Person implements Drinkable, Wiseable {
private int age;
private boolean isFemale;
Person(int age, boolean isFemale) {
this.age = age;
this.isFemale = isFemale;
}
public boolean isWise() {
if (isFemale) {
return age > 18;
} else {
return age > 21;
}
}
public boolean canDrink() {
return age > 21;
}
public boolean canMarry() {
return isWise() && canDrink();
}
public static void main(String args[]) {
Person p = new Person(28, false);
System.out.println(p.canMarry());
}
}
// Scala way
class Person(age: Int, isFemale: Boolean) {
def isWise: Boolean = if (isFemale) age > 18 else age > 21
def canDrink: Boolean = age > 50
def canMarry() =
isWise && canDrink
}
val p = new Person(28, true)
p.canMarry
class Person(age: Int, isFemale: Boolean) {
def canMarry(isWise: (Boolean, Int) => Boolean, canDrink: Int => Boolean) =
isWise(isFemale, age) && canDrink(age)
}
// function
def isWise(isFemale: Boolean, age: Int): Boolean = {
if (isFemale) age > 18 else age > 21
}
// function
def canDrink(age: Int): Boolean = age > 21
val p = new Person(28, true)
p.canMarry(isWise, canDrink)
class Person(age: Int, isFemale: Boolean) {
def canMarry(isWise: (Boolean, Int) => Boolean, canDrink: Int => Boolean) =
isWise(isFemale, age) && canDrink(age)
}
// function as vals in scala
val isWise: (Boolean, Int) => Boolean = {
(isFemale: Boolean, age: Int) => if (isFemale) age > 18 else age > 21
}
// function as vals in scala.
val canDrink: (Int) => Boolean = { age: Int => age > 21}
val p = new Person(28, true)
// Compose functions to get result
p.canMarry(isWise, canDrink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment