What is Prototype Design Pattern?
The prototype design pattern is a creational design pattern that allows creating new objects by cloning an existing object. This pattern is useful when the creation of an object is costly or complex, and we want to avoid repeating the same process for each new instance. By using the prototype pattern, we can create new objects by copying the properties and behaviors of an existing object, and then modifying them as needed.
Related Articles
One of the benefits of the prototype pattern is that it reduces the dependency on subclasses and factory methods. Instead of creating objects using specific constructors or factory methods, we can use a generic prototype object that can be cloned and customized. This makes the code more flexible and extensible, as we can add new types of objects without changing the existing code.
Components of Prototype Pattern
- Prototype: This will be an interface or abstract class used for the types of objects that can be cloned. In our example, it is going to be the Employee Abstract Class.
- ConcretePrototype: This class will implement the Prototype abstract class or interface for cloning. In our example, it will be the PermanetEmployee and TemporaryEmployee Classes.
- Client: The client is the class that creates a new object by asking a prototype to clone itself.
Let's dive into an example to illustrate the implementation of the Prototype pattern in C#.To implement the prototype pattern in C#
- We need to define an interface or an abstract class that declares a clone method. This method should return a copy of the object that implements the interface or inherits from the abstract class.
- We need to create concrete classes that implement the interface or inherit from the abstract class, and override the clone method to return a copy of themselves.
- We also need to create a prototype manager class that stores a collection of prototype objects and provides a method to access them by a key. This class acts as a registry for the prototype objects and allows retrieving them by their type or name.
Here is an example of how to use the prototype pattern in C# to create different types of cars.
- Creating the Prototype:We define an interface called ICar that declares a clone method and some properties:
public interface ICar { string Model { get; set; } string Color { get; set; } int Price { get; set; } public ICar Clone(); }
- Creating the Concrete Prototypes:Now we create two concrete classes that inherit from the ICar interface and implement the clone method to return a copy of themselves:
public class Sedan : ICar { public string Model { get; set; } public string Color { get; set; } public int Price { get; set; } public ICar Clone() { return (ICar)this.MemberwiseClone(); } }
public class Suv : ICar { public string Model { get; set; } public string Color { get; set; } public int Price { get; set; } public ICar Clone() { return (ICar)this.MemberwiseClone(); } }
- Creating the Prototype Manager:We create a prototype manager class that stores a dictionary of prototype objects and provides a method to access them by a key:
public class CarPrototypeManager { private Dictionary
_prototypes; public CarPrototypeManager() { _prototypes = new Dictionary (); } public ICar GetPrototype(string key) { return _prototypes[key].Clone(); } public void AddPrototype(string key, ICar prototype) { _prototypes.Add(key, prototype); } } - Client:Finally, we can use the prototype manager class to create new car objects by cloning the existing prototypes and modifying their properties:
// Create a prototype manager and add some prototype objects CarPrototypeManager manager = new CarPrototypeManager(); manager.AddPrototype("Sedan", new Sedan { Model = "Toyota Camry", Color = "White", Price = 30000 }); manager.AddPrototype("SUV", new Suv { Model = "Ford Explorer", Color = "Black", Price = 40000 }); // Create a new sedan object by cloning the prototype and changing the color ICar sedan = manager.GetPrototype("Sedan"); sedan.Color = "Red"; // Create a new SUV object by cloning the prototype and changing the model and price ICar suv = manager.GetPrototype("SUV"); suv.Model = "Honda CR-V"; suv.Price = 35000; // Display the properties of the new objects Console.WriteLine("Sedan: Model = {0}, Color = {1}, Price = {2}", sedan.Model, sedan.Color, sedan.Price); Console.WriteLine("SUV: Model = {0}, Color = {1}, Price = {2}", suv.Model, suv.Color, suv.Price); Console.ReadKey();
Output
As you can see, we have created two new car objects by cloning the existing prototypes and changing their properties. This way, we have avoided creating new objects from scratch and reused the existing ones.
The prototype design pattern is a powerful and flexible way to create new objects by cloning an existing object. It can help us reduce the complexity and dependency of object creation, and make our code more dynamic and extensible.
However, we should also be careful about the cloning process, as it may involve deep or shallow copying, and may affect the references or values of the object's fields. We should also make sure that the prototype objects are immutable, or at least thread-safe, to avoid concurrency issues.
The full source code is available here:
Happy coding!! 😊