Scala Design Patterns.
上QQ阅读APP看书,第一时间看更新

The need for design patterns and their benefits

Writing code without the conscious use of a design pattern is something many software engineers do. In the end, however, they either end up using one without realizing it, or they end up with code that can be improved in some way. As we mentioned earlier, design patterns help to write efficient, readable, extendable, and testable code. All these features are really important to companies in the industry.

Even though in some cases it is preferable to quickly write a prototype and get it out, it is more usually the case that a piece of software is supposed to evolve. Maybe you will have experience of extending some badly written code, but regardless, it is a challenging task and takes a really long time, and sometimes it feels that rewriting it would be easier. Moreover, this makes introducing bugs into the system much more likely.

Code readability is also something that should be appreciated. Of course, one could use a design pattern and still have their code hard to read, but generally, design patterns help. Big systems are usually worked on by many people, and everyone should be able to understand what exactly is going on. Also, people who join a team are able to integrate much more easily and quickly if they are working on a well-written piece of software.

Testability is something that prevents developers from introducing bugs when writing or extending code. In some cases, code could be created so badly that it is not even testable. Design patterns are supposed to eliminate these problems as well.

While efficiency is often connected with algorithms, design patterns could also affect it. A simple example could be an object that takes a long time to instantiate, and instances are used in many places in an application, but could be made a singleton instead. You will see more concrete examples in the later chapters of this book.