The Prototype

Design Pattern Practice in Python

Whenever I think of a prototype, a hastily constructed machine in someone’s garage comes to mind. The machine does what it was designed to do, but only well enough to prove that an idea was possible.

Google says a prototype is ‘a first, typical or preliminary model of something, especially a machine, from which other forms are developed or copied.’

In software design patterns, the Google definition sort of works!

The Prototype design pattern is a set of properties and/or methods, that are used to supplement other sets of properties and methods when creating objects. In Python this would be some class, that is used to give several other classes common properties and methods that they would each have.

In a class for an animal, there would be a set of properties. Then to make a class called ‘Fat Dog’, you could just create a Fat Dog instance, and extend that instance with the animal class. Write less code, get more functionality.

class FatDog(Animal):

A Prototype, in Python

I’ve recently finished a re-watch of one of my favorite shows, ‘Community’. So as an homage, I’ll use the Greendale mascot, the ‘Human Being’ to illustrate the use of a prototype.

The Greendale Human Being. An idea that Dean Pelton meant to be inviting looks like a cross between the Slenderman and the gimp from Pulp Fiction. Thanks Pierce.

The Greendale Human Being is the epitome of a prototype. The embodiment of traits and characteristics that anyone can possess. Whether you attend Greendale or not.

So how do we make an application out of this? Maybe some sort of RPG where the characters attending Greendale have some basic stats that any NPC / Character would have?

Abed: “Can anyone else tell that a guy is coding us into a very basic video game right now?”
Jeff: “Sure, Abed. We’re in a digital world that some nerd is coding in his spare time.”
Troy: “Oh My God, we’re not even their main job?”
Abed: “Nope, he thinks writing code, and then writing about that code will help him along professionally.”

Let’s code it

Alright, so what traits does an Alumnus have? According to Quora you don’t have to graduate, but must attend for at least two semesters. That works!

class HumanBeing():
Human Being class is base class for any other character class
    def __init__(self, f_name, l_name): 100
self.is_enrolled: True
self.f_name = f_name
self.l_name = l_name
def say(self, phrase):
Print something to the screen, said by the human.

And that is all.

That is the first prototype of the Greendale Community College RPG. Can it have more properties / methods? Sure! Will it? Almost Certainly! But what matters is now, any character from our fake Community RPG can rely on this prototype for it’s most basic properties. Here is a nice example of the prototype pattern being implemented.

I like the idea of prototypes

When I first started coding all the videos and blogs I read talked about D.R.Y. (Do Not Repeat Yourself) a lot. I feel like prototypes fall in line perfectly with that practice. In order to avoid repeating yourself, you create a whole different type of object, one time and avoid needing to write thirty times.

Next Time

I’m going to stick with creational patterns, so the next pattern I’m self.teaching is the singleton.

Thanks for reading!

Show your support

Clapping shows how much you appreciated Alex Wilson’s story.