Archive for the ‘Reference’ Category

simple git, yarn, react, mongo project checklist

Wednesday, December 13th, 2017

mkdir foo && cd $_

git init

yarn add react

vi yarn.lock (if you’d like to see versions)

git add package.json yarn.lock

echo “/node_modules” >> .gitignore

git add .gitignore

touch README

git add README

git remote add origin git@github.som:credentials/foobar.git

git commit -m “Initial commit”

git push -u origin master

git checkout -b develop

git push -u origin develop

git checkout -b feature/foo develop

vi foo.js

git add foo.js

git commit -m “New foo file”

git rebase develop (if we believe changes have been made to origin develop, will pull those and then apply our commits)

git checkout develop

git merge feature/foo

git push

git checkout -b release/1.0 develop

git push -u origin release/1.0

git checkout -b hotfix/1.0 develop

etc.

——————

example index connecting to mongo:

require(‘dotenv’).config();

var MongoClient = require(‘mongodb’).MongoClient;

MongoClient.connect(process.env.MONGO_URI, function(err, db) {

    if (err) {

        console.log(‘Cannot connect to MongoDB!’, err);

    } else {

        console.log(‘Connected to MongoDB!’);

    }

});

(put env vars in .env file)

vi .env

MONGO_URI = mongodb://localhost:27017/foo

yarn add mongodb

yarn add dotenv

(have mongo installed and started first!):

brew install mongodb —with-openssl

brew services start mongodb

Connected to MongoDB!”

Design Patterns Reference: Observer

Sunday, November 14th, 2010

The Observer Design Pattern facilitates the creation of objects that watch the state of a target object and provides state targeted functionality that is uncoupled from the core object.

The most obvious example, applications that support some type of plugin system.  Another example, cache systems, or message notification (example, members are notified when an item is in stock, to add that members may also be notified when it’s out of stock, simply add another observer, or perhaps another that texts them rather than emailing them…and so on).

When adding features to software that are activated by an action or state change but are loosely coupled, objects based on the Observer Design Pattern should be created.

MyObject is the Observable object.  It contains a protected array of observers. A public method addObserver() takes an instance of an observer and stores it in the array.

A public method doSomething() applies a state change to MyObject, whose notify() public method then loops through the array of observers.

Each such MyObjectObserver has a public method change(), that accepts an instance of MyObject, which modifies said object via a notify() method of MyObject that calls MyObjectObserver’s change() method directly as it’s encountered in the array of observers.

class CD
{
public $title = ”;
public $band = ”;
protected $_observers = array();

public function __construct($title, $band)
{
$this->title = $title;
$this->band = $band;
}

public function attachObserver($type, $observer)
{
$this->_observers[$type][] = $observer;
}

public function notifyObserver($type)
{
if(isset($this->_observers[$type])){
foreach($this->_observers[$type] as $observer)
{
$observer->update($this);
}
}
}

public function buy()
{
// stub actions of buying
$this->notifyObserver(‘purchased’);
}
}

class buyCDNotifyStreamObserver
{
public function update(CD $cd)
{
$activity = “The CD named {$cd->title} by “;
$activity .= “{$cd->band} was just purchased.”;
activityStream::addNewItem($activity);
}
}

class activityStream
{
public static function addNewItem($item)
{
// stub functions
print $item;
}
}

$title = “Wast of a Rib”;
$band = ‘Never Again’;
$cd = new CD($title, $band);

$observer = new buyCDNotifyStreamOberver();
$cd->attachObserver(‘purchased’, $observer);
$cd->buy();

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

memory jogger: Chain of responsibility, Polymorphism, Design Pattern reference, Decorator pattern, Singleton

Saturday, August 21st, 2010

Good stuff, take a moment to refresh and review:

Chain of responsibility:
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
Polymorphism:
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
JavaScript really OOP?:
http://articles.techrepublic.com.com/5100-10878_11-1044656.html
http://mckoss.com/jscript/object.htm
JS Prototype object:
http://www.javascriptkit.com/javatutors/proto4.shtml
Design Patterns reference/list:
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29
Decorator Pattern:
http://en.wikipedia.org/wiki/Decorator_pattern
Loose Coupling def:
http://en.wikipedia.org/wiki/Loose_coupling
Singleton Pattern:
http://en.wikipedia.org/wiki/Singleton_pattern