https://airbrake.io/blog/design-patterns/factory
The post this week hinges on factory pattern method. Similar to simple factory, factory method revolves around the concept of a factory. An important difference is that factory methods provides a simpler way to further abstract the underlying class. As further explained in the article, like factories, the code should make use of an intermediary factory class, which provides for an easier way to rapidly produce objects for the client. The main benefit as explained is that the factory should “take care of the work for us”, meaning that we do not have to care about what happens behind the scene, we can just want to use the codes.
In order to explore a real world example of implementing a factory method, the article explores the relationship between authors and publishers. As we all know, there are many different types of authors. For example, those that specializes in fiction or nonfiction. Similarly, different publishers prefer authors that specializes in certain fields and styles of writing. An example of a publisher is a newspaper. A newspaper is a type of publisher that focuses on publishing nonfiction authors.
In the example, the publisher acts as a factory method, where it always have some tasks that remains the same. The baseline concept is that it acts as a factory to abstract and separate the different types of publishers from the different types of authors. The main goal is to separate the process of hiring the type of author from the particular type of publisher. The code starts off with a basic IAuthor interface, which contains the Write() method. From there two unique types of authors are used FictionAuthor and NonfictionAuthor. Both contains the Write() method. The publisher class contains the core component of the factory method pattern that is it contains a HireAuthor() method and a Publish() method.
The convenience of the factory pattern method in this case is that implementations of the Blog.HireAuthor() and Newspaper.HireAuthor() methods can be different. All in all, the client can freely create a blog and newspaper by issuing the Publish() command without knowing the internal workings of the factory method. The result is automatic instantiations of the appropriate types of IAuthors, which implies that it instantiates the correct type of author for what the type of publication had expected. The Publisher class furthermore adheres to the open/closed principle so that it can be easily expanded without affecting the other internal codes. The main idea here is that any inherited classes from Publisher can be referenced without having knowledge of how the authorship or the writing process works behind the scenes.
I chose this article because the example outlines the advantages and disadvantages of factory pattern methods. One advantage based on the example is that it encourages consistency in code that whenever an object is created it uses Factory instead of different constructors at different client side. Another advantage is that it enables subclasses to to have extended versions of an object. Therefore, creating objects inside factory is more flexible than creating one directly in the client. Finally, factory design makes it easier to debug and troubleshoot the code since it centralizes object creation and every client is receiving the object from the same place. The main disadvantage that I see from the example is that it can complicate the code when it is unnecessary. That is why I chose this topic this week in order to analyze the advantages and disadvantages of factory pattern.
From the blog CS@Worcester – Site Title by myxuanonline and used with permission of the author. All other rights reserved by the author.