根据《你必须知道的.NET》单一职责原则章节启发,使用vs2010 uml建模,并生成代码。完成权限职责分离UML设计,说明面向对象单一职责原则。涉及到的知识点:单一职责原则、vs2010 uml建模项目、权限职责分离的设计。
图1:使用vs2010完成的uml类图
图2:整个项目解决方案 图3:Uml类图资源
完成图1可以在UML模型资源管理器中右键ModelingProject2->点击Generate Code生成图2解决方案中的ModelingProject2Lib项目。经过简单修改完成单一职责原则实现代码。
特别注意:改写生成的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建模截图,及实现代码。如有好的意见请不令赐教!