-
Notifications
You must be signed in to change notification settings - Fork 11
Нижегородов Антон #9
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: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,20 @@ | |
| * Когда присваивается корректный e-mail в консоль выводится сообщение email valid. | ||
| * Когда присваивается некорректный e-mail возбуждается ошибка. | ||
| */ | ||
| function validDecorator(target: Object, propertyKey: string):any{ | ||
| let descriptor:PropertyDescriptor = { | ||
| set(email : string) { | ||
| if (email.match(/^([\w.*-]+@([\w-]+\.)+[\w-]{2,4})?$/g)){ | ||
| console.log("email valid"); | ||
| } | ||
| else throw "Invalid" | ||
| } | ||
| } | ||
| return descriptor; | ||
| } | ||
|
|
||
| class Example { | ||
| class Example{ | ||
| @validDecorator | ||
| public email: string = ""; | ||
| } | ||
|
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. 3 бала |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,28 +12,42 @@ | |
| abstract class Control<T> { | ||
| public name: string = ""; | ||
|
|
||
| protected value: T; | ||
| protected value: T | undefined; | ||
|
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. Зачем используешь объединение типов? |
||
| /**взять значение из контрола */ | ||
| public abstract getValue(): T; | ||
| public abstract getValue(): T | undefined; | ||
| /**установить значение в контрол */ | ||
| public abstract setValue(val: T): void; | ||
| } | ||
| /**Класс описывает TextBox контрол */ | ||
| class TextBox extends Control<string> { | ||
| protected val: string | undefined | ||
|
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. зачем переопределил объект val? |
||
| public getValue(): string | undefined{ | ||
| return this.val; | ||
| } | ||
| public setValue(val: string): void{ | ||
| this.val = val; | ||
| } | ||
| } | ||
| /**value контрола selectBox */ | ||
| class SelectItem { | ||
| public value: string; | ||
| public id: number; | ||
| public value: string | undefined; | ||
| public id: number | undefined; | ||
| } | ||
|
|
||
| /**Класс описывает SelectBox контрол */ | ||
| class SelectBox extends Control<SelectItem> { | ||
| protected val: SelectItem | undefined; | ||
|
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. зачем поле в котором хранится значение ? |
||
| public getValue(): SelectItem | undefined{ | ||
| return this.val; | ||
| } | ||
| public setValue(val: SelectItem): void{ | ||
| this.val = val; | ||
| } | ||
| } | ||
|
|
||
| class Container { | ||
| public instance: Control<any>; | ||
| public type: string; | ||
| public type: string | undefined; | ||
| } | ||
|
|
||
| /**Фабрика которая отвечает за создание экземпляров контролов */ | ||
|
|
@@ -45,10 +59,24 @@ class FactoryControl { | |
| this._collection = []; | ||
| } | ||
|
|
||
| public register<?>(type: ?) { | ||
| public register<T extends Control<any>>(type: new () => T){ | ||
| const typeInstance: T = new type(); | ||
| if (this.existType(typeInstance.name)) { | ||
| throw new Error("Already exists") | ||
| } | ||
| if (!(typeInstance instanceof Control)){ | ||
| throw new Error("Invalid type"); | ||
|
|
||
|
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. Нет регистрации типа и созданного объекта. Работать не будет. |
||
| } | ||
| } | ||
|
|
||
| public getInstance<?>(type: ?): ? { | ||
| public getInstance<T>(type: new () => Control<T>): Control<T>{ | ||
| const typeInstance: Control<T> = new type(); | ||
| const instances = this._collection.filter(g => g.type === typeInstance.name); | ||
| if (instances.length > 0) { | ||
| return instances[0].instance; | ||
| } | ||
| throw Error("Instance not found"); | ||
| } | ||
|
|
||
| private existType(type: string) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,6 @@ function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { | |
| return obj[key]; | ||
| } | ||
|
|
||
| const x = undefined; | ||
| const x = { "m": "Ы" }; | ||
|
|
||
|
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. 3 бала |
||
| console.log(getProperty(x, "m")); | ||
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.
Это не патерн декоратор
https://refactoring.guru/ru/design-patterns/decorator/typescript/example