Evan Harmon - Memex

Design Pattern

In software engineering, a Software Design Pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. Rather, it is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
wikipedia:: Software design pattern

Book: Design Patterns by the Gang of Four

Book: Head First Design Patterns

Structural Design Patterns

Data Mapper Pattern

Creational Design Patterns

The Accumulator Pattern

  • Numerical
  • String Concatenation
    • def remove_vowels(s): vowels = "aeiouAEIOU" no_vowels = "" for each_char in s: if each_char not in vowels: no_vowels = no_vowels + each_char return no_vowels
  • Add 1...n
    • let n = 6; let total = 0; for (let i = 1; i <= n; i++) { total += i; } console.log(total);
  • an incrementing variable
  • LC: "This pattern of iterating the updating of a variable is commonly referred to as the accumulator pattern. We refer to the variable as the accumulator. This pattern will come up over and over again. Remember that the key to making it work successfully is to be sure to initialize the variable before you start the iteration. Once inside the iteration, it is required that you update the accumulator."

Modulo Operator to count nth element

  • take the index as the numerator and perform modulo over the increment you want as the denominator:
  • in a list of numbers, print every nth number def print_every(n, nums): counter = 0 for num in nums: if counter % n == 0: # take the index as the numerator and perform modulo over the increment you want as the denominator print(num) counter += 1 print_every(3, [4, 7, 2, 10, 1, 0, 9, 6])

Null Object Design Pattern

Factory Pattern

Behavioral Pattern

Pattern

  • "A sequence of statements, or a style of coding something that has general applicability in a number of different situations. Part of becoming a mature Computer Scientist is to learn existing and establish new patterns and algorithms that will form your toolkit. Patterns often correspond to your “mental chunking”."

Find Odd/Even Number & num divisible by x

  • num % 2 == 0
    even
  • num % 2 == 1
    odd
  • num % 4 == 0
    number is divisible by 4, else it is not.

Reverse a collection (Reverse Accumulator)

  • .reverse method
  • reverse the accumulator pattern
    • accumulator = item + accumulator
      • (instead of accumulator = accumulator + item
  • reverse the loop, e.g. range(10, 0, -1)

Index Shifting with accounting for end of list needing to loop back around to beginning

  • modulo operator
  • desired_index_position % len(container)
    • if index is less than len, it will return that index, if it is 1 above the index, it will be equal to the len of the container, % returning 0, thus looping back around and the next index up will return itself.
  • e.g., elementary alphabet ciphers
Design Pattern
Interactive graph
On this page
Design Pattern
Book: Design Patterns by the Gang of Four
Book: Head First Design Patterns
Observer Pattern
Structural Design Patterns
Data Mapper Pattern
Creational Design Patterns
The Accumulator Pattern
Modulo Operator to count nth element
Null Object Design Pattern
Factory Pattern
Behavioral Pattern
Pattern
Find Odd/Even Number & num divisible by x
Reverse a collection (Reverse Accumulator)
Index Shifting with accounting for end of list needing to loop back around to beginning