Chapter 1: What are Design Patterns?

大綱

A real-world example

“design patterns are reusable, template solutions to common development problems”

案例

“You’re the proud owner of a gardening company, and your business is really, er, blooming. You’ve only done a few small projects up to now - a tree planted here, and maybe a few flowers there. However, you just landed a big client who wants several dozen trees and flowers planted on their property. Your standard procedure has been for your employees to carry each flower or tree sapling into place individually. Once the flower has been temporarily placed in the flowerbed, your customer inspects and approves the arrangement before you plant everything in the ground. You’re worried it’s going to take forever to carry each flower and tree into place for this large project. And you even need a few people to carry some of the bigger trees While you could hire lots of temporary employees, you wouldn’t make a profit on the job. There’s got to be a better way! You decide to ask other gardeners what they do, and you find out they use wheelbarrows and carts. What a great idea! You tell your employees to use a cart to move multiple flowers at the same time into place and a wheelbarrow to move the heavy trees

Example explanation

“The “design pattern” here is the use of wheelbarrows and carts. These are common, best practice tools in gardening”

design pattern就是在軟體世界中,幫我們解決在某些特殊情境下問題的一種工具。

Types of design patterns

  • Structural design pattern:

    • Describes how objects are composed and combined to form larger structures. Examples of structural design patterns include Model-View-Controller (MVC), Model-View-ViewModel (MVVM) and Facade.

  • Behavioral design pattern

    • Describes how objects communicate with each other. Examples of behavioral design patterns are Delegation, Strategy and Observer.

  • Creational design pattern

    • Describes how to create or instantiate objects. Examples of creational patterns are Builder, Singleton and Prototype.

Criticisms of design patterns

  • “If you overuse design patterns, your project can become overly complex.”

  • “Many design patterns are made redundant by modern programming languages.”

  • “Design patterns are a lazy substitute for learning object-oriented principles.”

Benefits of design patterns

  • “Knowing design patterns allow you to spot similarities between code.”

Key points

  • Design patterns aren't concrete implementations, but rather, they are a starting point for writing code.

  • Design patterns collectively form a set of best practices to help you write more understandable and easier-to-maintain code.

  • There are three main types of design patterns: structural, behavioral and creational.

  • There are both criticisms and benefits of design patterns.

Last updated

Was this helpful?