image
FlyWeight Design Pattern
Start course
Difficulty
Beginner
Duration
3h 59m
Students
274
Ratings
3.4/5
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.

Learning Objectives

  • Learn about various design patterns

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello, dear friends. In this video, we will examine the Flyweight Design Pattern. So, let's begin. Sometimes the objects in an application might have great similarities and be of a similar kind. In case there are also heavy objects to create, they should be controlled by the application developer. Otherwise, they might consume much of the memory and eventually slow down the whole application. The flyweight pattern is designed to control such kind of object creation and provides you with the basic caching mechanism. It allows you to create one object per type and if you ask for an object with the same property, it will return you the same object instead of creating a new one. 

According to GoF, Flyweight design pattern enables the sharing of objects to support large numbers of fine-grained objects efficiently. As you can see in this UML diagram, the structure of flyweight is very simple. Flyweight design pattern is a structural design pattern. Flyweight design pattern is used when we need to create a lot of objects of a class. Since every object consumes memory space, that can be crucial for low memory devices such as mobile devices or embedded systems. Flyweight design patterns can be applied to reduce the load on memory by sharing objects.

Before we apply the flyweight design pattern, we need to consider some important factors. An application must use a considerable number of objects. Because of the number of objects, storage costs are high. The application does not depend on object identity. There are two important advantages of flyweight. The first is that flyweight reduces the number of objects to deal with. The second is that flyweight reduces the amount of memory and storage devices required if the objects are persisted. So, I think that's enough. Let's create a project using the flyweight pattern. Let's take, for example, a car assembly factory. First, we need an interface. Let's name it car. Okay, we need a method. Let's name it assemble. Let's define our first car maybe Mercedes, implements car. We have to override assemble method. Let's call another one as Jaguar, override method. Let's add another one, maybe Citroen.

Done, let's create our CarFactory class. First import HashMap. Define a HashMap for cars name as String. Now, we need a get method for cars. This will take a parameter as carType. Now we can check carType with the condition, but first make empty control. Mercedes, save the memory. If you want, you can copy this code and change for Jaguar. Last one is Citroen. Return car. Now we've finished to create our structure, let's use it. First, we need a static CarFactory. Now, let's add a loop for Mercedes. I want to give location of cars. Let's add another loop for Jaguar, and of course, I want to add for Citroen.

Now, if we run this project, we can see nine cars with positions. Actually to tell you the truth, there are no advantages of using the flyweight pattern in this project, but for large projects, these loops may work for 100,000. And in that case, this pattern will give us a significant advantage. As you can see, we can see our cars. So, I believe we've got it for flyweight pattern. In the next video, we'll move on to another structural pattern called the proxy design pattern. So, I'll see you in the next video my friends.

 

About the Author
Students
4005
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.