Factory Design Pattern

October 25, 2014

Reading time ~1 minute

This is one of the basic Creational patterns and one of the most used one in the software development. It is one of the GoF patterns. Like most design patterns it has multiple different implementations, but in every variant the purpose is to hide the instantiation logic from the client and to get instances of objects through common interface.

Factory Pattern Diagram

So lets think of a scenario in which the Factory Pattern could be of use. For example if we are building a game we might need multiple instances of game objects like characters, landscape or simply trees of rocks.

So lets create a simple implementation for this scenario using C#.

We would have a Character, Factory that will create the characters and Init file that will use the Factory.

So here is our Character. It is very simple implementation. All we’re doing is overriding the ToString method to give our Character some console representation. Nothing too fancy.

Here is our Factory. Again very simple implementation that just gives as new instance of our Character every time the CreateCharacter method is called.

And finally lets put everything together using some Init File that will be the entry point of our simple program.

Maybe you ask yourself - What will happen if I need my factory to return multiple kinds of objects? Do I need to create a new Factory for every kind of Object? Actually no, there are multiple kinds of solutions for this problem. One of the possible solutions involve registering different kinds of objects using reflection but more about this in another post.

Release early, release often (REPO) strategies

In this post I will try to review some of the things that every company should do in order to have better product releases. Continue reading

New things in C# 6

Published on December 22, 2014

ECMAScript 6 - The new JavaScript Part 2

Published on December 07, 2014