Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/traviswimer/patterns.js
https://github.com/traviswimer/patterns.js
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/traviswimer/patterns.js
- Owner: traviswimer
- License: other
- Created: 2013-06-25T00:46:06.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-06-25T22:50:10.000Z (over 11 years ago)
- Last Synced: 2024-11-06T21:14:05.157Z (about 2 months ago)
- Language: JavaScript
- Size: 145 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE.txt
Awesome Lists containing this project
README
Patterns.JS
===========A library to simplify the use of design patterns in JavaScript
# API Documentation #
## factory( defaultClass ) ##
### Parameters ###
* **defaultClass** - A function that serves as the base for all classes instantiated by the factory### Methods ###
* **build( extendingClass )** - builds a new factory
* **Parameters**
* extendingClass - a function that will inherit from defaultClass when created by the factory
* **Methods**
* create() - returns an extendingClass object that inherits from defaultClass### Example ###
```js
var animalBuilder = patterns.factory(function(){
this.sayHello = function(){
console.log("Hello, I am a " + this.type);
}
this.type = "mammal";
});var cat = function(){
this.type = "cat";
}
var catFactory = animalBuilder.build(cat);
catFactory.create().sayHello(); // "Hello, I am a cat"var dog = function(){
this.type = "dog";
}
var dogFactory = animalBuilder.build(dog);
dogFactory.create().sayHello(); // "Hello, I am a dog"animalBuilder.build().create().sayHello(); // "Hello, I am a mammal"
```## composite( leafClass ) ##
### Parameters ###
* **leafClass** - A function all leaf objects inherit from### Methods ###
* **addComposite()** - create a new composite as a child of the current composite
* **addLeaf()** - creates an object inheriting from leafClass as a child of the current composite
* *arguments passed to this method will be passed to the leafClass constructor*### Example ###
```js
var animalLeafClass = function(type){
this.sayHello = function(){
console.log("Hello, I am a " + type);
}
}var animalComposite = patterns.composite(animalLeafClass);
animalComposite.addLeaf("mammal");var catComposite = animalComposite.addComposite();
var dogComposite = animalComposite.addComposite();catComposite.addLeaf("Cat");
dogComposite.addLeaf("Dog");
catComposite.addLeaf("Kitten");
dogComposite.addLeaf("Puppy");animalComposite.sayHello();
/* Calling sayHello() a composite will call
sayHello() on each leaf beneath it as wellThis Prints:
Hello, I am a mammal
Hello, I am a Cat
Hello, I am a Kitten
Hello, I am a Dog
Hello, I am a Puppy
*/
```