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:
Michael Olayemi 2023-07-26 05:25:09 +00:00 committed by GitHub
parent f471dd3cf9
commit ddce1d63db
7 changed files with 128 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.demeter;
public class Department {
private Manager manager = new Manager();
public Manager getManager() {
return manager;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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";
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.demeter;
public class Manager {
public void approveExpense(Expenses expenses) {
System.out.println("Expense approved" + expenses.total());
}
}