Skip to content

Latest commit

 

History

History

2.Open_Close_Principle

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Open/Closed Principle

Before

classDiagram
    class Order
    Order: +line_items
    Order: +shipping
    Order: +get_total()
    Order: +get_shipping_cost()
    Order: +get_arrival_date()
Loading

An class should be open for extension but closed for modification. This is the motto of this principle. Here as we can see, if we need to add another shipping method in future let's say by 'waterways', we need to update the Order class which is not a good way.

After

classDiagram
    class Order
    Order: +line_items
    Order: +shipping#58; Shipping
    Order: +get_total()
    Order: +get_shipping_cost()
    Order: +get_arrival_date()

    class Shipping
    <<interface>> Shipping
    Shipping: +get_cost(order)
    Shipping: +get_arrival_date(order)

    class Ground
    Ground: +get_cost(order)
    Ground: +get_arrival_date(order)

    class Air
    Air: +get_cost(order)
    Air: +get_arrival_date(order)

    Order o-- Shipping
    Ground ..|> Shipping
    Air ..|> Shipping
Loading

Now here, shipping actually is an object that has all the necessary functions defined using an interface (protocol). If new shipping method are further added to the system, we now need no modification in order class. Instead we implement Shipping interface for our new 'waterways' shipping method