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.

Challenge: The Flyweight pattern

Challenge: The Flyweight pattern - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Challenge: The Flyweight pattern

(upbeat music) - [Instructor] Let's have a look at our next challenge. In this exercise, you'll be refactoring an app that simulates a wildlife population of Lions and Tigers. This is very similar to the traffic simulation app in the previous video, but simulating animals instead of vehicles. So this app has an interface called animal, which has methods called get animal type, get location and set location. There are two implementations of animal called Lion and Tiger, which both implements these two methods. For example this is the Lion class. Finally, there is the population simulator class. This has a method called createRandomAnimal, which creates a Lion if the random number is zero and the Tiger if the random number is one. And it assigns the animal a random location, then it adds it to a list of animals. In the main method there are two runnables, one cause they createAnimal method and the other cause the…

Contents