Design Principles - II
- Law of Demeter (LoD)
- Boy Scout Rule
- Code for the Maintainer
- Principle of Least Astonishment (POLA)
- Tight Coupling vs. Loose Coupling
- Minimize Coupling & Maximize Cohesion
1. Law of Demeter (LoD)
Section titled “1. Law of Demeter (LoD)”- Description: An object should only talk to its immediate neighbours. Avoid long chains of method calls.
- Example: A
Carobject should not directly access theEngine’sPistonobject. Instead,Carshould ask theEngineto perform operations on thePiston. - Instead of:car.getEngine().getPiston().move();- Do:car.startEngine();(wherestartEngineencapsulates the necessary interactions within theEngineclass).
Guideline:
Section titled “Guideline:”- A method should call only:
- Its own methods
- Methods on objects it creates
- Methods on objects passed to it
- Methods on its member variables

Example:
Section titled “Example:”Violating LoD
Section titled “Violating LoD”class Engine { public void start() { System.out.println("Engine started"); }}
class Car { private Engine engine;
public Car() { this.engine = new Engine(); }
public Engine getEngine() { return engine; }}
class Driver { public void startCar(Car car) { car.getEngine().start(); // Violates LoD }}Following LoD
Section titled “Following LoD”class Car { private Engine engine;
public Car() { this.engine = new Engine(); }
public void start() { engine.start(); // Encapsulation }}
class Driver { public void startCar(Car car) { car.start(); // Follows LoD }}2. The Boy Scout Rule
Section titled “2. The Boy Scout Rule”Definition:
Section titled “Definition:”“Always leave the code better than you found it.”
Application:
Section titled “Application:”- Refactor code when necessary.
- Improve readability and maintainability.
- Remove duplicate or unused code.

Example:
Section titled “Example:”Before applying the Boy Scout Rule:
class User { public String firstName; public String lastName;}After refactoring:
class User { private String firstName; private String lastName;
public String getFullName() { return firstName + " " + lastName; }}3. Code for the Maintainer
Section titled “3. Code for the Maintainer”Definition:
Section titled “Definition:”Write code that is readable, self-explanatory, and well-documented for future maintainers.
Best Practices:
Section titled “Best Practices:”- Use meaningful variable/method names.
- Write clear comments.
- Avoid complex logic when possible.
- Follow coding conventions.
Example:
Section titled “Example:”Poorly written code:
class C { int x; int y; void p() { x = 10; y = 20; System.out.println(x + y); }}Refactored, readable code:
class Calculator { private int firstNumber; private int secondNumber;
public void setNumbers(int a, int b) { this.firstNumber = a; this.secondNumber = b; }
public void printSum() { System.out.println(firstNumber + secondNumber); }}4. Principle of Least Astonishment (POLA)
Section titled “4. Principle of Least Astonishment (POLA)”Definition:
Section titled “Definition:”The system should behave in a way that minimizes surprises for users and developers.

Example:
Section titled “Example:”Violating POLA
Section titled “Violating POLA”class LightSwitch { public void toggle() { System.out.println("Fan turned ON"); // Unexpected behavior }}Following POLA
Section titled “Following POLA”class LightSwitch { private boolean isOn = false;
public void toggle() { isOn = !isOn; System.out.println(isOn ? "Light turned ON" : "Light turned OFF"); }}5. Tight Coupling vs. Loose Coupling
Section titled “5. Tight Coupling vs. Loose Coupling”Definition:
Section titled “Definition:”- Tightly Coupled: Classes depend heavily on each other.
- Loosely Coupled: Classes have minimal dependencies and interact via interfaces.
Example:
Section titled “Example:”Tightly Coupled Code (Bad)
Section titled “Tightly Coupled Code (Bad)”class EmailService { public void sendEmail(String message) { System.out.println("Email sent: " + message); }}
class Notification { private EmailService emailService = new EmailService();
public void notifyUser() { emailService.sendEmail("Hello User"); }}Loosely Coupled Code (Good)
Section titled “Loosely Coupled Code (Good)”interface MessageService { void sendMessage(String message);}
class EmailService implements MessageService { public void sendMessage(String message) { System.out.println("Email sent: " + message); }}
class Notification { private MessageService messageService;
public Notification(MessageService messageService) { this.messageService = messageService; }
public void notifyUser() { messageService.sendMessage("Hello User"); }}6. Minimize Coupling & Maximize Cohesion
Section titled “6. Minimize Coupling & Maximize Cohesion”Definition:
Section titled “Definition:”- Minimizing Coupling: Reduce dependencies between modules.
- Maximizing Cohesion: Keep related functionalities together.
Example:
Section titled “Example:”Low Cohesion (Bad Design)
Section titled “Low Cohesion (Bad Design)”class Employee { String name; int age;
public void calculateSalary() { // Salary calculation logic (Unrelated responsibility) }}High Cohesion (Good Design)
Section titled “High Cohesion (Good Design)”class Employee { String name; int age;}
class SalaryCalculator { public double calculateSalary(Employee emp) { // Properly encapsulated salary logic return 50000.0; }}Summary:
Section titled “Summary:”| Principle | Key Idea |
|---|---|
| Law of Demeter | Reduce unnecessary dependencies |
| Boy Scout Rule | Leave the code cleaner than you found it |
| Code for the Maintainer | Write readable and maintainable code |
| Principle of Least Astonishment | Avoid unexpected behavior in code |
| Tight Coupling vs. Loose Coupling | Prefer loose coupling using interfaces |
| Minimize Coupling & Maximize Cohesion | Reduce dependencies, keep related logic together |