Giter Site home page Giter Site logo

itmani1991 / design-patterns-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from metova/design-patterns-sample

0.0 2.0 0.0 2.27 MB

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.

Java 23.40% Ruby 30.05% JavaScript 0.81% CoffeeScript 0.26% CSS 2.31% HTML 7.91% Swift 26.44% Objective-C 8.84%

design-patterns-sample's Introduction

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

design-patterns-sample's People

Contributors

lgauthier avatar mahyarv avatar meatherly avatar

Watchers

James Cloos avatar Manivannan M avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.