Design Patterns #4: Factories

Hello there!
It already is a weekend so time for next pattern from the list: factories. Factories are creational-type patterns which means they are used to (surprise, surprise) to create instances. There are three types of factories (of which only latter two are considered as design patterns): simple factory, factory method and abstract factory. Let’s start with the non-pattern one.

Simple factory is a class that has a method to create a certain types of objects where the decision what exactly type to create is made by the factory itself based on given parameters. Assume that we’re building a book store and we want to sell paperbooks, audiobooks and ebooks. We have definitions of a sub-class for each of them implementing IBook base interface. We can build our simple factory in a way that we pass a char parameter and if it matches with first letter of our sub type we create that sub-type:

And that’s it: we have our simple factory. But the problem is that when we want to introduce another sub-type it is necessary to modify that class. And we don’t want that. And why you find [here].

About factory method I’ve already written [one post]. Consider this post as a supplement to the previous one.
Factory method solves the issue we had with simple factory by removing it’s responsibility to decide (based on the parameter) what type exactly to create. If we want to modify the factory we’ve created above we would need to create separate classes responsible for creating each type of book. And that’s what was done in my [first post] about this pattern:

Now, if we need another sub-type we simply create another class that will create new sub-type.
Let’s take a look at the definition:

Factory method pattern defines an interface enabling to create a single object making the sub-class implementing this interface to decide what exactly type of object to create.

What does the above mean? It means that the client at the point of invocation of factory method doesn’t care (and doesn’t need to care) what type is going to be created: the client wants an object and is going to get one. Very good example is provided by Christopher Okhravi [here] – a simulator a national park where multiple types of animals dwell and the simulator care only about the right number of animals, when it needs an animal to spawn it just calls the factory and the factory decides what animal to create. (I’ve already said that multiple times but I highly recommend his [playlist] about design patterns.)
And now the UML:

We have an abstraction of factory method class and its concretion that creates an instance of a class which is a concretion of one interface common to all sub-types of product.

If you know how the factory method works understanding the abstract factory will be a piece of cake. Let’s start with the definition:

Abstract Factory provides an interface for creating multiple related or dependent objects without necessity to define their concrete classes.

And the UML:

So, the client uses a factory to create instances. The factory is abstracted by the interface which has multiple (in the case above two) concretions which create two related sub-types of ProductA and ProductB. The IFactory interface doesn’t specify what concrete classes will be instantiated and the client doesn’t care. It is the concretion of the factory which specifies what sub-types of products exactly to create. So, in really really short we may say that the difference between factory method and abstract factory is that the former creates only one type objects while the latter can create multiple types of objects (related and/or dependent).

And that’s it. Before I say goodbye: have a look at the links below to find out more about factories; plan for next week – singleton.

Christopher Okhravi: Factory Method Pattern – Design Patterns (ep 4)
Christopher Okhravi: Abstract Factory Pattern – Design Patterns (ep 5)
CODE Project: Factory Patterns [Snesh Prajapati]

.NET programming basic course

As I had mentioned in one of my previous posts I attended a programming course dedicated for C#/.NET beginners. The course was organised by polish IT leader Comarch (at the end of this post you will find a link to the course on their website).
I have to say that most of the topics presented during the course were nothing new to me. But, it’s completely different than learning on your own no matter if it’s from a book or YouTube course or lecture. Having that possibility to ask questions and get instant answer is the biggest advantage. Sometimes, you think that you might have not understood something you ask a question to clarify and you get an affirmative answer –  that’s a confidence boost. That gives you that feeling that you’re on the right track. You can’t get that any other way than face-to-face learning experience.
What’s more that improves the quality of the course is the fact that it was mostly all time practice. The theory part was more of a brief introduction to the topic than a lecture. All important things were presented with an example and tested by students ourselves.
After completing the course I have one advice for anyone who wants to get into programming: be extremely careful when learning online. Some of the learning materials are not prepared with that mindset that someone who’s going to learn is absolute beginner. They tend to go shortcuts, don’t pay attention to details which denies learners to fully comprehend the knowledge. And sometimes they teach bad habits (for instance I have not heard before the course about the ‘private field, public property’ convention).
What I learnt during the course and I find most important for me at this stage is deeper insight into class inheritance and polymorphism.
I am pretty happy with the course but still there were some disappointments. I was really hoping to get some more details about such aspects as interfaces, data-binding and MVVM model. We talked a bit about interfaces, the tutor showed us an example of data-binding and he said few words about MVVM. But I was simply hoping for more.
Generally, I find the course much beneficial for me and I will surely continue with courses from Comarch. The course ended with issuing a certificate. That’s my first 🙂
Thanks, Michal

Programowanie .NET – kurs podstawowy (Comarch)

MakeNoMistakes – my implementation of a classic

Hello All,
It has been a while since my last post in which I announced ‘another small app’. It is now time to publish its first version. Generally, it is another version of small game which is distributed with MS Windows and it is about finding mines. The app I have written is slightly different. It gives a player a possibility to disarm a mine if it is found by right clicking. In classic minesweeper it is possible to click with both mouse buttons to uncover all adjacent tiles. In my app it can be done by hovering a tile for a short moment. In this case uncovering the mine will result in an explosion and therefore the game ends.
This is the first version. It is not finished, but I would say it is 95% complete. What is missing is possibility to play a game on custom tile-board and a hall of fame (and nice graphics, but I am not an artist at all). Right now I am publishing only an EXE file (in a ZIP archive). Source code will be published when the application is completed.
Thanks, Michal

MakeNoMistakes (*.ZIP)