Seven OOP principles of object-oriented (II)
Previous wrote the first four principles, this one continues ~~
Interface Isolation: The client should not rely on it does not interface; a dependency on another class of the class should be based on the smallest interface.
Is a big vernacular - that is to say as much as possible to refine the interface, interface methods should try less
/** * Oop interface to implement the principle of isolation * @Author gongliying * @date 2019-06-15 */ achieve(){ class cosmetics { constructor(name) { this.name = name } color() { console.log(this.name, "颜色") } efficacy() { console.log (this.name, "moisturizing") } size() { console.log(this.name, "粗细") } } class lipstick extends cosmetics {} class eyeliner extends cosmetics {} let kouhong = new lipstick ( "lipstick") kouhong.color () // lipstick color kouhong.efficacy () // lipstick moisturizing kouhong.size () // lipstick thickness let yanying = new eyeliner("眼影") yanying.color () // eyeshadow colors yanying.efficacy () // eyeshadow moisturizing yanying.size () // eyeshadow thickness },
I do indeed have achieved, but there is no special feeling emmmm redundant, but still a little logic seems right? ? ? Because there is no lipstick and eye shadow thickness ah I also have not heard of moisturizing ah, so there are some methods inherited when it becomes a bit redundant, the official word is - the client should not rely on his unwanted interfaces,
Following changes
/** * Oop interface to implement the principle of isolation * @Author gongliying * @date 2019-06-15 */ achieve(){ class cosmetics { constructor(name) { this.name = name } color() { console.log(this.name, "颜色") } } class lipstick extends cosmetics { efficacy() { console.log (this.name, "moisturizing") } } class eyeliner extends cosmetics { size() { console.log(this.name, "粗细") } } let kouhong = new lipstick ( "lipstick") kouhong.color () // lipstick color kouhong.efficacy () // moisturizing lipstick let yanying = new eyeliner("眼影") yanying.color () // eyeshadow colors yanying.size () // eyeshadow thickness },
This appears to be not very good, with a common approach, also has its own method, the interface will refine, improve flexibility (Mandarin), it does not need to rely on an interface, which is separated from the interface