Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question 9 - Law of Demeter #1

Open
mesal opened this issue Mar 27, 2025 · 0 comments
Open

Question 9 - Law of Demeter #1

mesal opened this issue Mar 27, 2025 · 0 comments

Comments

@mesal
Copy link

mesal commented Mar 27, 2025

The "bad" example seems to apply the rule very well. The owner knows nothing about the Engine, just the Car. Moreover, the "good" example is also fine, though the method name suggests that the engine should be started in the Car.

A violation would be as follows:

public class Owner {
    private Car car;

    public Owner() {
        this.car = new Car();
    }

    public void startCar() {
        car.getEngine().start();
    }
}

public class Car {
    private Engine engine;

    public Car() {
        this.engine = new Engine();
    }
   public Engine getEngine(){
     return engine;
   }
    public void start() {
        engine.start();
    }
}

public class Engine {
    public void start() {
        System.out.println("Starting engine");
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant