Section 6 - part 2 (Prototype Design Pattern)
Section 6 - part 2 (Prototype Design Pattern)
Engineering
Section (6) Part 2– Prototype Design Pattern
• 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:
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