4

So I have a Person object, and a GumballMachine object. The behavior I'm searching for is, in the intent to avoid unnecessary coupling, have the method insertCoin() called from within the Person object, which will then call the insertCoin() within the GumballMachine to receive a Gumball.

What is a proper design-pattern, or solution that avoids coupling, and keeps these two objects as oblivious to each other as possible while having a type of "Bridge" between them?

1
  • Maybe you could make an EventHandler and then assign an id to every object in your program, thus, the EventHandler will know on which object to call the method.
    – redxef
    Nov 21, 2015 at 20:52

3 Answers 3

5

To expand upon what Elliot was saying.

Your interface that returns true if the coin was accepted.

public interface Coinable {
    public boolean insertCoin();
}

Some person class that has some coins to insert into a Coinable interface.

public class Person {
    private int coins;

    public Person(int coins) {
        this.coins = coins;
    }

    public void insertCoin(Coinable cn) {
        if (this.coins <= 0) {
            System.out.println("You have no coins");
        } else {
            boolean coinAccepted = cn.insertCoin();
            if (coinAccepted) {
                coins--;
            }
        }
    }
}

And your GumballMachine that implements the Coinable interface.

class GumballMachine implements Coinable {
    private int gumBalls;

    public GumballMachine(int gumBalls) {
        this.gumBalls = gumBalls;
    }

    @Override
    public boolean insertCoin() {
        if (gumBalls <= 0) {
            System.out.println("There are no gumballs left");
            return false;
        } else {
            System.out.println("A gumball was dispensed!");
            gumBalls--;
            return true;
        }
    }
}

And an example of how all this works

public static void main(String[] args) {
    Person p = new Person(3); // 3 coins
    GumballMachine g = new GumballMachine(2); // 2 gumballs 

    p.insertCoin(g);
    p.insertCoin(g);
    p.insertCoin(g); // out of gumballs

    g = new GumballMachine(2); // refill the machine, still have 1 coin
    p.insertCoin(g);
    p.insertCoin(g);
}

Output

A gumball was dispensed!
A gumball was dispensed!
There are no gumballs left
A gumball was dispensed!
You have no coins
1
  • 1
    i see how you did that, that was the answer i was looking for! Although i found another solution i want to run by you. basically i just used a static method from another class to facilitate the communication between the two objects via (static method called from person: static method calls a method in GumballMachine: static method returns a Gumball back to Person). can you see an issue with that?
    – 3alto
    Nov 21, 2015 at 21:27
4

Create an interface, say Coinable, with a single insertCoin() method.

interface Coinable {
    insertCoin();
}

Then you can make n Object(s) that implement Coinable (and, some may delegate). And by programming to the interface, you can use various instances interchangeably.

3
  • i created one called InsertCoinInterface, which was implemented by Person, and GumballMachine, how would i then have GumballMachine implementation of the interface be called after the Person implementation was called?
    – 3alto
    Nov 21, 2015 at 20:59
  • The caller doesn't care. But the Person would need a GumballMachine to delegate to. There's also Chain of Responsibility. I'm not positive what you're asking. Nov 21, 2015 at 21:02
  • Let's back up for a moment. Why does the Person object need to know anything about the GumballMachine object? When a person inserts a coin into the gumball machine, why can't that action be executed outside of both classes (in the main body of the program)?
    – John Doe
    Nov 22, 2015 at 21:26
0

OK, here is a different take on the question.

An object oriented principle is to minimize coupling. I agree that it is often a good idea. But another important programming principle is to minimize complexity. The answer by cricket_007 does a good job of reducing coupling, but it does add more code. I don't know the specifics of your application, but there may be times when it is better to go with the simpler but more coupled code. This is especially true if the application is going to be fairly small.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.