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


    单一职责原则(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(); } } }
  • 相关阅读:
    bug、兼容性、适配问题
    关于daterangepicker取消默认值的设置
    重构-改善既有代码设计
    iphoneX 适配
    汇编语言(2)程序表示
    汇编语言(1)基础理论
    css 边框颜色渐变的半圆
    横向时间轴(进度条)
    pdf中内嵌字体问题
    jabRef里引用的相邻同名作者变横线
  • 原文地址:https://www.cnblogs.com/yangjingqi/p/3648269.html
Copyright © 2020-2023  润新知