Prototype Design Pattern
Start course
3h 59m

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


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine another creational design pattern known as the prototype pattern. So, 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 application needs to create a number of instances of a class which is 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 is all about 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're going to create an interface prototype that contains the method get clone of prototype. Then we create a concrete class which implements the 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 sub-classing. It lets you dynamically configure an application with classes. With a small example, I think it will be way clearer, so let's get to it.

Let's create a new project and name it Prototype. First, we need an interface that extends the cloneable interface and contains the method clone. This interface is implemented by classes which want to create a prototype object. Now, we can add an abstract class. Implements Prototype, define properties, controlLevel and access. Now, create the constructor. Let's override the method. Generate getters and setters. Okay, done, now we can add another class and name it User.

Before constructor, we need properties. Now, add a constructor. Generate getters and setters. If you want, we can override the String method also. Okay, done, now we will add a provider. For this we need Map and 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 upon the type of user going to use it, for example, user, admin, manager, etc. So, let's just put USER. Now, ADMIN. Go on, and done. Now, we need an object called getAccessControl object. The getAccessControl method fetches the 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.

Okay, now the structure is ready, let's use it. Create an instance from AccessControl and the value will be USER. Now, I will add another two users. Okay, done, let's try. As you can see, we can see the user's access state. So, 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 the adapter design pattern. So, I'll see you in the following video. Goodbye friends.


About the Author
Learning Paths

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.