Skip to content

Instantly share code, notes, and snippets.

@briantical
Last active June 10, 2020 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briantical/736e1cec6abcdf3b412eb10e549f7155 to your computer and use it in GitHub Desktop.
Save briantical/736e1cec6abcdf3b412eb10e549f7155 to your computer and use it in GitHub Desktop.
Implementing the GRASP design patterns in JavaScript
//These are the group members
LUTAAYA BRIAN IVAN
MIKE REMBO
PATIENCE AKATUKUNDA MUHABUZI
TIMOTHY SSERUNJOGI
// Farm Manager for Mangos and Bananas
//
var bananas = 0;
var mangoes = 0;
var areBananasHarvestable = true;
var areMangoesHarvestable = true;
var year = 2020;
function harvestBananas() {
return Math.floor(Math.random() * (20 - 10) + 10);
}
function harvestMangoes() {
return Math.floor(Math.random() * (30 - 10) + 10);
}
function messWithHarvest() {
bananas = 1;
mangoes = 1;
}
function main() {
while (year <= 2030) {
if (areBananasHarvestable === true) {
bananas += harvestBananas();
}
if (areMangoesHarvestable === true) {
mangoes += harvestMangoes();
}
console.log(`${year} Bananas: ${bananas}, Mangoes: ${mangoes}`);
year++;
}
}
main();
// Farm Manager for Mangos and Bananas
//
class Fruit {
#name // private variables in class
#count
constructor(aName = "Fruit") {
this.#name = aName;
this.#count = 0;
}
get count(){ // getter
return this.#count; // JS needs this.
}
// But no setter for count so truely private
harvestable() {
return true;
}
harvest(min, max) {
return this.#count += Math.floor(Math.random() * (max - min) + min);
}
}
class Farm {
constructor(){
this.banana = new Fruit("Bananas");
this.mango = new Fruit("Mangoes");
// avacado = new Fruit("Avacado");
}
harvest(){
if (this.banana.harvestable() === true) {
this.banana.harvest(10, 20);
}
if (this.mango.harvestable() === true) {
this.mango.harvest(10, 30);
}
return { // create an object just to return
bananas: this.banana.count,
mangoes: this.mango.count
}
}
}
function main() {
let year = 2020;
const farm = new Farm(year);
while (year <= 2030) {
const harvestResults = farm.harvest();
console.log(`${year} Bananas: ${harvestResults.bananas}, Mangoes: ${harvestResults.mangoes}`);
year++;
}
}
main();
/**
* LUTAAYA BRIAN IVAN
* MIKE REMBO
* PATIENCE AKATUKUNDA MUHABUZI
* TIMOTHY SSERUNJOGI
*/
/**
* In this version these are changes and considerations i made:
* 1. The year paramter is not needed when creating the Farm.
* 2. We need to change the farm to handle all sorts of crops and not just fruits
* 3. The farm constructor should accept any object as long as it has the count, name properties,
* the harvestable and harvest methods.
*
* By the end of this,
* Farm is nolonger reliant on the fruit class(hence low coupling)
* The main function become the Information Expert, and creator
*/
// Farm Manager for Mangos and Bananas
class Fruit {
#name; // private variables in class
#count;
#min;
#max;
constructor(aName = 'Fruit', min, max) {
this.#name = aName;
this.#count = 0;
this.#max = max;
this.#min = min;
}
// count getter
// But no setter for count so truely private
get count() {
return this.#count; // JS needs this.
}
// name getter
// But no setter for count so truely private
get name() {
return this.#name; // JS needs this.
}
harvestable() {
return true;
}
harvest() {
return (this.#count += Math.floor(Math.random() * (this.#max - this.#min) + this.#min));
}
}
// Handles any kind of harvesting
// the crops paramter should be an array of crops that
// have count, name properties , harvest and harvestable methods
function harvester(crops) {
let cropharvest = [];
crops.map((crop) => {
if (crop.harvestable() === true) {
crop.harvest();
}
let { count, name } = crop;
cropharvest = [...cropharvest, { count, name }];
});
return cropharvest;
}
class Farm {
constructor(crops) {
this.crops = crops;
}
// Delegate the harvesting function to an expert
harvest = () => harvester(this.crops);
}
function main() {
let year = 2020;
// create many crop options
const bananas = new Fruit('Bananas', 10, 20);
const mangoes = new Fruit('Mangoes', 10, 30);
// const cabbage = new Vegetable("Cabbage")
// Create a new farm that produces numerous crops
const farm = new Farm([bananas, mangoes]);
// Harvest all the harvestable crops on the farm for every year
while (year <= 2030) {
const harvestedcrops = farm.harvest();
console.log(`Year : ${year}`);
harvestedcrops.map((harvestedcrop) => {
console.log(`${year} ${harvestedcrop.name} : ${harvestedcrop.count}`);
});
console.log(`================================================================`);
year++;
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment