设计模式原则--单一职责原则

单一职责原则(SRP)

定义:就一个类而言,应该仅有一个引起它变化的原因
场景: 一个公司有3类员工,分别是 主管,程序员,销售
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.单一职责
{
   public  class Employee
    {  

       //程序员写代码
       public string Coding()
       {
           return "程序员写代码";     
       }
       //销售打电话
       public string Calling()
       {
           return "销售打电话";
       }
       //主管叼烟
       public string Smoking()
       {
           return "主管叼烟";
       }       
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using 设计模式原则.单一职责;

namespace 设计模式原则
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("上班了.....");

             Employee emp = new Employee();
             Console.WriteLine(emp.Calling());
             Console.WriteLine(emp.Coding());
             Console.WriteLine(emp.Smoking());             
                  
             Console.Read();

        }
    }
}
 这时变化来了主管需要叫人加班
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.单一职责
{
   public  class Employee
    {  
       //程序员写代码
       public string Coding()
       {
           return "程序员写代码";     
       }
       //销售打电话
       public string Calling()
       {
           return "销售打电话";
       }
       //主管叼烟
       public string Smoking()
       {
           return "主管叼烟";
       }
       //变化 突发情况  主管要使唤人了
       //程序员加班写代码
       public string  CoderWorkOvertime()
       {
           return "程序员加班" + Coding();
       }
       //销售加班打电话
       public string SellerWorkOvertime()
       {
           return "销售加班" + Calling(); 
       }    
       //.....等等                 
    }
}
这样就会发现Employee类 在主管,销售,程序员都会引起这个类发生变化,一旦发生变化方法就会增加,类就需要修改,当代码量多时,维护困难
这时就需要分类,把Employee的职责分离
程序员:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.单一职责
{
    public class CoderEmployee
    {
        //程序员写代码
        public string Coding()
        {
            return "程序员写代码";
        }
    }
}
销售:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.单一职责
{
   public  class SellerEmployee
    {

        //销售打电话
        public string Calling()
        {
            return "销售打电话";
        }
    }
}
 主管:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 设计模式原则.单一职责
{
   public class HeaderEmployee
    {     
        //主管叼烟
        public string Smoking()
        {
            return "主管叼烟";
        }
        //变化 突发情况  主管要使唤人了 
       
       //程序员加班写代码
        public string CoderWorkOvertime(CoderEmployee coder)
        {
            return "程序员加班" + coder.Coding();
        }
        //销售加班打电话
        public string SellerWorkOvertime(SellerEmployee seller)
        {
            return "销售加班" + seller.Calling(); 
        } 
    }
}
 分类过后 Employee职责被分离。主管,销售,程序员 都只关心自身的职责。如果还需要添加职责的话只需要在对应的类里添加对应的职责。
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using 设计模式原则.单一职责;

namespace 设计模式原则
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("上班了.....");
          HeaderEmployee header = new HeaderEmployee();
             SellerEmployee seller = new SellerEmployee();
             CoderEmployee coder = new CoderEmployee();
    
     Console.WriteLine(seller.Calling());
//销售打电话 Console.WriteLine(coder.Coding());//程序员写代码 Console.WriteLine(header.Smoking());//主管叼烟; Console.WriteLine(header.CoderWorkOvertime(coder)); Console.WriteLine(header.SellerWorkOvertime(seller)); Console.Read(); } } }

 

转载于:https://www.cnblogs.com/yangjingqi/p/3648269.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值