Software Craftsmanship: How to implement Open Closed Principle

/*

Author: Jiangong SUN

*/


S.O.L.I.D (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion) are object oriented design principles, which are best practices for OOP software development.


Open Closed Principle means: open for extension, closed for modification.

For example: when there are some new functionalities coming, minimize the modification of exsiting code, and just add new class, new code to extend it.


Here I've made an example for demonstrating open closed principle.


There are three cars with their properties

            public class Peugeot107
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
            }

            public class Peugeot208
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
            }

            public class Peugeot307
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
            }

Here we calculate their prices

            public class Car
            {
                public double PriceCalculate(object car)
                {
                    double price = new double();
                    if (car is Peugeot107)
                    {
                        Peugeot107 x = (Peugeot107)car;
                        price = 10 * x.PriceFactor1 + x.PriceFactor2;
                    }
                    else if (car is Peugeot208)
                    {
                        Peugeot208 x = (Peugeot208)car;
                        price = 10 * x.PriceFactor1 * x.PriceFactor2;
                    }
                    else if (car is Peugeot307)
                    {
                        Peugeot307 x = (Peugeot307)car;
                        price = 10 * x.PriceFactor1 + 30 * x.PriceFactor2;
                    }
                    return price;
                }
            }

In this case, when we need to add new cars, we need to modify the method "PriceCalculate" which doesn't respect open closed principle.


Now, we will see how to optimize it with open closed principle.

Firstly, we create an interface and declare a method in it.

            public interface ICar
            {
                double PriceCalculator();
            }

Then, create several car classes who implement this interface.

            public class Peugeot107 : ICar
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
                public double PriceCalculator()
                {
                    return 10 * PriceFactor1 + PriceFactor2;
                }
            }
            public class Peugeot208 : ICar
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
                public double PriceCalculator()
                {
                    return 10 * PriceFactor1 * PriceFactor2;
                }
            }
            public class Peugeot307 : ICar
            {
                public string Name { get; set; }
                public int PriceFactor1 { get; set; }
                public int PriceFactor2 { get; set; }
                public double PriceCalculator()
                {
                    return 10 * PriceFactor1 + 30 * PriceFactor2;
                }
            }

In this way, if we want to add new cars, we just need to add a new car class and implement its own price calculation. The application is extended, and not modified.


I hope you enjoy this article. Enjoy coding! 


references:

http://joelabrahamsson.com/a-simple-example-of-the-openclosed-principle

http://en.wikipedia.org/wiki/Open/closed_principle

http://www.oodesign.com/open-close-principle.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值