Skip to the content ForoHackers
Log inJoin

Design patterns: start here

Design patterns
#3345 · · Quote · Report

A design pattern is a named, reusable solution to a problem that keeps coming back in software design, with its context and its costs. Patterns are not code to paste: they are a shared vocabulary that lets a team say "use an observer here" and be understood, and they show up whenever objects or functions must cooperate.

Key concepts

  • Problem, solution, consequences: a pattern is only a pattern when you know when not to use it.
  • Creational patterns: factory, builder and singleton control how objects get made.
  • Structural patterns: adapter, decorator, facade and composite shape how objects fit together.
  • Behavioural patterns: strategy, observer, command, iterator and state shape how objects share work.
  • Principles underneath: program to interfaces, prefer composition to inheritance, keep one responsibility per unit.

Vocabulary

  • Gang of Four: the authors of the classic catalogue of object-oriented patterns.
  • Anti-pattern: a common response to a problem that makes things worse.
  • Dependency injection: passing a part its collaborators instead of letting it create them.
  • SOLID: five object-oriented design principles, from single responsibility to dependency inversion.

What to study, in order

  1. Write a small program with long if chains, then replace them with a strategy.
  2. Implement observer and iterator by hand in your main language.
  3. See which patterns your language makes unnecessary, such as functions as values replacing command.
  4. Read a mature codebase and name the patterns you find.
  5. Refactor one pattern you overused back to plain code.

Related tags

#architecture #refactoring #functional-programming #language-design

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply