GetNoticed IT#10: Factory method

Good evening!
Post number TEN in my GetNoticed IT series! In my DEV#8 post I mentioned that most likely my next IT post will be about tabs in WPF/MVVM but as you’ve probably figured it out it won’t be about tabs. It’s going to be about factory method design pattern.
So, factory method allows to encapsulate object creation. For instance, you can bring all logic that decides which object to create into one class. Or another example. In my project I used some singletons (which is considered to be an anti-pattern). This could have been avoided by using factory method (and it will be). In this case wherever I wanted to use an instance of singleton I would call factory method that returns object of my singleton instead of static property Instance.

How it works
Basic construction of that pattern is that you create abstractions of your factory class and of your – call it – product. Then you can create multiple different factories that return concrete product. Depending on what logic you will include in your concrete factory. Once you have that concrete factory you inject that into your class and let your class use that factory to instantiate concrete objects. It is very well explained by Christopher Okhravi in his just recently posted video. I highly recommend his videos.

Example
I prepared quick example for the purpose of this post. There are books and bookstore:

Pretty simple: book described by two properties and a method to display details; and a bookstore with a collection of books and a method to list all books in that collection.
Now, our books can be paper, ebooks or audiobooks:

All of the above can be stored in our bookstore. And we want to decide which one to create at run time. So, let’s introduce a factory. We’ll start with abstraction and then concrete factories for each type of book. Our factory will have only one method that returns an object which is abstracted by IBook interface:

Now, we need to update our BookStore class and equip it with factory property and allow a factory to be injected in the constructor.

What more you can notice in the code above Factory property is public therefore you can swap factories anytime you want.
And here’s how you can do that:

When an instance of BookStore is created in gets default factory injected through the constructor and then every time a book is about to be created the factory is replaced by another factory. The output of the above would be:

Notice that each book is of different type depending on which factory has been used to create the object.
So, that’s the basic use of factory method. To dig deeper I recommend the links below:

https://www.youtube.com/watch?v=EcFVTgRHJLM
https://stackoverflow.com/questions/69849/factory-pattern-when-to-use-factory-methods

I hope that this post (as the rest of my IT posts) is in any way useful for you. That is my last post IT series post for Get Noticed contest. But I will continue blogging and reporting my learning progress so if you’ve been hoping for that post about tabs please come back later. You’ll get it, promise!
Thanks, Michal