Nemanja Rogic on X: "Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
Brief introduction + demo code ↓
#designpatterns #dotnet #csharp #coding #programming"
Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
Brief introduction + demo code ↓
#designpatterns#dotnet#csharp#coding#programming
Prototype is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
Brief introduction + demo code ↓
#designpatterns#dotnet#csharp#coding#programming
The first example will demonstrate how Prototype pattern can be really useful with objects that manipulate with a complex state.
We can see that LogicPuzzle maintains a complex state for every invoked action.
Consider the next usage example. What if after a certain time of manipulating with the complex object we need to create a copy of the object?
Calling executed methods again isn't an option since it can be expensive and we may not even know what methods were executed.
That's when we can use Prototype design pattern to create a copy of the existing object.
You can think of Prototype as a preset object, where you save a bunch of state as a new starting point.
The second example uses a little bit different structure for implementation. It introduces a prototype registry, which provides an easy way to access frequently-used prototypes.
+ You can clone objects without coupling to their concrete classes
+ You can get rid of repeated initialization code in favor of cloning pre-built prototypes
+ You can produce complex objects more conveniently