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.
Solution: The Composite pattern - Java Tutorial
From the course: Java Design Patterns: Structural
Solution: The Composite pattern
(upbeat music) - [Instructor] Let's have a look at my solution for the composite pattern challenge. As usual, don't worry if your solution is a bit different to mine. The first thing I've done is I've created an interface called Books, which defines two methods called Checkouts and Return Book. My fiction book, nonfiction book, and book collection classes all implement this interface. Next in the book collection class, I've only got a single array list for books rather than one each for fiction and nonfiction books. And I've simplified the methods instead of having an add nonfiction book and an add fiction book method, I've just got one called add book. I've also simplified the checkouts and return book methods so that they only iterate over that one list instead of two. Finally, in the library class, I've got a single method for checking out books instead of three and a single method for returning books. Using the…
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.