-
Notifications
You must be signed in to change notification settings - Fork 19
Pattern solutions #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
73507b7
f0de113
b60f116
c47bd93
af892ce
73d3dc6
d6c66eb
db76e0b
5faf2ab
a641a5e
66ebe82
8c6c9bc
92911d2
e12172e
ff793c0
30868d5
5974926
50a5445
2514244
bc726df
7e9bfbf
19721ab
363bd47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,36 @@ | ||
| class Monster { | ||
| // todo: implement logic | ||
| constructor(settings) { | ||
| this.settings = settings; | ||
| } | ||
| } | ||
|
|
||
| export default class Location { | ||
| monsters = []; | ||
|
|
||
| constructor(name = '', monstersCount = 0, monstersSettings = {}) { | ||
| this.name = name; | ||
| // todo: implement logic | ||
| this.monstersCount = monstersCount; | ||
| this.monstersSettings = monstersSettings; | ||
| } | ||
|
|
||
| get monstersSettings() { | ||
| return this._settings; | ||
| } | ||
|
|
||
| set monstersSettings(value) { | ||
| this._settings = value; | ||
| this.initMonsters(); | ||
| } | ||
|
|
||
| initMonsters () { | ||
| // todo: implement logic | ||
| const newMonsters = []; | ||
| for(let i = 0; i < this.monstersCount; i++) { | ||
| newMonsters.push(new Monster(this._settings)); | ||
| } | ||
| this.monsters = newMonsters; | ||
| } | ||
|
|
||
| clone () { | ||
| // todo: implement logic | ||
| return new Location(this.name, this.monstersCount, { ...this._settings }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| export default class Singleton { | ||
| static instance; | ||
|
|
||
| constructor() { | ||
| // todo: implement logic | ||
| if (Singleton.instance) { | ||
| return Singleton.instance; | ||
| } | ||
|
|
||
| Singleton.instance = this; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,26 @@ | ||
| export class Milk { | ||
| price = 2; | ||
|
|
||
| // todo: add implementation | ||
| constructor (extra) { | ||
| this.extra = extra; | ||
| } | ||
|
|
||
| getPrice () { | ||
| return this.extra ? this.extra.getPrice() + this.price : this.price; | ||
| } | ||
| } | ||
|
|
||
| export class Sugar { | ||
| price = 1; | ||
|
|
||
| // todo: add implementation | ||
| // Смущает что получилось два одинаковых решения для Milk и для Sugar | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. в этом задании это ок |
||
| constructor (extra) { | ||
| this.extra = extra; | ||
| } | ||
|
|
||
| getPrice () { | ||
| return this.extra ? this.extra.getPrice() + this.price : this.price; | ||
| } | ||
| } | ||
|
|
||
| export class Coffee { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,14 @@ | ||
| export class Game { | ||
| constructor() { | ||
| this.hero = new Hero(); | ||
| this.location = new Location(); | ||
| } | ||
| start () { | ||
| // todo: add implementation | ||
| this.hero.name = 'Barbarian'; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше через конструктов, тк там есть аткая возможность |
||
| this.location.name = 'darkForest'; | ||
| this.location.addMonster(new Monster('demon')); | ||
| this.location.addMonster(new Monster('demon')); | ||
| this.location.addMonster(new Monster('undead')); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,14 +10,19 @@ export class User { | |
| } | ||
|
|
||
| export class ProxyUser { | ||
| // Не понимаю зачем здесь это свойство | ||
| rights = []; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. это моя опечатка |
||
|
|
||
| constructor() { | ||
| // todo: add implementation | ||
| constructor(user) { | ||
| this.user = user; | ||
| } | ||
|
|
||
| write () { | ||
| // todo: add implementation | ||
| if (this.user.rights.includes('admin')) { | ||
| return this.user.write(); | ||
| } | ||
|
|
||
| return "user does not have permissions to write"; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,29 @@ | ||
| class Handler { | ||
| name = ''; | ||
| regExp = /./; | ||
| nextHandler; | ||
|
|
||
| setNext (handler) { | ||
| // todo: implement | ||
| this.nextHandler = handler; | ||
| return this.nextHandler; | ||
| } | ||
|
|
||
| next (data) { | ||
| // todo: implement | ||
| if (this.nextHandler) { | ||
| return this.nextHandler?.validate(data); | ||
| } | ||
| } | ||
|
|
||
| validate (data) { | ||
| // todo: implement | ||
| if (this.regExp.test(data)) { | ||
| /* | ||
| Непонятно для чего нужно свойство next, если можно записать короче: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. для единообразия работы с элементами цепочки |
||
| return this.nextHandler?.validate(data); | ||
| */ | ||
| return this.next(data); | ||
| } | ||
|
|
||
| return `Validation rule \"${this.name}\" didn\'t pass for string \"${data}\"`; | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
такое проще через switch case делать