stcalica / design-patterns-sample

Code samples for our presentation at VandyHacks 2015. This repository demonstrates how a handful of design patterns can be applied and used in iOS, Android, and Rails.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview

The goal of this repository is to demonstrate use cases for design patterns and help solidify your understanding with simple code examples.

If you are a web or mobile developer you may not be aware that you use some of these patterns every day. This repository will demonstrate how to apply design patterns to the iOS, Android, and Rails frameworks and give you a deeper understanding of when and how to use these patterns.

Design Patterns

The following list is a compilation of design patterns that we have explanations and code examples for.

Singleton

Reference: Singleton Pattern

Intent

  • Ensure a class has only one instance, and provide a global point of access to it.
  • Encapsulated "just-in-time initialization" or "initialization on first use".

Problem

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

Examples

Factory

Intent

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
  • Defining a "virtual" constructor.
  • The new operator considered harmful.

Problem

A framework needs to standardize the architectural model for a range of applications, but allow for individual applications to define their own domain objects and provide for their instantiation.

Examples

RestaurantFactory

Adapter

References:

Intent

  • Convert the interface of a class into another interface clients expect.
  • Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface. Impedance match an old component to a new system

Problem

An "off the shelf" component offers compelling functionality that you would like to reuse, but its "view of the world" is not compatible with the philosophy and architecture of the system currently being developed.

Examples

Flyweight

References:

Intent

  • Use sharing to support large numbers of fine-grained objects efficiently.
  • The Motif GUI strategy of replacing heavy-weight widgets with light-weight gadgets.

Problem

Designing objects down to the lowest levels of system "granularity" provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage.

Examples

ViewHolder - Android

Observer

Intent

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy.
  • The "View" part of Model-View-Controller.

Problem

A large monolithic design does not scale well as new graphing or monitoring requirements are levied.

Examples

NSNotificationCenter - iOS

Delegation

Reference: Delegation Pattern

Intent

  • Allow an object to act on behalf of, or in coordination with, another object.
  • Clients can customize behavior of another object while remaining fairly decoupled.

Problem

You want to customize the behavior of an object when inheritance is either not feasible, or inheritance doesn't allow an object to maintain a separation of concerns.

Examples

UITableView - iOS

About

Code samples for our presentation at VandyHacks 2015. This repository demonstrates how a handful of design patterns can be applied and used in iOS, Android, and Rails.


Languages

Language:Ruby 30.0%Language:Swift 26.4%Language:Java 23.4%Language:Objective-C 8.8%Language:HTML 7.9%Language:CSS 2.3%Language:JavaScript 0.8%Language:CoffeeScript 0.3%