Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oop exercise #126

Merged
merged 8 commits into from
Aug 26, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added oop exercise
  • Loading branch information
OlStani committed Aug 11, 2022
commit 4292da2517d5269a5f44c59dc3b654d867e6502f
42 changes: 42 additions & 0 deletions submissions/OlStaniev/OOP exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Inhabitants {
constructor(name, gender, legs, saying) {
this.name = name;
this.gender = gender;
this.legs = legs;
this.saying = saying
}

prepareToPrint() {
return [this.name, this.gender, this.legs, this.saying].join(';')
}
}

class People extends Inhabitants {
constructor(name, gender, legs, hands, saying) {
super(name, gender, legs, saying)
this.hands = hands
}
prepareToPrint() {
return [this.name, this.gender, this.legs, this.hands, this.saying].join(';')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is way to re-use parent class method. It makes sense as this helps to follow SOLID principles. Check the supplementary materials on the task's README.

}
}

class PeopleWithFriends extends People {
constructor(name, gender, legs, hands, saying, friends) {
super(name, gender, legs, hands, saying)
this.friends = friends
}
prepareToPrint() {
return [this.name, this.gender, this.legs, this.hands, this.saying, this.friends].join(';')
}
}

const dog = new Inhabitants('Bob', 'male', 4, 'woof!')
const cat = new Inhabitants('Kitty', 'female', 4, 'meow!')
const woman = new People('Sara', 'female', 2, 2, 'Hello!')
const catWoman = new People('Bella', 'female', 2, 2, `${cat.saying}`)
const man = new PeopleWithFriends('Mario', 'male', 2, 2, 'Hi!', ['Bob', 'Tom', 'Eva'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention class name should be in singular form unless it holds multiple entitites indeed. new People that returns single human reads a bit weird as there is no option to create multiple humans in a single call of new People.

No need to have People and PeopleWithFriends. This can be resolved by having default friends = [] in a class constructor signature and relevant logic in prepareToPrint method. Even if friends has no default value and assigned with none, it's value gonna be undefined, which is also quite handleable.

Imagine we want to specify neighbours, pets etc (and any such list may be empty or undefined). This should be handled by a single class.


const inhabitants = [cat, dog, woman, catWoman, man]

inhabitants.forEach(inhabitant => print(inhabitant.prepareToPrint()))