1

my problem is that I want that Product class implements both classes [Review and Category] to get more scalability in my code. However, I got stuck in that. I tried to use even Mixins but I had to re-assign all methods from Category and Review, Does anyone knows a better and smart solution to do so?

interface Fetch {
    getInfo() :string;
}

class Review implements Fetch {

    getInfo(): string {
        return 'Nice Product'
    }

    getName(): string {
        return 'Shirt Star wars'
    }

    geRate(): string {
        return '5 stars'
    }
}

class Category implements Fetch {

    getInfo(): string {
        return 'Nice Category'
    }

    getCategory(): string {
        return 'Geek'
    }

    getSimilar(): string[] {
        return []
    }
}

class Product extends Review {
    constructor() {
        super();
    }
}

let Shirt = new Product()

Shirt.getInfo()
Shirt.geRate()
2
  • 3
    How about CategoryInterface and ReviewInterface, now implement them in Category and Review, while both in Product ? Commented Jun 10, 2018 at 23:43
  • Could give me an example to clarify? cheers Commented Jun 11, 2018 at 0:19

1 Answer 1

2

Here is an example of what you have asked:

interface Fetch {
    getInfo() :string;
}

interface ReviewInterface extends Fetch{

    getName(): string;

    getRate(): string;
}

interface CategoryInterface extends Fetch{


    getCategory(): string;

    getSimilar(): string[];
}

class Review implements ReviewInterface {

    getInfo(): string {
        return 'Nice Product'
    }

    getName(): string {
        return 'Shirt Star wars'
    }

    geRate(): string {
        return '5 stars'
    }
}

class Category implements CategoryInterface {

    getInfo(): string {
        return 'Nice Category'
    }

    getCategory(): string {
        return 'Geek'
    }

    getSimilar(): string[] {
        return []
    }
}

class Product implements ReviewInterface, CategoryInterface {
    constructor() {
        super();
    }

    // .... Here goes all implementations...
}

let Shirt = new Product()

Shirt.getInfo()
Shirt.geRate()
Sign up to request clarification or add additional context in comments.

4 Comments

Is this case inheritance over composition?
I mean that, composition over inheritance. hahaha
Right, make lot of sense this implementation and looks like mixins but my class Product must implement all interface methods. I mean, despite the relation between classes is not bad re-implement the same methods of Category class inside Product class again?
Is category to product 1 on 1 relation? If yes then this will be overkill. But AFA I think it should inherit Review not Category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.