0% found this document useful (0 votes)
13 views

Section 6 - part 2 (Prototype Design Pattern)

The document discusses the Prototype Design Pattern, a creational design pattern that allows for the cloning of existing objects to avoid costly and complex object creation. It highlights various challenges in object duplication, such as private fields, time consumption, and tight coupling, and presents the Prototype pattern as a solution that minimizes these issues by delegating the cloning process to the objects themselves. The document outlines the steps to implement the Prototype pattern, including defining a prototype interface and creating concrete implementations.

Uploaded by

israahussein737
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Section 6 - part 2 (Prototype Design Pattern)

The document discusses the Prototype Design Pattern, a creational design pattern that allows for the cloning of existing objects to avoid costly and complex object creation. It highlights various challenges in object duplication, such as private fields, time consumption, and tight coupling, and presents the Prototype pattern as a solution that minimizes these issues by delegating the cloning process to the objects themselves. The document outlines the steps to implement the Prototype pattern, including defining a prototype interface and creating concrete implementations.

Uploaded by

israahussein737
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Selected Labs in Software

Engineering
Section (6) Part 2– Prototype Design Pattern

Eng. Essam Ayman


Teaching Assistant
Prototype Design Pattern
Prototype is a creational design pattern that lets you copy existing objects without
making your code dependent on their classes.

Prototype provides a mechanism to create


objects by copying existing objects
(prototypes). Instead of creating new objects
from scratch using a new keyword, the
Prototype Pattern allows you to clone an
existing object. This can be particularly useful
when object creation is costly or complex.
Problem
• Imagine you are working on a graphics editor application where you frequently need to
create various shapes (e.g., circles, rectangles, and polygons). Each shape has multiple
attributes like size, color, position, etc.

• Challenges:

1. Object Creation Cost: If the shape objects have many configurations, creating them from scratch
every time can be expensive and error-prone.

2. Performance: Using the new keyword repeatedly in scenarios involving complex configurations
can degrade performance.

3. Duplication: You may frequently need to duplicate existing objects with slight variations.

• Key Goal: Minimize the overhead of creating and initializing objects while ensuring
flexibility in creating similar objects.
Problem: Check this code:
Problem 1: Object Duplication
To duplicate an object, you need to:
• Create a new object of the same class.
• Manually copy all field values from the original object to the new one.
Problem 2: Private Fields
• If the object has private fields, you cannot directly access them to copy their values.
This requires using getters or reflection, which compromises encapsulation.
Problem 2: Private Fields
• If the object has private fields, you cannot directly access them to copy their values.
This requires using getters or reflection, which compromises encapsulation.
Problem 3: Time Consuming
• Manually copying fields for every object is inefficient and prone to errors, especially for
objects with many fields.
Problem 3: Time Consuming
• Manually copying fields for every object is inefficient and prone to errors, especially for
objects with many fields.
Problem 4: Dependency on Classes (tight coupling)
• The client must know the exact class of the object to duplicate it, increasing coupling
and reducing flexibility.
Problem 5: Interface Abstraction
• In some scenarios, the client only knows the interface that an object implements but
not its concrete class. This makes manual duplication challenging.
Problem 5: Interface Abstraction
• In some scenarios, the client only knows the interface that an object implements but
not its concrete class. This makes manual duplication challenging.
Solution (prototype design pattern)
• The Prototype pattern delegates the cloning process to the actual objects that are
being cloned. The pattern declares a common interface for all objects that support
cloning. This interface lets you clone an object without coupling your code to the
class of that object. Usually, such an interface contains just a single clone method.

• The implementation of the clone method is very similar in all classes. The method
creates an object of the current class and carries over all of the field values of the old
object into the new one. You can even copy private fields because most
programming languages let objects access private fields of other objects that belong
to the same class.

• An object that supports cloning is called a prototype. When your objects have
dozens of fields and hundreds of possible configurations, cloning them might serve
as an alternative to subclassing.
Solution (prototype design pattern)
• Steps:

• Step 1: Define the Prototype Interface


• Step 2: Create Concrete Implementations of the Prototype
• Step 3: Create the Prototype Registry (optional)
• Step 4: Client Code
Solution (prototype design pattern)
• Step 1: Define the Prototype Interface
Solution (prototype design pattern)
• Step 2: Create Concrete Implementations of the Prototype

Circle
class
Solution (prototype design pattern)
Rectangle class
• Step 2: Create Concrete Implementations of the Prototype
Solution (prototype design pattern)
• Step 3: Create the Prototype Registry (optional)
Solution (prototype design pattern)
• Step 4: Client Code

You might also like