Created
July 15, 2014 17:01
-
-
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.
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
// 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()); | |
} | |
} | |
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
// 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()); | |
} | |
} |
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
// 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 |
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
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) |
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
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