Skip to content

Commit 3cd556e

Browse files
committed
add: prototype pattern
1 parent 9504d3c commit 3cd556e

23 files changed

Lines changed: 117 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
out
2+
*.class
3+
practice ce
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Prototype interface
2+
interface Person {
3+
Person clone();
4+
void display();
5+
}
6+
7+
// Concrete implementation of Person
8+
class ConcretePerson implements Person {
9+
private String name;
10+
11+
public ConcretePerson(String name) {
12+
this.name = name;
13+
}
14+
15+
@Override
16+
public Person clone() {
17+
return new ConcretePerson(this.name);
18+
}
19+
20+
@Override
21+
public void display() {
22+
System.out.println("Hello, I'm " + name);
23+
}
24+
}
25+
26+
// Client code
27+
public class Main {
28+
public static void main(String[] args) {
29+
Person originalPerson = new ConcretePerson("ThunderBolt");
30+
originalPerson.display(); // Hello, I'm ThunderBolt
31+
32+
Person clonedPerson = originalPerson.clone();
33+
clonedPerson.display(); // Hello, I'm ThunderBolt (cloned)
34+
}
35+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Prototype Design Pattern 🧬
2+
3+
<p align="center">
4+
<img src="https://refactoring.guru/images/patterns/content/prototype/prototype.png" alt="Prototype Design Pattern Illustration" width="400">
5+
</p>
6+
7+
The Prototype design pattern allows you to create new objects by copying an existing object's properties. It's like making custom cakes at a bakery—instead of inventing new recipes, you clone a design and customize it according to your preferences.
8+
9+
## 🔧 Key Principles
10+
11+
🧬 **Prototype Interface:** The prototype interface declares a `clone` method for copying objects. Concrete prototypes implement this method.
12+
13+
🧫 **Concrete Prototypes:** Concrete prototype classes implement the `clone` method to create copies of themselves with customized properties.
14+
15+
🔬 **Client:** The client code selects a prototype, clones it, and customizes the clone as needed.
16+
17+
## 🚀 Flow of Prototype Pattern
18+
19+
1. **Create Prototype Interface:** Define a prototype interface with a `clone` method.
20+
21+
2. **Create Concrete Prototypes:** Implement concrete prototype classes by extending the prototype interface. Implement the `clone` method to create a copy and customize properties.
22+
23+
3. **Create Client Code:** In your application, choose a prototype, clone it, and customize the clone.
24+
25+
## 🌟 Pros
26+
27+
🔁 **Flexible Object Creation:** Prototype lets you create customized objects by copying existing ones, avoiding complex constructors.
28+
29+
📖 **Simplified Customization:** Customize cloned objects easily by modifying their properties.
30+
31+
💡 **Minimized Subclasses:** Prototype reduces the need for creating multiple subclasses.
32+
33+
## 🛑 Cons
34+
35+
🔌 **Copying Complexity:** Cloning nested objects might require deep copying to ensure proper duplication.
36+
37+
🗂️ **Clones Management:** Managing multiple clones can be challenging in some scenarios.
38+
39+
## 🎮 Real-World Analogy
40+
41+
Think of a bakery where you make custom cakes. You copy existing cake designs and customize them with different flavors and decorations.
42+
43+
## 💼 Real-World Example
44+
45+
Imagine a graphic design app. Users create shapes with colors and sizes. Instead of creating separate classes for each shape-color-size combination, you use prototypes.
46+
47+
## 🤔 Why Use Prototype?
48+
49+
**Efficient Object Creation:** Prototype is used when creating new objects from scratch is resource-intensive or complex. By copying existing objects, you save time and resources.
50+
51+
## 💡 Problem Solved by Prototype
52+
53+
Imagine you're building a game with multiple characters. Each character has unique properties, and creating new characters from scratch would be cumbersome. Prototype solves this by allowing you to clone a character prototype and customize it for each character, saving development effort.
54+
55+
## 🤔 When to Use
56+
57+
Use the Prototype pattern when you want to:
58+
59+
- Create similar objects with customized properties.
60+
- Avoid subclass proliferation when dealing with variations.
61+
- Efficiently create objects without complex constructors.
62+
63+
## 🤓 Interview Questions
64+
65+
1. **What's the main idea behind the Prototype design pattern?**
66+
It involves copying existing objects to create new instances with similar properties.
67+
68+
2. **How is Prototype different from Factory?**
69+
Prototype focuses on copying, while Factory focuses on creating instances of classes.
70+
71+
3. **What's the significance of the `clone` method?**
72+
The `clone` method defines how an object can copy itself.
73+
74+
4. **When would you use Prototype over other patterns?**
75+
Prototype is useful when creating similar objects with variations, without resorting to complex constructors or subclasses.
76+
77+
## 🚀 Conclusion
78+
79+
The Prototype pattern simplifies object creation by copying existing ones. It offers flexibility and customization while minimizing the need for complex class structures. By using prototypes as templates, you can create and customize objects effortlessly, making your codebase more efficient and organized.
-408 Bytes
Binary file not shown.
-300 Bytes
Binary file not shown.
-451 Bytes
Binary file not shown.
-111 Bytes
Binary file not shown.
-131 Bytes
Binary file not shown.
-408 Bytes
Binary file not shown.
-300 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)