1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Design Patterns in Java Projects

Prototype Pattern

Contents

keyboard_tab
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
Overview
Difficulty
Intermediate
Duration
4h 56m
Students
26
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 another creational design pattern known as the prototype pattern. Let's begin. The prototype design pattern is used to specify the kinds of objects to create using a prototypical instance and create new objects by copying this prototype. The concept is to copy an existing object rather than creating a new instance from scratch; something that may include costly operations. The existing object acts as a prototype and contains the state of the object. The newly copied object may change some properties only if required. This approach saves costly resources and time, especially when the object creation is a heavy process. In Java, there are certain ways to copy an object in order to create a new one. One way to achieve this is using the Cloneable interface. Java provides the clone method which an object inherits from the object class. You need to implement the Cloneable interface and override this clone method according to your needs.

Basically, prototype design pattern is used in scenarios where applications need to create a number of instances of a class which has almost the same state or differs very little. In this design pattern, an instance of an actual object, i.e., prototype is created on starting; and therefore, whenever a new instance is required, this prototype is cloned to have another instance. The main advantage of this pattern is to have a minimal instance creation process, which is much more costly than the cloning process. Prototype pattern says that cloning of an existing object instead of creating a new one, and it can also be customized as per the requirement. If the cost of creating a new object is expensive and resource intensive, you can use this pattern. The copying of objects in Java is typically done by the clone method of Java Lang object. If you look at the UML diagram of the prototype pattern, you can see that we need this method. We are going to create an interface prototype that contains a method getClone of prototype type.

Then, we create a concrete class which implements prototype interface that does the cloning of the record object. Prototype demo class will use this concrete class. Of course, this pattern will provide us some advantages like: it lets you add or remove objects at runtime; it lets you specify new objects by varying their values; it reduces the need for subclassing; it lets you dynamically configure an application with classes. With a small example, I believe it will be clearer. Let's create a new project and name it Prototype. First, we need an interface that extends the Cloneable interface and contains a method clone. This interface is implemented by classes, which we want to create a prototype object. Now, we can add an abstract class. Implements Prototype. Defines properties. Control level and access. Now, create the constructor. Let's override method. Generate getters and setters. Done. And now we can add another class and name it User. Before constructor, we need properties.

Now, add the constructor. Generate getters and setters. If you want, we can override to String method also. Done. Now, we will add a provider. For this, we need a Map and a HashMap library. Define a static map instance. In this class, we will define our user types. The class also contains two properties. The control level is used to specify the level of control this object contains. The level depends on the type of user going to use it. For example, user, admin, manager, etc. Put user. Now, admin. Go on. Done. Now, we need an object called getAccessControlObject. The getAccessControlObject method fetches a stored prototype object according to the control level passed to it from the Map and returns a newly created cloned object to the client code. Now, the structure is ready. Let's use. Create an instance from AccessControl and value will be user. Now, I will add another two users. Done. Let's try. As you see, we can see users access state. That's it for now. I hope you have a better understanding of prototype. In the next video, we will begin to examine structural patterns starting with adapter design pattern. See you in the following video.

 

About the Author
Students
512
Courses
47
Learning Paths
3

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