tdudzik

SOLID - Interface Segregation Principle

Apr 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. /* Interface Segregation Principle */
  2.  
  3. // Before
  4. class Person {
  5.  
  6.     private final String firstName;
  7.     private final String lastName;
  8.     private final float gpa;
  9.     private final String accountNumber;
  10.  
  11.     public Person(String firstName, String lastName, float gpa, String accountNumber) {
  12.         this.firstName = firstName;
  13.         this.lastName = lastName;
  14.         this.gpa = gpa;
  15.         this.accountNumber = accountNumber;
  16.     }
  17.  
  18.     public String getFirstName() {
  19.         return firstName;
  20.     }
  21.  
  22.     public String getLastName() {
  23.         return lastName;
  24.     }
  25.  
  26.     public float getGpa() {
  27.         return gpa;
  28.     }
  29.  
  30.     public String getAccountNumber() {
  31.         return accountNumber;
  32.     }
  33.  
  34. }
  35.  
  36. class Accountant {
  37.    
  38.     public void paySalary(Person person) {
  39.         System.out.println("Account number: " + person.getAccountNumber());
  40.     }
  41.  
  42. }
  43.  
  44. // After
  45. interface Employee {
  46.  
  47.     public String getFirstName();
  48.  
  49.     public String getLastName();
  50.  
  51.     public String getAccountNumber();
  52.  
  53. }
  54.  
  55. interface Student {
  56.  
  57.     public String getFirstName();
  58.  
  59.     public String getLastName();
  60.  
  61.     public float getGpa();
  62.  
  63. }
  64.  
  65. class Person implements Student, Employee {
  66.  
  67.     private final String firstName;
  68.     private final String lastName;
  69.     private final float gpa;
  70.     private final String accountNumber;
  71.  
  72.     public Person(String firstName, String lastName, float gpa, String accountNumber) {
  73.         this.firstName = firstName;
  74.         this.lastName = lastName;
  75.         this.gpa = gpa;
  76.         this.accountNumber = accountNumber;
  77.     }
  78.  
  79.     public String getFirstName() {
  80.         return firstName;
  81.     }
  82.  
  83.     public String getLastName() {
  84.         return lastName;
  85.     }
  86.  
  87.     public float getGpa() {
  88.         return gpa;
  89.     }
  90.  
  91.     public String getAccountNumber() {
  92.         return accountNumber;
  93.     }
  94.  
  95. }
  96.  
  97. class Accountant {
  98.    
  99.     public void paySalary(Employee employee) {
  100.         System.out.println("Account number: " + employee.getAccountNumber());
  101.     }
  102.  
  103. }
Add Comment
Please, Sign In to add comment