Skip to content

Instantly share code, notes, and snippets.

Strategy Pattern

The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.

The Strategy Pattern consists of strategies, which are the interchangeable classes which encapsulate variants of a particular algorithm, and the context class, which utilizes these strategies. The context can choose different strategies at runtime depending on the situation.

Here is an example implementation of the Strategy Pattern:

# SSL self signed localhost for rails start to finish, no red warnings.
# 1) Create your private key (any password will do, we remove it below)
$ openssl genrsa -des3 -out server.orig.key 2048
# 2) Remove the password
$ openssl rsa -in server.orig.key -out server.key
@abrahamsangha
abrahamsangha / gist:6241947
Last active December 21, 2015 03:28
Notes on API Design

From http://pages.apigee.com/rs/apigee/images/api-design-ebook-2012-03.pdf

Routes

  • 2 base urls per resource ("/dogs" and "/dogs/1234")
  • Keep verbs out of base URLs -HTTP verbs to operate on collections and elements -Shouldn't need URL deeper than /resource/identifier/resource -Sweep parameters under HTTP ? ("GET /dogs?color=red&state=running&location=park")
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">
[{"id":1,"name":"John","created_at":"2013-08-14T20:55:47Z","updated_at":"2013-08-14T20:55:47Z"},{"id":2,"name":"Samantha","created_at":"2013-08-14T20:56:40Z","updated_at":"2013-08-14T20:56:40Z"}]
</pre>
</body>
</html>
function Cookie(type, bakeTime) {
this.type = type,
this.bakeTime = bakeTime,
this.timeBaked = 0,
this.status = "Raw"
};
Cookie.prototype = {
updateStatus: function(){
@abrahamsangha
abrahamsangha / gist:6134215
Created August 1, 2013 19:03
Git from CL
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/1neway/quotations.git
git push -u origin master
Push an existing repository from the command line
/* Here is your chance to take over Socrates!
Spend 10 minutes on each of the following hacks to the socrates website.
Enter them in the console to make sure it works and then save
your results here.
Choose a new pair for each. Add your names to the section you complete.
*/
@abrahamsangha
abrahamsangha / zoo.js
Last active December 20, 2015 05:49 — forked from dbc-challenges/zoo.js
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
function Animal(kind,legs) {
this.kind = kind;
this.legs = legs;
};
var Zoo = {
@abrahamsangha
abrahamsangha / index.html
Last active December 20, 2015 05:39 — forked from dbc-challenges/index.html
DBC Phase 2 Practice Assessment Part 3
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://cdn.jsdelivr.net/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lato:100,900">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/3.0.2/css/font-awesome.min.css">
</head>

Instructions:

  1. Download this application skeleton.
  2. Convert the app to use AJAX.
  3. Add any files you changed to your gist and submit your code.