From the course: Java Design Patterns: Structural

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implement the Composite pattern

Implement the Composite pattern - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Implement the Composite pattern

- [Instructor] Now I'm going to use the composite pattern to refactor this expenses payment app. The first thing I'm going to do is create an interface, which will sits above the Manager, Salesperson, and SalesTeam classes. So I'll right click on the Java package and go to New, Java Class. And I'll call this Payee, and choose the interface option. In this interface, I'll define the payExpenses method. So I'll do public void payExpenses. And this takes an int as an arguments, which I will call amount. And next I'll go to the Manager class. And in the class declaration, I'll add implements Payee. I already have a payExpenses method here, so I don't need to do anything else here. Next, I'll go to the Salesperson class. And again add implements Payee to the class declaration. And again, I have a payExpenses method, so no more needs to be done. Finally, I'll do the same for the SalesTeam class. So I'll add implements Payee.…

Contents