Law of Demeter in Java (#14465)
* Law of Demeter in Java * Law of Demeter in Java * Law of Demeter in Java
This commit is contained in:
parent
f471dd3cf9
commit
ddce1d63db
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class DemeterApplication {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Expenses expenses = new Expenses(100, 10);
|
||||
Employee employee = new Employee();
|
||||
employee.getDepartment()
|
||||
.getManager()
|
||||
.approveExpense(expenses);
|
||||
|
||||
Manager mgr = new Manager();
|
||||
Employee emp = new Employee(mgr);
|
||||
emp.submitExpense(expenses);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class Department {
|
||||
|
||||
private Manager manager = new Manager();
|
||||
|
||||
public Manager getManager() {
|
||||
return manager;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class Employee {
|
||||
private Department department = new Department();
|
||||
private Manager manager;
|
||||
|
||||
public Employee() {
|
||||
|
||||
}
|
||||
|
||||
Employee(Manager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
public Department getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void submitExpense(Expenses expenses) {
|
||||
manager.approveExpense(expenses);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class Expenses {
|
||||
|
||||
private double total;
|
||||
private double tax;
|
||||
|
||||
public Expenses(double total, double tax) {
|
||||
this.total = total;
|
||||
this.tax = tax;
|
||||
}
|
||||
|
||||
public double total() {
|
||||
return total + tax;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class Greetings {
|
||||
|
||||
HelloCountries helloCountries = new HelloCountries();
|
||||
|
||||
private static HelloCountries helloCountriesStatic = new HelloCountries();
|
||||
|
||||
public String generalGreeting() {
|
||||
return "Welcome" + world();
|
||||
}
|
||||
|
||||
public String world() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
public String getHelloBrazil() {
|
||||
HelloCountries helloCountries = new HelloCountries();
|
||||
return helloCountries.helloBrazil();
|
||||
}
|
||||
|
||||
public String getHelloIndia(HelloCountries helloCountries) {
|
||||
return helloCountries.helloIndia();
|
||||
}
|
||||
|
||||
public String getHelloJapan() {
|
||||
return helloCountries.helloJapan();
|
||||
}
|
||||
|
||||
public String getHellStaticWorld() {
|
||||
return helloCountriesStatic.helloStaticWorld();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class HelloCountries {
|
||||
|
||||
public String helloBrazil() {
|
||||
return "Hello Brazil";
|
||||
}
|
||||
|
||||
public String helloIndia() {
|
||||
return "Hello India";
|
||||
}
|
||||
|
||||
public String helloJapan() {
|
||||
return "Hello Japan";
|
||||
}
|
||||
|
||||
public String helloStaticWorld() {
|
||||
return "Hello Static World";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.demeter;
|
||||
|
||||
public class Manager {
|
||||
|
||||
public void approveExpense(Expenses expenses) {
|
||||
System.out.println("Expense approved" + expenses.total());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue