Response to article “Design Patterns: are they so difficult to understand?”

This is a response to the article “Design Patterns: are they so difficult to understand” posted by s0943644 [1].

In the article, the author talks about the importance of using design patterns in software development and presents a problem that people are still arguing about the best way to study these patterns. Then the author covers single responsibility principle and factory pattern that he used in real project development and explains the steps he took to understand them.

Yes, the essential part to understand design patterns is to code with them.

I totally agree with the author on this point. For my own case, I ever took the course Software Engineering with Objects and Components. In the course, we covered concepts of design principles as well as several design patterns like factory pattern, singleton pattern, composite pattern, etc. I have to admit that as a student with little experience on software development, it was so difficult for me to understand these abstract concepts.

At that time, I was also developing a game using Java. In the game, users collected gems in a house with several rooms. Players could move between rooms and turn left or right, and were shown with photos from different perspective of the room as if they were in it.

At first, I wrote all the functions in one class and it worked well. However, when I tried to add a new ‘’room’’ to the ‘’house’’, I had to change almost all methods in the class. It was a disaster because the project was in low extensibility and was also hard to read for others. Then I compared the design principles I learnt from SEOC course and reviewed the code. No doubt I found no design pattern was used nor design principle was obeyed. And I also realized that I could use all these principles in my project to make it a better one. For example, one principle is single responsibility, to achieve this, I separated the class into several ones and each was responsible for only one purpose. Another example is, for open/closed principle, I used interfaces and let the class with similar functions implement the interface so that they were open for extension and close for modification. To be specific, one function of the project was to retrieve photos from different sources like Flickr [4], deviantART [5] and use these photos to build the ‘’house’’. After modifying the code, I implemented the function by using a PhotoFinder interface, several classes like FickrPhotoFinder, devianARTPhotoFinder that enable our project to retrieve photos. And letting the class MyPhotoController that implemented the interface decide which class to instantiate. And I realized that this satisfied factory method pattern.


Figure 1: UML diagram (*if you cannot see it clearly, please click it and view the large one)

After re-developing the project, the quality of the code was highly improved. And I had a much better understanding on how SOLID works in software development and how design patterns are used.

More things to discuss on learning design patterns through coding.

Sometimes people say they don’t have the opportunity or experience to develop large-scale software, and this is a barrier for them to practice and understanding design patterns. However, I think even small projects with few functions can be developed using different patterns. For example, when we write a UI using Java Swing, observer pattern is used when adding a button and registering the button with an action listener.

Another thing I want to mention is, we should not change our design so that it satisfies some certain pattern just because we want to use that pattern. Instead, we use patterns because after taking SOLID principles into consideration, we find our design satisfies some certain patterns. In fact, the principles are easy to remember, and as for beginners with little experience, it would be helpful if we think through all the principles at project designing stage.

Back to the discussion of the effective way in learning design patterns.

Other than coding practice the author mentioned in the article. I would like to talk about another effective way for beginners to learn design patterns, or at least helpful to me-—using real life examples.

Examples are always useful in understanding the concepts. And I think real life examples are more helpful than programming examples. For instance, when studying singleton pattern in the lecture, a given example was that, sometimes the data is required to be held consistently in one version [2]. This is easy to understand, but not intuitive enough. I refer to Wikipedia webpage [3]. It mentions several examples on how to implement singleton pattern and an example on Java Abstract Window Toolkit (AWT). For learners who are not quite familiar with AWT, this was not easy to understand as well.

Don’t we have other good examples? Wait! I thought of one when I was learning this pattern. Thinking about the president in the USA, there is only one person in that position (at least for now). In this example, president is the single instance, getPresident() method ensures that only one president can be instantiated. This example is vivid, it’s easy to understand and is helpful in understanding singleton pattern. In fact, I tried to learn all the patterns in this way and found it was really interesting and helpful.


Figure 2: Singleton pattern


Based on similar experience of learning design patterns by coding, I definitely agree with the author that coding is an essential part to learn these patterns. Besides, real life examples regarding to these patterns are also helpful for us to understand these patterns.