Last active
August 29, 2015 14:23
-
-
Save OriginUnknown/230ee9e2a3b6327c6ae2 to your computer and use it in GitHub Desktop.
JavaScript OOP Design Pattern - Composite Pattern v1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Composite pattern</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
/* | |
* * Structure of my music collection via the composite pattern | |
* * -AllSongs | |
* * --Alternative | |
* * --Eple | |
* * --Galvanize | |
* * ----African | |
* * ------Waka waka | |
* * ------Asonto | |
* * ------Chop your dolla | |
* * --Trance | |
* * --Electronic | |
* * ----Dance | |
*/ | |
var _msg = ""; | |
//CompositePattern object - HAS children | |
var SongDir = function ( newGroupName ) { | |
var list = [], groupName = newGroupName; | |
var children = function () { | |
return list; | |
}, | |
hasChildren = function () { | |
return list.length > 0; | |
}, | |
add = function ( song ) { | |
list.push( song ); | |
}, | |
remove = function ( index ) { | |
list.splice( index, 1 ); | |
}, | |
getItemInSongGroup = function ( i ) { | |
return list[ i ]; | |
}, | |
displaySongInfo = function () {//displaySongInfo() method is the common "operation" method used by BOTH leaf and composite objects | |
return groupName; | |
}; | |
return { | |
"add": add, | |
"remove": remove, | |
"children": children, | |
"hasChildren": hasChildren, | |
"getChildAt": getItemInSongGroup, | |
"displaySongInfo": displaySongInfo | |
} | |
}; | |
//leaf object - HAS NO children | |
var Song = function ( newSongName ) { | |
var songName = newSongName || null; | |
return { | |
"displaySongInfo": function () { | |
return songName; | |
} | |
} | |
}; | |
//client | |
/* | |
* * 1. Declare the parent song directory that will house all of your music groups AND songs | |
*/ | |
var AllSongs = SongDir( "Root" ); | |
/* | |
* * 2. Declare your child directories (composite object set #1) | |
*/ | |
var Alternative = SongDir( "Alternative" ), | |
Trance = SongDir( "Trance" ), | |
Electronic = SongDir( "Electronic" ); | |
//Dance and African will be children (composite objects) of Alternative and Electronic | |
var Dance = SongDir( "Dance" ), | |
African = SongDir( "African" ); | |
/* | |
* * 3. Add your subset music groups accordingly | |
*/ | |
//added to Root dir - secondary level {children} | |
AllSongs.add( Alternative ); | |
AllSongs.add( Trance ); | |
AllSongs.add( Electronic ); | |
//tertiary level | |
Alternative.add( African ); | |
Electronic.add( Dance ); | |
/* | |
* * 4. Add some songs to each song directory | |
*/ | |
Alternative.add( Song( "Eple" ) ); | |
Alternative.add( Song( "Galvanize" ) ); | |
African.add( Song( "Waka waka" ) ); | |
African.add( Song( "Goldigga" ) ); | |
African.add( Song( "Asonto" ) ); | |
Trance.add( Song( "Wide Open Space" ) ); | |
Trance.add( Song( "Offshore" ) ); | |
Trance.add( Song( "Salva Mea" ) ); | |
Electronic.add( Song( "Didn't know I was looking for love" ) ); | |
Electronic.add( Song( "Insomnia" ) ); | |
Electronic.add( Song( "Interlude" ) ); | |
Dance.add( Song( "Groove is in the Heart" ) ); | |
Dance.add( Song( "Move Your Body" ) ); | |
Dance.add( Song( "Playing with Knives" ) ); | |
AllSongs.add( Song( "Wow" ) ); | |
AllSongs.add( Song( "Jamiroquai" ) ); | |
AllSongs.add( Song( "Animal Army" ) ); | |
function traverse ( indent, node ) { | |
_msg += Array( indent ++ ).join( "--" ) + node.displaySongInfo() + "\n"; | |
if ( node.hasOwnProperty( [ 'children' ] ) ) { | |
var i = 0, len = node.children().length; | |
for ( ; i < len; i ++ ) { | |
traverse( indent, node.children()[ i ] ); | |
} | |
} | |
} | |
traverse( 1, AllSongs ); | |
console.log( _msg ); | |
//View Trance song collection | |
console.log( AllSongs.getChildAt( 0 ).displaySongInfo() );//Returns "Alternative" | |
//Remove "Alternative" collection from the AllSongs collection. | |
//This will also remove all child collections respectively | |
AllSongs.remove( 0 ); | |
traverse( 1, AllSongs ); | |
console.log( _msg ); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment