Permalink
Browse files

Now that the general structure is in place, I have added comments.

  • Loading branch information...
1 parent 898c4e7 commit 9ffbe891d11ae29407f510087246cc343158628a @bennadel committed May 7, 2012
View
@@ -7,7 +7,7 @@ if (!Object.hasOwnProperty( "create" )){
var Connector = function(){};
- Connector.prototype = superClass();
+ Connector.prototype = superClass;
return( new Connector() );
@@ -35,18 +35,30 @@ require(
],
function( Factory ){
+ // Create an instance of our factory. The factory is
+ // responsible for creating new instances of the Person
+ // class with a certain amount of randomness.
var factory = new Factory();
+ // Create our match maker - this is the domain entity that
+ // will come up with pairings of people who may have a
+ // successful relationship.
var matchMaker = factory.createMatchMaker();
+ // Create a number of people and "introduce" them to the
+ // match maker.
for (var i = 0 ; i < 10 ; i++){
matchMaker.introduce( factory.createPerson() );
}
+ // Now that we have introduced a number of people to the
+ // match maker, ask the match maker to suggest matches that
+ // might lead to viable relationships.
var matches = matchMaker.suggestMatches();
+ // Output the suggested matches.
for (var i = 0 ; i < matches.size() ; i++){
console.log( matches.toString( i ) );
@@ -29,32 +29,40 @@ define(
){
+ // I provide a central point for "new" constructor calls.
function Factory(){
+ // Create a list of men's names.
this._maleNames = new RandomAccessCollection(
- "Arnold", "Brett", "Byron", "Collin", "Dan", "Elton", "Isaac",
- "James", "Logan", "Louis", "Phil", "Rex", "Robert", "Spencer",
- "Sly", "Stan", "Vin", "Willy", "Zack"
+ "Arnold", "Brett", "Byron", "Collin", "Dan", "Elton",
+ "Isaac", "James", "Logan", "Louis", "Phil", "Rex",
+ "Robert", "Spencer", "Sly", "Stan", "Vin", "Willy",
+ "Zack"
);
+ // Create a list of women's names.
this._femaleNames = new RandomAccessCollection(
- "Adeline", "Alyson", "Amy", "Betsy", "Christina", "Hillary",
- "Joanna", "Kim", "Krysten", "Mallory", "Nicole", "Rebecka",
- "Sally", "Sarah", "Stacy", "Tricia", "Vanessa"
+ "Adeline", "Alyson", "Amy", "Betsy", "Christina",
+ "Hillary", "Joanna", "Kim", "Krysten", "Mallory",
+ "Nicole", "Rebecka", "Sally", "Sarah", "Stacy",
+ "Tricia", "Vanessa"
);
}
+ // Define the instance methods.
Factory.prototype = {
+ // I create the blackbook used to collect people.
createBlackbook: function(){
return( new Blackbook() );
},
+ // I create the match maker.
createMatchMaker: function(){
return(
@@ -64,8 +72,10 @@ define(
},
+ // I create a person with random characteristics.
createPerson: function(){
+ // Distribute male/female creation.
if (Math.random() > .5){
var name = this.getRandomMaleName();
@@ -86,6 +96,7 @@ define(
},
+ // I create a random gender preference.
createPreference: function(){
var random = Math.random();
@@ -107,6 +118,7 @@ define(
},
+ // I create a random female Name instance.
getRandomFemaleName: function(){
return(
@@ -116,6 +128,7 @@ define(
},
+ // I create a random male Name instance.
getRandomMaleName: function(){
return(
@@ -6,6 +6,8 @@ define(
],
function( Collection ){
+
+ // I represent a collection of single people
function Blackbook(){
Collection.apply( this, arguments );
@@ -24,5 +26,6 @@ define(
// Return constructor definition.
return( Blackbook );
+
}
-);
+);
@@ -5,6 +5,7 @@ define(
function(){
+ // I represent a possible match between two people.
function Match( person1, person2 ){
this._person1 = person1;
@@ -13,8 +14,12 @@ define(
}
+ // Define the instance methods.
Match.prototype = {
+ // I determine if this match equals the given match
+ // based on the people involved. The direciton of the
+ // relationship does not matter.
equals: function( match ){
return(
@@ -32,22 +37,30 @@ define(
},
+ // I return the first person in the match.
getPerson1: function(){
return( this._person1 );
},
+ // I return the second person in the match.
getPerson2: function(){
return( this._person2 );
},
-
+
+ // I determine if the match bewteen these two people might
+ // be promising. That is, could it lead to a successful
+ // relationship. This is baseue purely on potential
+ // attraction, not actual personality.
isPromising: function(){
+ // This match is promising if both people are
+ // generally attracted to each other's type.
return(
this._person1.isAttractedTo( this._person2 ) &&
this._person2.isAttractedTo( this._person1 )
@@ -56,6 +69,7 @@ define(
},
+ // I return the string representation.
toString: function(){
return(
@@ -79,4 +93,4 @@ define(
}
-);
+);
@@ -7,18 +7,30 @@ define(
function( Collection ){
+ // I represent a collection of Matches.
function MatchCollection(){
Collection.apply( this, arguments )
}
+ // Extend the core Collection class.
MatchCollection.prototype = Object.create( Collection.prototype );
+ // I deterine if the given match already exists in the current
+ // collection - the order of the people within the match is
+ // not taken into account.
MatchCollection.prototype.exists = function( match ){
+ // Loop over each item to check equality.
+ //
+ // NOTE: I know that this approach violates Rule #1 - one
+ // level of indentation per method; however, as this is a
+ // class specifically for collections, I find it hard to
+ // perform logic across the collection without an extra
+ // level of indentation.
for (var i = 0 ; i < this.size() ; i++){
if (this.get( i ).equals( match )){
@@ -29,11 +41,14 @@ define(
}
+ // If none matched, the given match doesn't exist.
return( false );
};
+ // I add the given match to the collection; but, I only add
+ // it if it didn't already exist.
MatchCollection.prototype.push = function( match ){
if (this.exists( match )){
@@ -47,6 +62,8 @@ define(
};
+ // I remove any matches from the collection that don't appear
+ // to foster a possible relationship (based on attraction).
MatchCollection.prototype.removeBadMatches = function(){
for (var i = this._values.length - 1 ; i >= 0 ; i--){
@@ -58,6 +75,8 @@ define(
};
+ // I remove the match at the given index if it doesn't look
+ // to foster a possible relationship.
MatchCollection.prototype.removeMatchIfBad = function( index ){
if (!this._values[ index ].isPromising()){
@@ -69,6 +88,8 @@ define(
};
+ // I return the string representation of the match at the
+ // given index.
MatchCollection.prototype.toString = function( index ){
return(
@@ -87,4 +108,4 @@ define(
}
-);
+);
@@ -8,24 +8,34 @@ define(
function( MatchCollection, Match ){
+ // I represent the match maker that will provide logic for
+ // aggregating and finding possible relationships between
+ // single people.
function MatchMaker( blackbook ){
this._blackbook = blackbook;
}
+ // Define the instance methods.
MatchMaker.prototype = {
+ // Intoduce a single person to the match maker.
introduce: function( person ){
this._blackbook.push( person );
},
+ // I gather all the possible matches for the given
+ // person based on combination (cross-product) rather
+ // than attraction.
gatherMatches: function( person, matches ){
+ // Gather all the people in the Blackbook who are not
+ // the given person (ie. all possible combinations).
for (var i = 0 ; i < this._blackbook.size() ; i++){
var possibleMatch = this._blackbook.get( i );
@@ -43,10 +53,13 @@ define(
},
+ // I suggest all possible matches for all people that
+ // have been introduced to the match maker.
suggestMatches: function(){
var matches = new MatchCollection();
+ // Gather all possible combinations.
for (var i = 0 ; i < this._blackbook.size() ; i++){
this.gatherMatches(
@@ -56,6 +69,8 @@ define(
}
+ // Remove any combinations that don't seem likely
+ // to lead to a relationship.
matches.removeBadMatches();
return( matches );
@@ -74,4 +89,4 @@ define(
}
-);
+);
Oops, something went wrong.

0 comments on commit 9ffbe89

Please sign in to comment.