Deadly Designs

SAKSHI SINHA
3 min readMay 1, 2020

--

Part Four: Problematic Prototypes

As per definitions, a prototype “copies something that already exists”.

Storytime:

Maggu is a teacher’s pet. He loves assignments, completes them before deadlines, and ensures that they are always to the point.

You want his sort of grades but are too lazy to make an effort and so are your friends!

Everyone wants his assignments but Maggu does not like to talk to anyone except Mistry. People start flooding Mistry with requests to get Maggu’s assignment.

Mistry being a money-minded guy decides to team up with Maggu to rake up some cash and comes up with his own sort of a system.

We have three components in the system

Prototype: Maggu’s Assignment that he gives to Mistry.

Prototype Registry: Mistry, who keep a track of the number of assignments ordered and the amount of cash he has received.

Clients: Our lazy ass students paying for the assignment’s copy delivered to them and modifying it so as to not get caught.

Let us now look into Maggu’s behavior

  • You cannot insatiate conversations with him. Only Mistry talks to him.
  • You cannot thus ask him for the copies of his assignment

Maggu seems like an interface and Mistry like the registry!

Maggu/An interface

  • Cannot be instantiated
  • Has no constructors
  • Has all abstract methods
  • Implemented by a class(Mistry for this case)

Mistry The registry

  • Keeps track of the objects created, clones, and returns a new clone.

So, here is a small diagram explaining the system

Let’s understand this better by a code

Mistry’s Messed Up Management

Why Prototyping?

When cloning is a better option than creating a new object

Your Assignment as per you

  • Is time-consuming to do
  • Is cost-intensive to you (Needs pen, paper and time XD)
  • Is easier to copy from Maggu and modified as per your needs

Thus, you do a thing like prototyping

(Getting a modified copy of Maggu’s Assignment)

Similarly, say we have an object that loads data from the database. The data loaded needs to be modified multiple times. Creating a new object using the new keyword would mean loading all the data from the database again.

In such a case cloning the existing object to a new one and then manipulating data would be a better thing to do.

However, making a copy can sometimes be complicated

  • You do not know the topics the assignment covered thus have to study it panicking a night before the exam. Similarly, the client does not know the concrete product classes in case of a prototyped mechanism.
  • Overkill for a project that uses very few objects

This was thus an overview of the Prototype Design Pattern. I hope it could make some sense. Taking my leave with a meme :D

Source: Refactoring Guru

References: Head First Design Patterns, Geeks For Geeks, and Refactoring Guru.

#TraningTheDesignDragon #DeadlyDesigns

--

--