Jump to content

implementing polymorphism and inheritance


bernardnthiwa

Recommended Posts

Hello, I'm configuring php api endpoints for crud operations with a React.JS frontend. I would like help on how to implement the logic with OOP, an abstract class for the main product logic. I have three product types, Book, Furniture, and DVD. All products have a name, sku (primary key), and price. Moreover, each product type has a product-specific value. A book has a weight property, furniture has dimensions (length, width, and height) and a DVD has a size property.

How can I proceed to use polymorphism to achieve this, so that i can avoid using conditional statements to detect product type.

 

Link to comment
Share on other sites

On 2/22/2023 at 9:56 AM, bernardnthiwa said:

Hello, I'm configuring php api endpoints for crud operations with a React.JS frontend. I would like help on how to implement the logic with OOP, an abstract class for the main product logic. I have three product types, Book, Furniture, and DVD. All products have a name, sku (primary key), and price. Moreover, each product type has a product-specific value. A book has a weight property, furniture has dimensions (length, width, and height) and a DVD has a size property.

How can I proceed to use polymorphism to achieve this, so that i can avoid using conditional statements to detect product type.

 

I assume you have a database behind this.  How have you solved it with database design?  Typically this would be "subtyping".  So polymorphism is not involved.  What is involved would be inheritance.  

But I'd start with your assumption here.  You basically have a list of "Finished Goods".  All of them have dimensions and weight.  Is this academic, because your original supposition is arbitrary and incorrect?   

Rather, it seems that you want to associate one or more product categories or tags to the finished goods in this list.  Let's assume you had a product_type table that included a primary key and a name.  That table would have in it:  'Book', 'DVD', and 'Furniture' and likely many more. You'd classify the product by having a foreign key for this table in the product table.

Now beyond this, there could be any of a million different facts that relate to a product.  How do you think an ERP or Amazon solves this problem?  Do you think they formulate a bunch of structure specifically for each type before it can be sold?

One answer is: use properties.  In the database consider how you might allow for One-to-many properties such that they could be assigned to a product.  When you need something generic and extensible, you often have to design something generic and extensible.

Another answer is to use a structure that is intrinsically hierarchical, like json, and allow each product to store data in json format.  This can then alleviate the problem with the rigidity of relational databases, in cases where you might need some data that isn't always required, and also isn't always structured exactly the same way.

Some databases have json types, and then there are "Document" databases like Mongodb, which is built upon a version of json, and essentially uses json as its internal structure.  From document to document in a Mongodb collection, the json structure can vary.  So you could have a basic structure, and then a section that completely varies by product type.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.