0% found this document useful (0 votes)
25 views6 pages

Classtest CAM

The document discusses how design patterns improve the design of an ecommerce application. It describes using the decorator pattern to dynamically apply discounts to product prices without modifying product objects. It also uses the builder pattern to construct product instances in a way that separates object construction from client code and handles variability.

Uploaded by

Christopher Ami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Classtest CAM

The document discusses how design patterns improve the design of an ecommerce application. It describes using the decorator pattern to dynamically apply discounts to product prices without modifying product objects. It also uses the builder pattern to construct product instances in a way that separates object construction from client code and handles variability.

Uploaded by

Christopher Ami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

2023

Design pattern

CHRISTOPHER KERVIN AMI

UTM | Student Id:2303_25360, MSE/23A/PT


By referring to your assignment, discuss how design patterns improve the
design of your application.

For the ecommerce Eboutique website, I used the decorator design pattern on
displaying the product price discount. The idea is to decorate the product price with
discount decorators dynamically without affecting the product object.

The following improvements for this design pattern are:

1. Separation of Concerns: The Decorator pattern promotes the separation of


concerns by dividing the functionality into smaller, more focused classes. In
this case, the core component could be the product display, and the
decorators could handle various types of discounts example seasonal
discounts such as summer discount or winter discount. This separation makes
the source codes more readable and organized.

For example,
2. Open/Closed Principle: The Decorator pattern follows the Open/Closed
Principle, which states that software entities (classes, modules, functions)
should be open for extension but closed for modification. This means you can
introduce new discount types (decorators) without changing the existing
codebase, reducing the risk of introducing bugs in already working code. For
example, the DiscountDecorator class is an abstract class and required
concrete discount implementation classes such as SummerDiscount
extending it and this makes the application open to extension.

3. Code Reusability: Decorators are reusable components. Once you've


implemented a discount decorator, you can apply it to any product display
without duplicating code. This reusability can save time and effort in the long
run.

4. Flexibility: The Decorator pattern offers a high degree of flexibility. You can
mix and match different decorators to achieve various combinations of
discounts. For instance, you could apply a percentage discount, a fixed
amount discount, or even a combination of both to a single product display.

5. Maintainability: As your application evolves, adding new discount types or


modifying existing ones becomes easier with decorators. You won't need to
modify the core product display code, reducing the risk of introducing errors in
that part of the system.

6. Single Responsibility Principle: Each decorator class has a single


responsibility – managing a specific type of discount. This makes the code
easier to understand and maintain since each decorator focuses on a specific
behaviour. Moreover, each discount class is responsible for re evaluating the
product real price and turn it into a product discounted price.

7. Order of Execution: Decorators can be stacked in a specific order to achieve


the desired behaviour. For example, the discount decorator will come into play
while adding a new product to the inventory table. The admin will decide
which discount will be applicable to which product.
8. Promotes Composition over Inheritance: The Decorator pattern promotes
composition, allowing you to build complex objects by combining smaller
components. This is often considered more flexible and maintainable than
using deep inheritance hierarchies. Composition makes the application
loosely coupled. Hence instead of extending Decorators classes we just add
the decorator abstract type as an has-a property and at time of object creation
we can apply any related decorator class implementation objects as per
needed according to the requirement. This is the concept of polymorphism.

9. Avoids Class Explosion: Without the Decorator pattern, you might end up with
many classes if you create separate classes for every possible combination of
discounts. Decorators allow you to add features in a more granular and
manageable way.

Builder Pattern

I also used the builder design pattern to build product instances.

The improvements using this pattern:

Separation of Concerns: The Builder pattern separates the process of creating a


complex object from the client code that uses it. This separation simplifies client
code, as it only needs to interact with the builder and doesn't need to know the
details of object construction.
Fluent Interface: Builders often implement a fluent interface, allowing for a more
readable and expressive way to construct objects. This can make the code more
intuitive and self-documenting.

Variability and Complexity: If your products have different attributes and


configurations, the Builder pattern can handle the variability and complexity of
creating these objects. This can help avoid cluttering the product class with
numerous constructors or setter methods.

Maintainability: Changes in the way products are constructed can be localized to the
builder class, reducing the impact of modifications on the rest of the codebase.

Therefore, the use of the Decorator and Builder design patterns enhances the
application's design by promoting modular, reusable, and maintainable code. These
patterns allow you to add new features, discounts, or product attributes with minimal
impact on existing code, making your application more flexible and adaptable to
future changes.

You might also like