面向对象原则之单一职责原则实现

根据《你必须知道的.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建模截图,及实现代码。如有好的意见请不令赐教!

转载于:https://www.cnblogs.com/jiangxin/archive/2013/01/11/2856457.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值