Single Yuanz(单一原则)

(一)单一模式定义

一个类,只负责一件事。(例如:老师负责教学,学生负责学习)

(二)单一原则优劣

缺点

  1. 一个职责的变化可能会削弱或者抑制这个类实现其他职责的能力例如:张三精通C,C#,Java,而叫他去负责C辅导工作,就埋没了他原有的C#,Java能力
  2. 当客户端需要该对象的某一个职责时,不得不将其他不需要的职责全都包含进来,从而造成冗余代码或代码的浪费张三会精通Java,C.C#,一个人能够完成工作,而上面的领导有派了李四(精通C#,Java),这就冗余了人才

优点

  1. 单一原则的核心就是控制功能粒度的大小,将对象解耦,提高内聚性(相当于每个功能有自己的工作局域,分工明确,有助于提高工作效率)
  2. 降低复杂度
  3. 提高类的可读性
  4. 提高系统的可维护性
  5. 变更时引起的风险降低

该图主要表达的内容是:总经理起到引领作用,各部门经理,主管都是本部门的监督者,执行者,只管本部门的事,其它部门不需要管理,这就是一个单一原则。 

 /// <summary>
    /// 总经理
    /// </summary>
    public class General { }

    /// <summary>
    /// 各部门属性接口
    /// </summary>
    public interface Single
    {
        /// <summary>
        /// 前厅部:迎客
        /// </summary>
        string Recept { get; set; }

        /// <summary>
        /// 销售部:销售
        /// </summary>
        string Sale { get; set; }

        /// <summary>
        /// 技术部:技术支持
        /// </summary>
        string Technology { get; set; }

        /// <summary>
        /// 安保部:负责安全
        /// </summary>
        string Security { get; set; }

        /// <summary>
        /// 财务部:负责财产数据
        /// </summary>
        string Finance { get; set; }
    }

    /// <summary>
    /// 部门职责接口
    /// </summary>
    public interface Patter
    {
        /// <summary>
        /// 总经理接口
        /// </summary>
        /// <param name="Generals"></param>
        void Reception(General Generals);

        /// <summary>
        /// 前台做接待工作
        /// </summary>
        void Recept();

        /// <summary>
        /// 销售做销售工作
        /// </summary>
        void Sales();

        /// <summary>
        /// 技术部做技术工作
        /// </summary>
        void Technology();

        /// <summary>
        /// 安保部做安保工作
        /// </summary>
        void Security();

        /// <summary>
        /// 财务做财务工作
        /// </summary>
        void Finance();

    }

    /// <summary>
    /// 部门属性实现类
    /// </summary>
    public class MobibleProperty : Single
    {
        public string Recept { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string Sale { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string Technology { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string Security { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string Finance { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }

    /// <summary>
    /// 部门职责实现类
    /// </summary>
    public class MobiblePropertys : Patter
    {
        public void Reception(General Generals)
        {
            throw new NotImplementedException();
        }

        public void Sales()
        {
            throw new NotImplementedException();
        }

        void Patter.Finance()
        {
            throw new NotImplementedException();
        }

        void Patter.Recept()
        {
            throw new NotImplementedException();
        }

        void Patter.Security()
        {
            throw new NotImplementedException();
        }

        void Patter.Technology()
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 具体公司
    /// </summary>
    public class WYN
    {
        private MobibleProperty w_Single;
        private MobiblePropertys w_Patter;
        public WYN(MobibleProperty property, MobiblePropertys propertys)
        {
            w_Single = property;
            w_Patter = propertys;
        }
    }

 文章简短,案例简单,不断的学习,才能不断的成长!

发布了122 篇原创文章 · 获赞 36 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qqj3066574300/article/details/103839957
今日推荐