Prototype Pattern

[tr] Türkçe Oku

2023-06-11

Once upon a time, there was a place called the Land of Creators. In this land, there was a secret to creating new objects quickly and efficiently. This secret was called the “Prototype Pattern”.

One day in the Land of Creators, there was a magical drawing pen. This pen had the ability to copy any object it touched. This feature made the creation of objects easy and fast, because instead of creating each object from scratch, an existing object was being copied.

This story is implemented in the C# language as follows:

public interface IPrototype
{
    IPrototype Clone();
}

public class ConcretePrototype : IPrototype
{
    public IPrototype Clone()
    {
        return (IPrototype)this.MemberwiseClone(); // Shallow copy
    }
}

The same story in Java:

public interface Prototype {
    Prototype clone();
}

public class ConcretePrototype implements Prototype {
    public Prototype clone() {
        try {
            return (ConcretePrototype) super.clone(); // Shallow copy
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}

In Python:

import copy

class Prototype:
    def clone(self):
        return copy.copy(self) // Shallow copy

In Go:

type Cloner interface {
	Clone() Cloner
}

type ConcretePrototype struct {
	// ...
}

func (p *ConcretePrototype) Clone() Cloner {
	out := *p // Shallow copy
	return &out
}

And in Rust:

pub trait Clone {
    fn clone_box(&self) -> Box<dyn Clone>;
}

#[derive(Clone)]
pub struct ConcretePrototype {
    // ...
}

impl Clone for ConcretePrototype {
    fn clone_box(&self) -> Box<dyn Clone> {
        Box::new(self.clone()) // Shallow copy
    }
}

This story expresses the main idea of the prototype design pattern: copying an existing object can be faster and more efficient than creating it from scratch. This is particularly useful when it’s necessary to create large and complex objects, or when an object needs to preserve a particular state. This pattern can make the code more readable and easier to maintain. When each object is copied, all properties and states are copied in the same way, which ensures consistency in the code.



More posts like this

The Dance of Components: The Composite Design Pattern

2023-06-22 | #composite-pattern #design-patterns #structural-patterns

Once upon a time, there was a tree in a forest. This tree was a complex structure with leaves and branches. Each branch could have smaller branches and leaves on it. The tree worked as a whole, holding its branches and leaves together. This tree is an example of the Composite design pattern. The tree (Composite) contains two types of components: branches (also Composite) and leaves (Leaf). Both branches and leaves implement the same interface (Component) recognized by the tree.

Continue reading 


Gaining Flexibility by Building Bridges: Analysis and Implementations in Four Different Languages with the Bridge Design Pattern

2023-06-22 | #bridge-pattern #design-patterns #structural-patterns

Once upon a time, there were two kingdoms. Despite being very close to each other, a wide and deep river was located between them. This river made it difficult for the kingdoms to interact with each other. To solve this situation, both kingdoms decided to build a bridge. This bridge became an interface that facilitated communication between the two kingdoms. However, the bridge had different structures and features on each side.

Continue reading 


Decorator Design Pattern: The Adornments of Software

2023-06-15 | #decorator-pattern #design-patterns #structural-patterns

Once upon a time, there was an object named “Component”. This object was used to perform a specific function. However, sometimes it was necessary to extend or modify this function. That’s where “Decorators” came into play. Decorators are objects that “decorate” or extend the Component. A Decorator comes on top of a Component and extends or modifies its function. This is used to extend the functionality of the Component without changing it itself.

Continue reading 