zoukankan      html  css  js  c++  java
  • Layered Supertype

    Layered Supertype(层超类型)模式定义了一个对象,改对象充当自己所在层的所有类型的基类,而且采用类继承机制实现。

    意图:当某层中所有对象共享一组公共的业务逻辑时,可以使用Layered Supertype模式来移除重复的逻辑并将逻辑集中起来。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Chap5.LayerSuperType.Model
    {
        /// <summary>
        /// 1、抽象类EntityBase是所有业务实体都要继承的Supertype(超类型),所有实体都需要一个标识符,所有该Supertype类可以提供保存ID,并确保一经设置绝不改变的逻辑。
        /// 2、该Supertype类提供一个简单的框架来检查实体对象是否合法。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public abstract class EntityBase<T>
        {
            private T _id;
            private IList<string> _brokenRules = new List<string>();
            private bool _idHasBeenSet = false;
            public EntityBase()
            {
    
            }
            public EntityBase(T id)
            {
                this.ID = id;
            }
    
    
            public T ID
            {
                get;
                set
                {
                    if (_idHasBeenSet)
                        ThrowExceptionIfOverwritingAnd();
                    _id = value;
                    _idHasBeenSet = true;
                }
            }
    
            private void ThrowExceptionIfOverwritingAnd()
            {
                throw new ApplicationException("You cannot change the id of an entity.");
            }
            public bool IsValid()
            {
                ClearCollectionOfBrokenRules();
                CheckForBrokenRules();
                return _brokenRules.Count() == 0;
            }
    
            private void ClearCollectionOfBrokenRules()
            {
                _brokenRules.Clear();
            }
            protected abstract void CheckForBrokenRules();
    
            public IEnumerable<string> GetBrokenBusinessRules()
            {
                return _brokenRules;
            }
            protected void AddBrokenRule(string brokenRule)
            {
                _brokenRules.Add(brokenRule);
            }
        }
    }
    EntityBase.cs
    namespace Chap5.LayerSuperType.Model
    {
        public class Customer : EntityBase<long>
        {
            public Customer()
            {
    
            }
            public Customer(long id)
                : base(id)
            {
    
            }
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            protected override void CheckForBrokenRules()
            {
                if (string.IsNullOrEmpty(FirstName))
                    base.AddBrokenRule("You must supply a first Name");
    
                if (string.IsNullOrEmpty(LastName))
                    base.AddBrokenRule("You must supply a last Name");
            }
        }
    }
    Customer.cs

    Layered Supertype模式不属于GOF的设计模式。

  • 相关阅读:
    java中的IO整理
    指针 study~O(∩_∩)O~
    const形参与非const形参
    最大公约数递归求解
    值传递、地址传递、引用传递(参考网上各种资料总结哦~)
    windows8开发笔记(4)消息弹出动画
    Azure绑定域名以及利用FTP上传文件
    windows8安装第三方Modern UI(Metro)应用方法
    windows8开发笔记(2)通知
    windows8开发笔记(5)说说Json的序列化和反序列化
  • 原文地址:https://www.cnblogs.com/vichin/p/13062491.html
Copyright © 2011-2022 走看看