C#中类和接口的设计思想(本人认为比较好的思想,欢迎大家讨论指点)

        当我们设计一个类时,首先要规划这个类主要是干什么用的,并且对外要放出哪些接口。这时就要设计这个类的对外接口都有哪些了。在进行多层次开发的时候,由于下层对上层是透明的,上层无须知道下层的操作方式以及代码,因此我主张层与层之间的交互主要靠的是接口。尽量不要用类。下面的代码是设计的接口,类,以及其他的层的调用。


 

 1 None.gif      public   interface  IMaster
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 4InBlock.gif        /// 获得名称
 5ExpandedSubBlockEnd.gif        /// </summary>

 6InBlock.gif        string GetName();
 7ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 8InBlock.gif        /// 设置名称
 9InBlock.gif        /// </summary>
10ExpandedSubBlockEnd.gif        /// <param name="str">名称</param>

11InBlock.gif        void SetName(string str);
12ExpandedBlockEnd.gif    }

13 None.gif     public   class  Master:IMaster
14 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
15ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
16InBlock.gif        /// 底层
17ExpandedSubBlockEnd.gif        /// </summary>

18InBlock.gif        private Master()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            //
21InBlock.gif            // TODO: 在此处添加构造函数逻辑
22InBlock.gif            //
23ExpandedSubBlockEnd.gif        }

24InBlock.gif        private static Master _instance = null;
25ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
26InBlock.gif        /// 
27InBlock.gif        /// </summary>
28ExpandedSubBlockEnd.gif        /// <returns></returns>

29InBlock.gif        public static Master Instance()
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            if(_instance == null)
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                _instance =  new Master();
34ExpandedSubBlockEnd.gif            }

35InBlock.gif            return _instance;
36ExpandedSubBlockEnd.gif        }

37InBlock.gif        private string Name;
38InBlock.gif
39ContractedSubBlock.gifExpandedSubBlockStart.gif        IMaster接口#region IMaster接口
40InBlock.gif
41InBlock.gif        public string GetName()
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            return Name;
44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46InBlock.gif        void SetName(string str)
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48InBlock.gif            Name = str;
49ExpandedSubBlockEnd.gif        }

50ExpandedSubBlockEnd.gif        #endregion

51ExpandedBlockEnd.gif    }

52 None.gif
53 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
54InBlock.gif    /// 上层
55ExpandedBlockEnd.gif    /// </summary>

56 None.gif      public   class  User
57 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
58InBlock.gif        IMaster MasterExample = null;
59InBlock.gif        public User()
60ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
61InBlock.gif            MasterExample = Master.Instance();
62ExpandedSubBlockEnd.gif        }

63InBlock.gif        
64InBlock.gif        public string GetUseName()
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            return MasterExample.GetName();
67ExpandedSubBlockEnd.gif        }

68InBlock.gif        public string SetUseName(string str)
69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
70InBlock.gif            MasterExample.SetName(str);
71ExpandedSubBlockEnd.gif        }

72ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/yangyang8848/archive/2006/04/18/378202.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值