image
Flyweight

Contents

Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
4h 56m
Students
79
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello, dear friends. In this video, we will examine the Flyweight Design Pattern. Let's begin. Sometimes the objects in an application might have great similarities and be of a similar kind. In case they 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 a basic cacheing 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 use of 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 pattern 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 the number of objects, the 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. 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. 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 another one as Jaguar. Override method. Let's add another one. Maybe, Citroen. Done. First, import HashMap. Let's create our CarFactory class. To find a HashMap for cars, name a string. Now, we need a get method for cars. This will take a parameter as carType. Now, we can check carType with a 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 have finished to create our structure. Let's use. First, we need a static CarFactory. Now, let's add 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, 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. Now, I believe that's enough for flyweight pattern. In the next video, we'll move on to another structural pattern called the Proxy Design Pattern. See you in the next video.

 

About the Author
Students
2690
Courses
64
Learning Paths
4

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.

Covered Topics