DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Intro to Design Patterns: Prototype Pattern

Intro to Design Patterns: Prototype Pattern

Andre Mare user avatar by
Andre Mare
·
May. 07, 08 · Interview
Like (0)
Save
Tweet
Share
37.89K Views

Join the DZone community and get the full member experience.

Join For Free

Today we're going to look at the Prototype design pattern.  Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. - Gof

Type

Object Creational

Solution

The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it is dealing with as long as the concrete prototype extends or implements the prototype interface/class. The concrete prototype object is responsible for cloning itself and returning the cloned object.

The pattern enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at runtime as long as they conform to the abstract prototype.

Structure

Design Pattern, Prototype Pattern, GOF

Java Sample Code

Example 1:

Download Example 1

The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses called Sheep and Chicken. The AnimalCreator class contains references to the two concrete prototypes. During the initialization of the AnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.

Animal.java
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.

Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.

Code:

public Animal clone() {	 

Animal clonedAnimal = null;
try {
clonedAnimal = (Animal) super.clone();
clonedAnimal.setDescription(description);
clonedAnimal.setNumberOfLegs(numberOfLegs);
clonedAnimal.setName(name);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} // catch
return clonedAnimal;
} // method clone

Sheep.java
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.

Code:

public class Sheep extends Animal {

Chicken.java
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.

Code:

public class Chicken extends Animal {

AnimalCreator.java
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the "Prototype" pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.

Code:

public Animal retrieveAnimal(String kindOfAnimal) {	
if ("Chicken".equals(kindOfAnimal)) {
return (Animal) chicken.clone();
} else if ("Sheep".equals(kindOfAnimal)) {
return (Animal) sheep.clone();
} // if

return null;
} // method retrieveAnimal

AnimalClient.java
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of type Animal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.

Code:

AnimalCreator animalCreator = new AnimalCreator();	 
Animal[] animalFarm = new Animal[8];
animalFarm[0] = animalCreator.retrieveAnimal("Chicken");
animalFarm[1] = animalCreator.retrieveAnimal("Chicken");
animalFarm[2] = animalCreator.retrieveAnimal("Chicken");
animalFarm[3] = animalCreator.retrieveAnimal("Chicken");
animalFarm[4] = animalCreator.retrieveAnimal("Sheep");
animalFarm[5] = animalCreator.retrieveAnimal("Sheep");
animalFarm[6] = animalCreator.retrieveAnimal("Sheep");
animalFarm[7] = animalCreator.retrieveAnimal("Sheep");
for (int i= 0; i<=7; i++) {
System.out.println(animalFarm[i].helloAnimal());
} // for

Console
The following is the output of the AnimalClient calling different concrete prototype objects.

Cluck cluck World. I am Chicken1. I have 2 legs.
Cluck cluck World. I am Chicken2. I have 2 legs.
Cluck cluck World. I am Chicken3. I have 2 legs.
Cluck cluck World. I am Chicken4. I have 2 legs.
Meeeeeee World. I am Sheep1. I have 4 legs.
Meeeeeee World. I am Sheep2. I have 4 legs.
Meeeeeee World. I am Sheep3. I have 4 legs.
Meeeeeee World. I am Sheep4. I have 4 legs.

Class Diagram Example

Design Pattern, Prototype Pattern, GOF

Sequence Diagram

Sequence Diagram by "Yanic Inghelbrecht" with Trace Modeler

References

  • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995
Prototype Prototype pattern Object (computer science) Design ANIMAL (image processing)

Published at DZone with permission of Andre Mare. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: