DEV Community

loading...
Cover image for A football factory

A football factory

konstantinosblatsoukasrepo profile image Konstantinos Blatsoukas ・2 min read

Intro

A short blog on how you can create again some football legends!
I created a factory that produces football legends by using a software design patter, one of the most used ones, the Factory Method!

Players that we miss

All the football fun's we miss some players, especially players that they did things that we couldn't imagine,
like a dribble, a shot from a long distance, an assist with some weird way and so on.
Let's see how we can re-create them and watch some magic again!

The football factory

A factory is a static method that takes optionally a number of arguments and creates instances of a certain type.
One good property of the factory method is that the client has no knowledge how the instance is
created (or doesn't even cares, just wants one instance).

What is needed for a method factory:

  1. an interface, this is the type that the static method returns
  2. a concrete implementation of the interface (we could have more than one)
  3. a static method that generates the desired instances

Those are the basic ingredients of the Factory Method.

In the football factory the three ingredients are mapped to the following code fragments:

  1. the interface (the type that the factory returns), includes just two methods for the name and the position of the legendary player

Alt Text

  1. a concrete implementation of the interface

Alt Text

  1. the static method (factory) that creates the actual instances, takes as an argument an enum (the legendary players)

Alt Text

  • the enum that factory requires as argument (the legends!)

Alt Text

Let's create some legends

Let's produce some football legends from the factory!

Alt Text

Now we can watch some magic!

Cheers!

Discussion (0)

pic
Editor guide