设计模式原则--开放-封闭原则

开放-封闭原则

定义:软件实体应该是可以扩展,但是不可修改,对扩展开放,对更改封闭

场景:某公司需要招聘3类员工,分别是:主管,程序员,销售。公司根据不同的员工的需求,配置不同的资源。比如程序员应该配台电脑。

代码实现第一版:

首先定义一个 员工类型 枚举

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.开放封闭
{
    public  enum  EmployeeType
    {
          程序员,
          销售,
          主管
    }
}

定义一个公司类,公司类里需要有个分配资源方法 Allot(EmployeeType emp);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.开放封闭
{
    public class Company
    {
       //公司根据员工需求配置 方法一
        public string Allot(EmployeeType emp)
        {
            switch (emp)
            {
                case EmployeeType.程序员:                   
                    return "配置电脑";
                case EmployeeType.销售:
                    return "配置电话";
                case EmployeeType.主管:
                    return "配置烟";                  
            }
            return "";
        }
    }
}

客户端实现

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using 设计模式原则.开放封闭;
namespace 设计模式原则
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("开始配置");

             Company emp = new Company();
             //程序员
             Console.WriteLine(emp.Allot(EmployeeType.程序员));
             //销售
             Console.WriteLine(emp.Allot(EmployeeType.销售));
             //主管
             Console.WriteLine(emp.Allot(EmployeeType.主管));
Console.WriteLine(
"完成........"); Console.Read(); } } }

第一版实现中,当公司需要招聘更多不同类型的员工时,就需要对Company类的Allot方法进行修改,还得对EmployeeType类进行修改,显然是不符合 开放-封闭原则的。

代码实现第二版:

首先定义一个 员工接口  Employee ,并声明一个员工需求方法Need();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.开放封闭
{
    public interface Employee
    {
        string Need();
         
    }
}

然后定义不同类型的员工类,并且实现Employee接口。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.开放封闭
{
    //程序员类
    public class CoderEmployee : Employee
    {
        
        public string Need()
        {
            return "配置电脑"; 
        }
    }
 
    //销售类
     public class SellerEmployee:Employee
    {
        public string Need()
        {
            return "配置电话";
        }
    }
     
     // 主管类
     public class HeaderEmployee:Employee
    {
        public string Need()
        {
            return "配置烟";
        }
    }

} 

定义一个公司类,公司类里需要有个分配资源方法 Allot2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 设计模式原则.开放封闭
{
    public class Company
    {
        //公司给员工配置 方法二
        public string Allot2(Employee emp)
        {
               return   emp.Need();
        }

    }
}

客户端实现

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using 设计模式原则.开放封闭;

namespace 设计模式原则
{
    class Program
    {
        static void Main(string[] args)
        {
             Console.WriteLine("开始配置");

             Company emp = new Company();

             //程序员
             Employee empCoder = new CoderEmployee();
             Console.WriteLine(emp.Allot2(empCoder));
             //销售
             Employee empSeller = new SellerEmployee();
             Console.WriteLine(emp.Allot2(empSeller));
             //主管
             Employee empHeader= new HeaderEmployee();
             Console.WriteLine(emp.Allot2(empHeader));

             Console.WriteLine("完成........");
             Console.Read();
        }
    }
}

第二版实现中,当公司需要招聘更多不同类型的员工时,比如美工,就只需要添加一个美工类并实现Employee接口。此版本实现了对修改封闭,对扩展开放。

posted @ 2014-04-07 17:41  老树昏鸦  阅读(315)  评论(0编辑  收藏  举报