原文链接:https://www.cnblogs.com/tiger-wang/p/13525841.html
通常讲到设计模式,一个最通用的原则是SOLID:
- S - Single Responsibility Principle,单一责任原则
- O - Open Closed Principle,开闭原则
- L - Liskov Substitution Principle,里氏替换原则
- I - Interface Segregation Principle,接口隔离原则
- D - Dependency Inversion Principle,依赖倒置原则
嗯,这就是五大原则。
后来又加入了一个:Law of Demeter,迪米特法则。于是,就变成了六大原则。
原则好理解。怎么用在实践中?
一、单一责任原则
单一责任原则,简单来说就是一个类或一个模块,只负责一种或一类职责。
看代码:
public interface IUser
{
void AddUser();
void RemoveUser();
void UpdateUser();
void Logger();
void Message();
}
根据原则,我们会发现,对于IUser
来说,前三个方法:AddUser
、RemoveUser
、UpdateUser
是有意义的,而后两个Logger
和Message
作为IUser
的一部分功能,是没有意义的并不符合单一责任原则的。
所以,我们可以把它分解成不同的接口:
public interface IUser
{
void AddUser();
void RemoveUser();
void UpdateUser();
}
public interface ILog
{
void Logger();
}
public interface IMessage
{
void Message();
}
拆分后,我们看到,三个接口各自完成自己的责任,可读性和可维护性都很好。
下面是使用的例子,采用依赖注入来做:
public class Log : ILog
{
public void Logger()
{
Console.WriteLine("Logged Error");
}
}
public class Msg : IMessage
{
public void Message()
{
Console.WriteLine("Messaged Sent");
}
}
class Class_DI
{
private readonly IUser _user;
private readonly ILog _log;
private readonly IMessage _msg;
public Class_DI(IUser user, ILog log, IMessage msg)
{
this._user = user;
this._log = log;
this._msg = msg;
}
public void User()
{
this._user.AddUser();
this._user.RemoveUser();
this._user.UpdateUser();
}
public void Log()
{
this._log.Logger();
}
public void Msg()
{
this._msg.Message();
}
}
public static void Main()
{
Class_DI di = new Class_DI(new User(), new Log(), new Msg());
di.User();
di.Log();
di.Msg();
}
这样的代码,看着就漂亮多了。
二、开闭原则
开闭原则要求类、模块、函数等实体应该对扩展开放,对修改关闭。
我们先来看一段代码,计算员工的奖金:
public class Employee
{
public int Employee_ID;
public string Name;
public Employee(int id, string name)
{
this.Employee_ID = id;
this.Name = name;
}
public decimal Bonus(decimal salary)
{
return salary * .2M;
}
}
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee(101, "WangPlus");
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp.Employee_ID, emp.Name, emp.Bonus(10000));
}
}
现在假设,计算奖金的公式做了改动。
要实现这个,我们可能需要对代码进行修改:
public class Employee
{
public int Employee_ID;
public string Name;
public string Employee_Type;
public Employee(int id, string name, string type)
{
this.Employee_ID = id;
this.Name = name;
this.Employee_Type = type;
}
public decimal Bonus(decimal salary)
{
if (Employee_Type == "manager")
return salary * .2M;
else
return
salary * .1M;
}
}
显然,为了实现改动,我们修改了类和方法。
这违背了开闭原则。
那我们该怎么做?
我们可以用抽象类来实现 - 当然,实际有很多实现方式,选择最习惯或自然的方式就成:
public abstract class Employee
{
public int Employee_ID;
public string Name;
public Employee(int id, string name)
{
this.Employee_ID = id;
this.Name = name;
}
public abstract decimal Bonus(decimal salary);
}
然后,我们再实现最初的功能:
public class GeneralEmployee : Employee
{
public GeneralEmployee(int id, string name) : base(id, name)
{
}
public override decimal Bonus(decimal salary)
{
return salary * .2M;
}
}
class Program
{
public static void Main()
{
Employee emp = new GeneralEmployee(101, "WangPlus");
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp.Employee_ID, emp.Name, emp.Bonus(10000));
}
}
在这儿使用抽象类的好处是:如果未来需要修改奖金规则,则不需要像前边例子一样,修改整个类和方法,因为现在的扩展是开放的。
代码写完整了是这样:
public abstract class Employee
{
public int Employee_ID;
public string Name;
public Employee(int id, string name)
{
this.Employee_ID = id;
this.Name = name;
}
public abstract decimal Bonus(decimal salary);
}
public class GeneralEmployee : Employee
{
public GeneralEmployee(int id, string name) : base(id, name)
{
}
public override decimal Bonus(decimal salary)
{
return salary * .1M;
}
}
public class ManagerEmployee : Employee
{
public ManagerEmployee(int id, string name) : base(id, name)
{
}
public override decimal Bonus(decimal salary)
{
return salary * .2M;
}
}
class Program
{
public static void Main()
{
Employee emp = new GeneralEmployee(101, "WangPlus");
Employee emp1 = new ManagerEmployee(102, "WangPlus1");
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp.Employee_ID, emp.Name, emp.Bonus(10000));
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp1.Employee_ID, emp1.Name, emp1.Bonus(10000));
}
}
三、里氏替换原则
里氏替换原则,讲的是:子类可以扩展父类的功能,但不能改变基类原有的功能。它有四层含义:
- 子类可以实现父类的抽象方法,但不能覆盖父类的非抽象方法;
- 子类中可以增加自己的特有方法;
- 当子类重载父类的方法时,方法的前置条件(形参)要比父类的输入参数更宽松;
- 当子类实现父类的抽象方法时,方法的后置条件(返回值)要比父类更严格。
在前边开闭原则中,我们的例子里,实际上也遵循了部分里氏替换原则,我们用GeneralEmployee
和ManagerEmployee
替换了父类Employee
。
还是拿代码来说。
假设需求又改了,这回加了一个临时工,是没有奖金的。
public class TempEmployee : Employee
{
public TempEmployee(int id, string name) : base(id, name)
{
}
public override decimal Bonus(decimal salary)
{
throw new NotImplementedException();
}
}
class Program
{
public static void Main()
{
Employee emp = new GeneralEmployee(101, "WangPlus");
Employee emp1 = new ManagerEmployee(101, "WangPlus1");
Employee emp2 = new TempEmployee(102, "WangPlus2");
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp.Employee_ID, emp.Name, emp.Bonus(10000));
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp1.Employee_ID, emp1.Name, emp1.Bonus(10000));
Console.WriteLine("Employee ID: {0} Name: {1} Bonus: {2}", emp2.Employee_ID, emp2.Name, emp2.Bonus(10000));
Console.ReadLine();
}
}
显然,这个方式不符合里氏替原则的第四条,它抛出了一个错误。
所以,我们需要继续修改代码,并增加两个接口:
interface IBonus
{
decimal Bonus(decimal salary);
}
interface IEmployee
{
int Employee_ID { get; set; }
string Name { get; set; }
decimal GetSalary();
}
public abstract class Employee : IEmployee, IBonus
{
public int Employee_ID { get; set; }
public string Name { get; set; }
public Employee(int id, string name)
{
this.Employee_ID = id;
this.Name = name;
}
public abstract decimal GetSalary();
public abstract decimal Bonus(decimal salary);
}
public class GeneralEmployee : Employee
{
public GeneralEmployee(int id, string name) : base(id, name)
{
}
public override decimal GetSalary()
{
return 10000;
}
public override decimal Bonus(decimal salary)
{
return salary * .1M;
}
}
public class ManagerEmployee : Employee
{
public ManagerEmployee(int id, string name) : base(id, name)
{
}
public override decimal GetSalary()
{
return 10000;
}
public override decimal Bonus(decimal salary)
{