Skip to main content

Posts

Showing posts with the label DIP

Template Method Pattern: Don't Call Us, We'll Call You!

So far, the Template Method has been my most used design pattern. That is the reason why this post is quite long. J Definition from Wiki The Template Method defines the program skeleton of an algorithm in a method, deferring some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure. A Real World Use Case Imagine that you have many different kinds of documents. You want to generate a pdf file from a corresponding word template. Each type has its own small modifications but the main process for document generating is the same. We apply the Template Method for this case. We define a final method including some steps (such as preparing for content, generating file) at a superclass. There are three possibilities for these steps at subclasses: Must be overridden: abstract methods. Not mandatory to be overridden: default protected methods. Can not be overridden: default private methods. Dissecting the Patt...