zoukankan      html  css  js  c++  java
  • 面向对象原则之单一职责原则实现

    根据《你必须知道的.NET》单一职责原则章节启发,使用vs2010 uml建模,并生成代码。完成权限职责分离UML设计,说明面向对象单一职责原则。涉及到的知识点:单一职责原则、vs2010 uml建模项目、权限职责分离的设计。

    image

    图1:使用vs2010完成的uml类图

     

    image            image

    图2:整个项目解决方案                            图3:Uml类图资源

    完成图1可以在UML模型资源管理器中右键ModelingProject2->点击Generate Code生成图2解决方案中的ModelingProject2Lib项目。经过简单修改完成单一职责原则实现代码。

    image

    特别注意:改写生成的DBManagerProxy 类时,传给GetPermission(string id)的参数应为:GetPermission((dbmanager as DBManager).Id。

    接口代码:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Changes to this file will be lost if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 
    
    public interface IDBAction 
    {
        void Add(); 
    
        bool Delete(); 
    
        void View(); 
    
    } 
    
    

    数据管理类代码:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Changes to this file will be lost if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 
    
    public class DBManager : IDBAction
    {
        private DBManagerProxy dbmanagerProxy;
        private string id = String.Empty; 
    
        public string Id {
            get { return id; }
            set { id = value; }
        } 
    
        public DBManager(string id) {
            Id = id;
        }
        public virtual void Add()
        {
            Console.WriteLine(Id +"用户具有增加数据权限!!!");
        } 
    
        public virtual bool Delete()
        {
            throw new System.NotImplementedException();
        } 
    
        public virtual void View()
        {
            throw new System.NotImplementedException();
        } 
    
    } 
    
    
    
    数据管理代理类代码:
    
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Changes to this file will be lost if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 
    
    public class DBManagerProxy : IDBAction
    {
        private IDBAction dbmanager;
        public DBManagerProxy() { 
        }
        public DBManagerProxy(IDBAction dbAction)
        {
            dbmanager = dbAction;
        } 
    
        public virtual string GetPermission(string id) {
            return ID = id;
        } 
    
        public virtual bool Delete()
        {
            throw new System.NotImplementedException();
        } 
    
        public virtual void View()
        {
            throw new System.NotImplementedException();
        } 
    
        public virtual void Add()
        {
            if (GetPermission((dbmanager as DBManager).Id) == "CanAdd") {
                dbmanager.Add();
            }
        } 
    
        public string ID { get; set; }
    } 
    
    

    数据管理客户端代码:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Changes to this file will be lost if the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text; 
    
    public class DBClient
    {
        private IDBAction Manage
        {
            get;
            set;
        } 
    
        public virtual IDBAction IDBAction
        {
            get;
            set;
        } 
    
       public static void Main()
       {
           IDBAction DBManager = new DBManagerProxy(new DBManager("CanAdd"));
           DBManager.Add();
           Console.ReadKey();
       }
    } 
    
    

    以上为单一设计原则的所用UML建模截图,及实现代码。如有好的意见请不令赐教!

  • 相关阅读:
    js数组从小到大排序
    高效率去掉js数组中重复项
    Oracle start with.connect by prior子句实现递归查询
    ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)
    ofbiz进击 个人遇到的奇葩问题汇总。
    ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析
    ofbiz进击 第五节。 --OFBiz配置之[general.properties] 共有属性的分析(含email)
    ofbiz进击 第四节。 我的form之旅
    &nbsp|&quot|&amp|&lt|&gt等html字符转义
    ofbiz进击 第三节。 各个关键文件的说明与作用
  • 原文地址:https://www.cnblogs.com/jiangxin/p/2856457.html
Copyright © 2011-2022 走看看