Post

Log inSign up

Post

Log inSign up

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"

@rogic_nemanja
Nemanja Rogic
@rogic_nemanja
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
10:37 PM · Feb 25, 2021·
1

Log in or sign up for X

See what’s happening and join the conversation

Continue with phone
or
Log in with username or email

Relevant people

Avatar
Nemanja Rogic@rogic_nemanjaFollow
Senior Software Engineer @Microsoft • Passionate about software craftsmanship, shipping reliable products, and tackling challenging problems

Trending now

Terms·Privacy·Cookies·Accessibility·US TIDA·Ads Info·© 2026 X Corp.
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    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
    10:37 PM · Feb 25, 2021·
    1
  • @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    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.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    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.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    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.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    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.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    As an initial step, we will define the prototype interface as well as the Color class that will implement that interface.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    ColorRegistry is used to store information about frequently used colors.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    Some less commonly used colors can be made by mixing or modifying colors from the registry.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    Prototype pattern is required when object creation is time-consuming, and costly operation, so we create a new object by cloning existing objects.
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    + 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
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    - Cloning complex objects that have circular references might be tricky
    1
    @rogic_nemanja
    Nemanja Rogic
    @rogic_nemanja
    Feb 25, 2021
    Full implementation with a few additional usage examples can be found on
    GitHub - nemanjarogic/DesignPatternsLibrary: A comprehensive design patterns library implemented in...
    From github.com