Prototype Design Pattern

What is Prototype Pattern:
Below are the main points

1. Creating new objects (instances) by cloning (copying) other objects
2. Allows for adding of any subclass instances of a known superclass at runtime.
3. When there are numerous potential classes that you want to only use if needed at runtime.
4. Reduces the need for creating subclasses

UML Diagram:

Let’s see it by an example

1. Create Animal Interface class which extends Cloneable interface

2. Create CloneFactory class which is response create copy of the object

3. Create sheep class which create clone of the sheep object

4. Create TestClone class which actually call CloneFactory makeCopy function to create the clone of an object

Below is the output

Sheet is made
Sheep is being made
Learning daily is way to success
Learning daily is way to success
Sally Hashcode: 1311053135
CloneSheep Hashcode: 1550089733

Builder Design Pattern

What is the Builder Patter?
– The pattern used to create objects made from a bunch of other objects.
a. When you want to build an object made up from other objects
b. When you want the creation of these parts to be independent of the main object
c. Hide the creation of the parts from the client so both are not dependent
d. The builder knows the specifics and nobody else does

UML Digram:

Step 1: Create RobotBuilder interface

Step 2: Create OldRobotBuilder class which implements RobotBuilder

Step 3: Create RobotPlan interface

Step 4: Create Robot class which implements RobotPlan

Step 5: Create RobotEngineer class

Step 6: Create TestRobotBuilder class.
a. Create RobotBuilder object
b. Pass create robotbuilder object in RobotEngineer constructor.
c. With robotEngineer object call makeRobot


Robot built
Robot head: Ironman head
Robot Torso: Torso Ironman
Robot Arms: Powerful arms
Robot Legs: Flying legs

Please share your feedback at, also comment below.

Strategy design pattern

You use this pattern if you need to dynamically change an algorithm used by an object at runtime. Don’t worry, just see the article and you’ll get it.

UML Diagram

The pattern also allows you to eliminate code duplication. It separates behavior from super and subclasses. It is a super design pattern and is often the first one taught.

The strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. The strategy pattern

1. Defines a family of algorithms,
2. Encapsulates each algorithm, and
3. Makes the algorithms interchangeable within that family.

Problem: What if I say there are multiple subclasses from Animal and you need to tell if an animal has the capability to fly or not. What will be the solution?

a. Create a method in an Animal superclass and then subclasses will be overloaded.
b. Create an Animal interface with the fly method and then every subclass will be forced to create that.

Both above methods lead to duplicate the code a lot. Let’s see the perfect solution using strategy design patterns.

Step 1: Create Flys interface with fly and not fly capability.

Step 2: Create Animal Class with flyingType variable. See setter and getter method. This also leads to encapsulation.

Step 3: Create Dog subclass and initiate CantFly in the constructor

Step 4: Create Bird subclass and initiate ItsFlys in the constructor

Step 5: Create Main class to call Dog and Bird Class

Tell Dog behavior: I can’t Fly
Tell Bird behavior: Flying high

1. Here when you call Dog and Bird constructor (new Dog(), new Bird()) in AnimalPlay class (Step 5) then the fyingType object is called coming from Animal class. Check Dog and Bird class.
2. sparky and tweety is the Animal reference for Dog and Bird class.
3. When you call sparky.tryToFlys() method then it will call the fyls() method in step 1 according to the constructor you called in the Dog and Bird class.

Please share your feedback. If you need more explanation then email us at