Software Craftsmanship: How to implement Open Closed Principle
来源:互联网 发布:ip地址数据库下载 编辑:程序博客网 时间:2024/05/22 14:52
/*
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
- Software Craftsmanship: How to implement Open Closed Principle
- Open-Closed Principle,OCP
- Open-Closed Principle
- 开闭原则(open-closed principle)
- 开闭原则(Open-Closed Principle)
- OCP简介(OCP--Open-Closed Principle)
- OCP(Open-Closed Principle)开放封闭原则
- 开放闭合原则--Open-Closed Principle
- “开-闭”原则(Open-Closed Principle)
- Open-Closed Principle 开-闭原则
- “开闭原则”(Open-Closed Principle,OCP
- “开-闭”原则(Open-Closed Principle)
- 开放关闭原则【The Open Closed Principle】
- 开放--封闭原则(Open Closed Principle,OPC)
- 开放关闭原则(Open-Closed Principle)
- 开-闭原则(OCP - Open-Closed Principle )
- Open Closed Principle 开放封闭原则
- 【zz】Easy way to understand open/closed principle :O of S.O.L.I.D
- 一梦红尘,别问是劫是缘
- 判定一下设备是否为iphone5:
- J2EE (七) 配置文件详解以及dom4j进行解析
- C++中全局变量、静态变量、自动变量的在内存中的位置【DEBUG】
- 7款华丽的jQuery/CSS3效果及源码
- Software Craftsmanship: How to implement Open Closed Principle
- Automake stops with “non-POSIX variable name”
- DevPartner资源备忘
- 推荐12个漂亮的CSS3按钮实现方案
- dictionary用法(1)
- C#泛型详解
- Makefile中的wildcard用法
- 西风不暖当年月,一别经年再相逢
- make和makefile介绍