Design Patterns

Design Patterns: What is a Design Pattern, Factory Method, Iterator.

Updated

  1. What is a Design Pattern (Apprentice): A design pattern is a proven, named answer to a problem that keeps coming up in software design. Patterns are grouped as creational, structural and behavioral, and you've already used several without knowing their names: Singleton, Iterator, Observer, Dependency Injection and the DAO.
  2. Factory Method (Journeyman): A factory method makes an object for you, so the code that uses it doesn't name the class it gets. A simple factory picks the class in one switch, instead of in every caller; the Gang of Four's Factory Method lets each subclass decide what to create. ASP.NET Core's registrations with a lambda are factories too.
  3. Iterator (Journeyman): The Iterator pattern lets code walk through a collection without knowing how it's stored. In C#, foreach works on anything that implements IEnumerable<T>, which hands out an IEnumerator<T> with MoveNext and Current. Write one by hand to see how it works, then let yield return write it for you.