Client-Supplier Relationship (Java)

Context: FIT2099_MOC · one way to use a class — become its client · a class holding other objects as fields · the first taste of coupling Parent Framework: OOP Building Blocks (Class, Object, Field, Method)

Quick Revision

  • 🎯 Objective: build a class from other classes ➔ hold them as fields (has-a) and call their methods.
  • 📦 Core Components: client (uses) ↔ supplier (provides) | composition (this.x = new X()) | calling public methods.
  • ⚡ Key Constraint: the client needs to know only how to call a supplier’s method — not how it’s implemented (information hiding); it is coupled to the interface, not the internals.

📝 How It Works

1. Client and Supplier

  • Client ➔ a class that uses another; it holds the supplier as an attribute and calls its methods.
  • Supplier ➔ the class being used; it provides the behaviour.
  • Composition ➔ the client typically creates its suppliers in its constructor (this.coffee = new Coffee();).

2. Why It Matters

  • Information hidingMealMachine calls coffee.serve() without knowing how serve() works — only its signature.
  • Coupling ➔ the client depends on the supplier’s public interface; keep that interface small/stable to keep coupling loose.

⚙️ Core Implementation

🔹 MealMachine (client of Coffee & Bread)

When It Flips: measure this design on coupling (how many suppliers, how much of each interface it uses), cohesion (does MealMachine do one job?), extensibility (could a new meal item slot in without editing MealMachine much?). Loose coupling to small interfaces is the goal.

🧠 Active Recall