The goal of this lab is to use the flyweight design pattern, a structural design pattern, to solve a real-world problem.
Design patterns are proven solutions to solve recurring design problems to design flexible and reusable object-oriented software; that is, objects that are easier to implement, change, test and reuse. Structural patterns deal with the composition of classes or objects while keeping them extensible, flexible and efficient.
The flyweight design pattern is one of the twenty-three well-known Gang of Four (GoF) design patterns. It is classified under the category of structural patterns as it makes the seemingly complex and resource intensive process of creating “lots of objects” more efficient. The flyweight pattern uses sharing to support storing large number of objects efficiently.
In this lab, we will work on the problem of creating a large number of objects that will be used to create an environment for a game.
These objects are expected to repeat all over the game environment to enhance the user experience while playing the game. We will see how the flyweight design pattern answers the following questions:
How can we reduce the number of stored objects and improve memory storage (RAM) overhead due to the sheer quantity of stored objects?
How can we separate the object’s state into intrinsic and extrinsic states?
Video
Objectives
In this lab you will
understand a real-world scenario and choose when to apply the appropriate design pattern.
design and implement the flyweight design pattern.
If you do not like to use an IDE, you may use any text editor (e.g., VS Code, jEdit, etc.) and the Javac compiler.
Getting Started
If your instructor is using GitHub classroom, you will need to accept the assignment using the link below, clone the template repository, and import it as a project into your IDE.
A developer is working on a game, where it creates a large number of background elements/objects (e.g., clouds, trees, etc.) to create a large set of objects for a game. The developer is planning to release her game on mobile devices but is worried that creating these high quality objects will decrease the performance of her game. Although the game seems relatively simple, creating hundreds of thousands of these objects may impact the overall performance of the game.
The developer started by creating a class called ImageElement, which holds a set of ImageIcon objects for each image in the environment.
Next, she created the main client class that will maintain the extrinsic state and stores references to flyweights. For demonstration purposes, the client app is a Java Swing (GUI) application that will randomly display images in a set of JLabel objects, which will be added to the main JFrame.
import java.util.Map;import java.util.HashMap;publicclassImageElementsFactory{private Map<String, ImageElement> flyweights =new HashMap<String, ImageElement>();public ImageElement getFlyweight(String n){//TODO: return a flyweight if it already exists, otherwise, put it in the map.
}publicintnumberOfFlyweights(){// return the size of the HashMap
}}
After completing and running the program, it should produce the following output:
Complete the implementation of the flyweight design pattern as shown in the code above?
Change the number of created images to hundreds of thousands to experiment with the effects of the flyweight pattern? How many flyweights have been created?
Explain how the flyweight design pattern reduces the number of objects stored in memory compared to storing all objects in memory?
If your instructor is using GitHub classroom, then you should click on your class submission link,
link your GitHub username to your name if you have not already done so, accept the assignment, clone the
repository into your local
development environment, and push the code to the remote repository on GitHub. Please make sure that your
written
answers are included in either a README (Markdown) file or a PDF file.
Lab dues dates are listed on GitHub classroom unless otherwise
noted.
If your instructor is using GitHub classroom, your submission will be
auto-graded
by running the included unit tests as well as manually graded for correctness, style, and quality.
How to submit your lab to GitHub Classroom
The video below demonstrates how to submit your work to GitHub classroom
Extra Task [Optional]
If you are done with this activity, you may enable a continuos integration tool such as CircleCI ↗ to automatically run your JUnit test upon code changes. You may also add more unit tests to increase code coverage. Please embed the badge that shows the status of your build and test (passing/failing) as well as the coverage percentage into your README file (e.g.,
and ). Please be sure to fork the repository or push to a remote repository under your own account, so you can enable the integration of CI tools in your own account.