How can the Factory design pattern be employed in designing a system similar to Amazon shopping?

218 views Asked by At

I have been reading system design of an amazon shopping website like system on educative, it is mentioned in the solution that

In the Amazon online shopping system, we can use the Factory design pattern to maintain different orders and the bill generation process based on the products selected by a customer.

Can anyone help me understand what the above statement means exactly based on the below class diagram ?

Class Diagram:

enter image description here

Let me know if you need more details

Update: I asked the above question to the educative course authors and below is their response, adding this so that it may be useful for future readers

The provided code may not explicitly use the Factory design pattern, it demonstrates sound object-oriented principles and can serve as a foundational basis for handling different payment methods in a system.

We agree with what you’re saying. However, the code not implementing the said design patterns doesn’t necessarily indicate a problem. We have various strategies and design patterns available, such as Factory pattern and Abstract Factory pattern, which can be effective in specific situations. The choice of a design pattern depends on factors like project complexity, maintainability, and timelines.

In this code, simplicity has been prioritized, leading to the absence of explicit design patterns. It’s important to note that our design solutions may not always include all mentioned design patterns. We provide foundational knowledge and encourage learners to explore and adapt design patterns to suit their needs. Different problems may require different approaches, and we support flexibility in finding solutions that align with individual preferences and project requirements.

Use cases: Factory Design Pattern In the context of an online shopping system like Amazon, here’s a possible way the Factory pattern could be applied:

Product Factory: Amazon could have a ProductFactory responsible for creating instances of different products. This factory might take product identifiers or types as input and return the corresponding product objects. This abstraction can help in managing the creation of various product types.

Order Factory: For managing different orders, an OrderFactory could be used to create instances of orders. It might take customer information, selected products, and other order-related details as input and return an order object. This abstracts the order creation process.

Bill Generation: When it comes to generating bills, the Factory pattern might not be directly involved. However, it’s possible that the OrderFactory, when creating an order, internally calculates the bill based on the selected products, discounts, and other factors. This could be seen as a form of factory-like behavior.

1

There are 1 answers

0
AndrewR On

To set the scene (copy and paste from wikipedia): factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created

On the diagram I see no hierarchy (classes extend others) for orders, but I see a hierarchy for Payment. Payment has three specifics: credit cards, etransfer and cash.

By definition, Factory pattern could help with payment. Basically, Factory pattern would produce a right Payment based on various parameters.

p.s. As for the order, I would prefer to have a Builder pattern; as that will simplify the construction process, by having methods like addToOrder and removeFromOrder; and final build method would produce a fully constructed, immutable (that's the main point) Order