-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added prototype creational pattern summary.
- Loading branch information
1 parent
17930cc
commit 741d557
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Prototype Pattern | ||
|
||
## Definition | ||
The Prototype Pattern is a creational design pattern that allows you to create new objects by copying an existing object, known as the prototype. This pattern is particularly useful when the cost of creating an object from scratch is expensive or complex. | ||
|
||
## Intent | ||
- To enable object creation by copying an existing object (the prototype) instead of instantiating a new one. | ||
- To decouple the creation of objects from their specific classes, allowing for more flexibility. | ||
|
||
## Problem | ||
- You need to create multiple objects of the same type with the same initial state, but creating them from scratch is resource-intensive. | ||
- You want to avoid the dependency on the concrete class of the object being created. | ||
|
||
## Solution | ||
- Define a prototype interface with a method for cloning objects. | ||
- Let concrete classes implement this interface to allow copying of their instances. | ||
- Use the prototype instance to clone new objects when needed, avoiding direct instantiation. | ||
|
||
## Key Components | ||
- **Prototype Interface:** Declares the method for cloning objects. | ||
- **Concrete Prototype:** Implements the clone method to copy its instances. | ||
- **Client:** Uses the prototype to create new objects by cloning. | ||
|
||
## When to Use | ||
- When object creation is resource-intensive (e.g., requires a complex computation or database access). | ||
- When the system should be independent of how its objects are created. | ||
- When you need to create new instances with the same state as an existing object. | ||
|
||
## Benefits | ||
- **Performance Optimization:** Reusing an existing object reduces the overhead of creating objects from scratch. | ||
- **Decouples Object Creation:** The pattern abstracts the creation process, making the client code independent of concrete class implementations. | ||
- **Flexibility:** Allows dynamic addition of new object types without modifying existing code. | ||
|
||
## When to Avoid | ||
- When object creation is simple and inexpensive, as cloning might add unnecessary complexity. | ||
- If the objects being cloned have complex dependencies or hidden side effects. | ||
- When deep cloning is required, and the object graph is too large or complex to manage effectively. | ||
|
||
## Summary | ||
The Prototype Pattern enables object creation by copying an existing object rather than instantiating a new one. This pattern is ideal for cases where object creation is costly or when cloning is required to preserve the state of an existing object. | ||
|
||
## What to Prepare for Interview | ||
- **Definition and Use Case:** Be ready to define the prototype pattern and explain scenarios like creating objects with expensive initialization or requiring copies with similar properties. | ||
- **Code Example:** Prepare an example that demonstrates cloning objects using the prototype pattern. | ||
- **Deep vs. Shallow Copying:** Understand the differences and be able to explain when to use each in the context of the prototype pattern. | ||
- **Real-World Use Cases:** Examples include game development (cloning game objects like enemies or obstacles), GUI systems (cloning UI components), and document processing (duplicating templates or layouts). | ||
- **Comparison with Other Patterns:** Be prepared to contrast the Prototype Pattern with the Factory Method (which creates new objects) and Singleton (which ensures a single instance). | ||
- **Challenges:** Discuss challenges like managing deep cloning and avoiding unnecessary complexity in simple object creation scenarios. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters