From the course: Java Design Patterns: Structural
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Example of when to use the Composite pattern - Java Tutorial
From the course: Java Design Patterns: Structural
Example of when to use the Composite pattern
- [Instructor] This is a real world example of an app that would benefit from using the composite pattern. It's an app that's used for paying expenses to employees. So first of all, the app has a class called Manager, which has a field called name, and a method called payExpenses. In a real app, there would be some code in the payExpenses methods to actually make a payments. But to keep this example simple, it just prints out the name of the manager, and the amounts they have been paid. Next, there's a class called Salesperson. This is quite similar to the Manager class. It has a field called name, and print expenses method. That's again prints out the name and the amounts they've been paid. The slight difference is that a Salesperson also has a manager field. There's also a class called SalesTeam. This class has two fields, an ArrayList of managers, and an ArrayList of salespeople. There are two methods called…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.