posts - 18, comments - 317, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

2005年1月20日

剖析SharpDevelopCommand模式的应用    

SharpDevelop是一个非常优秀的开源项目,其中有一些思想非常出色的,如插件结构,服务设计,设计模式的应用,UI与行为的抽离,以及可插入式的设计结构, 其中有些设计思想我已经应用到我以前开发过的一些项目中。由于我的文笔不好及工作太忙,所以一直没有写什么心得之类的笔记。现在有点时间,也来记录一下我的分析随笔,先来分析一下SharpDevelop中模式的应用,至于其非常优秀的插件结构,博客园已有兄弟作过分析了。

           在剖析Command模式之前,先来看一下.NET Framework中的菜单及工具栏处理存在的一些缺点:

1.            无法自动同步更新菜单项与工具项的状态以及相关的行为及特性,需要手动去处理两个事件,从而造成了相同的两个操作需要两次处理,也就造成了需要手动地同时维护多份相同的操作;

2.            无法重用菜单项这部分的功能,因为在dotnet中菜单项的单击需要集成在UI中;

3.            无法很好地扩展,如需要增加或删除菜单项,则需要更改原有的代码及UI部分的设计,从而违反了OCP(开闭)原则。按照OCP原则,当扩展相关的功能时,不应该修改原始的代码,而应该扩展该代码;

4.            无法将相关的消息当作原子状态处理,试想一下,如果一个系统不仅通过菜单、工具栏来操作用户接口,也可通过命令(如Visual Studio就可以通过输入命令而执行相关的操作)来执行,如果不将相关的操作抽象出来的话,维护这个操作的成本太大。并且如果想实现更进一步的控制比如精确到原子状态的权限控制,则很难实现。

我们可以运用Command模式,将菜单项相应的操作抽象出来,定义一个命令接口ICommand,此接口仅具有Run方法,可以作为任何命令的基接口。但是由于菜单还具有一些其它的特性,如是否可见,是否可用,文本,需要执行此菜单项的宿主,所以再定义了一个IMenuCommand接口继承此接口,加入了一些新的特性,并定义了一个实现IMenuCommand接口的抽象基类MenuCommand,其它类可以派生此类。由于我自已修改了SharpDevelop中的一些设计,所以可能有些与原版的不一样。

 
          ICommand
接口:是一个最基本的接口,代表一个动作的行为;

IMenuCommand接口:派生于ICommand接口,作为菜单项或工具项所实现的行为及特性来使用;

IStatusUpdate接口:描述一个能够更新状态的接口,当对象需要更新状态时将调用此接口,如菜单项需要动态地更新EnableVisible特性;

MenuCommand抽象基类:实现了IMenuCommand接口,任何菜单命令可以从此继承;

CustomCommandBarItem:继承于CommandBar组件的CommandBarButton类,类似于菜单项或工具项,实现了IStatusUpdate接口,以根据IMenuCommand接口的VisibleEnabled属性的改变而相应地更新其Enabled,Visible属性;

CustomCommandBarMenu:继承于CommandBar组件的CommandBarMenu类,类似于菜单条。           
    当我们在客户端需要创建命令时,只需写相关的类继承于MenuCommand         
        

           在这里使用了另外一个开源组件CommandBarCustomCommandBarItem类的构造函数通过传入一个IMenuCommand类的实例与IMenuCommand关联起来,将单击事件(OnClick方法)委托给ICommand接口的Run方法来执行,其Update方法用于更新状态。CustomCommandBarMenu类中的OnDropDown方法用于当打开菜单条时通过调用IStatusUpdateUpdate方法自动更新所有菜单项的状态。

           SharpDevelop处理所有插件的菜单中,此处的设计是支持的基础。

          

           如果更进一步,可以在SharpDevelop中加入类似于Visual Studio的“命令窗口”;如果是一个ERP系统或MIS系统,可以将权限控制写在IMenuCommand中的Enabled中,在执行Run方法之前先检测Enabled,如果Enabledfalse则禁止执行,我先前写过一个权限框架,通过AttributeReflect技术来实现权限的自动控制。

           Reference:

1.            吕震宇的设计模式系列文章

2.            GoFDesign Pattern

posted @ 2005-01-20 12:03 jeseeqing 阅读(3848) 评论(11) 编辑

2005年1月10日

17的会议我因为后来有点事,参加了10多分钟后来退出去了,此表示抱谦。对于这次会议的成果,我只能通过会议记录总结了

posted @ 2005-01-10 09:55 jeseeqing 阅读(766) 评论(1) 编辑

2005年1月4日

随着钟声的敲响,2004年就这样走过了,在这一年中国家取得了各方面的好成绩,大家正普天同庆的时候,独自一人深夜走在广州的街道上,迈着沉重的步伐,时有一丝寒风吹进我的心里。对于我来说,2004年是我从事软件开发的第三个年头,当2005年来临时,我怎样总结这几年的工作,怎样制定新的职业规划,好让人生指针指向一个有目标确定的地址。

1.          2001年职业生涯总的来说,处于起步阶段,让我从一个陌生的行业渐渐确定了自已的方向,在这一年中我曾徘徊过做网络行业还是软件开发行业,也许当时对于我来说,这种选择是比较痛苦的,同时对自已又是有决定性作用的,一个偶然的机会及哥、朋友间的请示我坚决地决定了我未来十年的总体职业规划:成为一名优秀的软件工程师。

2.          对生活方式有了重新的想法。爸出门前再三强调“出门靠朋友,在家靠父母”,经过几年的工作我认识到了朋友在人生道路上的重要性。一个好的知心朋友有时可以决定你的将来,你的职业,你的喜怒哀乐。在此我衷心地希望我的知心朋友能够在未来的人生旅途中一路凯歌,事事顺心,走向人生的辉煌,我祝福你们在新的一年一路走好!因为以前在学校太内向,在学校的各种活动也不是很活跃,现在经过很多的磨练,已经改变。

3.          多出来走走,对自已很有利!在2001年朋友曾说过有机会走出长沙去外界转转,对你很有好处。我还不以为然,一次偶然机会使我走出了伤心之地长沙,来到了中国改革开放的前沿地-深圳特区。也许这次成为我职业生涯中的一个转折点,我从此起步,通过自己的不断努力将实现我事业上的成功。这里的生活节奏不允许我有丝毫的松懈,在这种激烈的竞争的环境中你只有苦学,苦练,苦问才会有立足之地。职业生涯中始终要记住:坚持,坚持,再坚持;失败,失败,再失败之后可能才有成功的机会;学习,学习,再学习才有说话的资源

4.          在我工作了从事了半年软件开发之后,也就是从2002年中开始我投入了.NET的怀抱,从一个VB程序员转变为.NET程序员。在这其中,从一个基于对象的思想并带有面向过程的思想的程序员完全转变为面向对象的程序员,这其中的转变多少也带有一些痛苦,但是转变成功后,发现.NET的世界真是美好。

5.          2004年结识了敏捷开发,并且在项目中实施了TDD,体验了TDD这种轻型开发方法的魅力,其最大的特点完全发挥了程序员本身的因素,真正做到了以人为本。

6.          2004年下半年知道了Blog的存在,在博客园上安了家,在上面结识了一些朋友,并且在2004年的最后一天加入了博客园的“Design & Pattern团队,希望能在团队中能多交些朋友,多学点东西。

 

总结2004年,收获了多少?缺点改变了多少?学到了多少?年初制定的计划实现了多少?做对了几件事?又做错了几件事?有了多少的进步?我想是我好好坐下来静静地想一想的时候了!时间不等人,在这个时代,速度、高效、学习、知识、信息充斥于地球地任一个角落。

 

                                                                                                                                                         Jesee Qing

                                                                                                                                                        2005.1.1

                                                                                                                                                        凌晨010       书于广州

posted @ 2005-01-04 12:18 jeseeqing 阅读(1494) 评论(4) 编辑

2004年12月30日

看到Wayfer说到GMail的事,我也来派发GMail。

申请到GMail已经差不多1个多月了,虽然其号称1G,但由于测试其间速度太慢,所以也就很少使用。

现在手上有5个邀请,谁要的话留下email及其firstName,lastName。

希望Google的东西永远是好东西,最近Google发布了Google Desktop Search,用于搜索桌面上的信息,可以与Google在线的集成。

谁要的话留给email,最后更新日期:2005.8.15

posted @ 2004-12-30 19:31 jeseeqing 阅读(3464) 评论(87) 编辑

2004年12月21日

C# Programming Guidelines(摘录)

以下条目来自于Larry O'BrienBruce Eckel的《Think C#》一书,我个人认为英文表达胜于使用中文来表达,所以也没有翻译相关的内容。

 

  1. Elegance Always pays off.
  1. First make it work, then make it fast.  在敏捷开发中,有一条原则:Keep it simple
  1. Remember the "divide and conquer" principle.
  1. Separate the class creator from the class user (client programmer)
  1. When you create a class, attempt to make your names so clear that comments are unnecessary.
  1. Your analysis and design must produce, at minimum, the classes in your system, their public interfaces, and their relationships to other classes, especially base classes.
  1. Automate everything. 类似于敏捷开发中的Continue Integrated.
  1. Write the test code first (before you write the class) in order to verify that your class design is complete. 类似于敏捷开发中的Write Test First
  1. All software design problems can be simplified by introducing an extra level of conceptual indirection.
  1. An indirection should have a meaning.
  1. Make classes as atomic as possible.  单一职责原则(SRP)很好地解释了这一点,SRP可以理解为为“就一个类而言,应该仅有一个引起它变化的原因”,详细信息请参阅《Agile Software Development Principles, Patterns, and Practices》;
  1. Watch for long argument lists. 可以使用重构中的Introduce Parameter object(引入参数对象)来重构代码,关于重构,请参阅《Refactoring: Improving the Design of Existing Code
  1. Don't repeat yourself. 不要使你的代码有Duplicated Code坏味道,当有后可以使用重构中的Extract Method手段来重构代码;
  1. Watch for switch statements or chained if-else clauses. 不要使你的代码有Switch Statements坏味道,当有后可以使用重构中的Replace Conditional with Polymorphism手段来重构代码;
  1. From a design standpoint, look for and separate things that change from thins that stay the same.
  1. Don't extend fundamental functionality by subclassing.
  1. Less is more.
  1. Read your classes aloud to make sure they're logical.
  1. When deciding between inheritance and composition, ask if you need to upcast to the base type.
  1. Use data members for variation in value and method overriding for variation in behavior.
  1. Watch for overloading.
  1. Use exception hierarchies;
  1. Sometimes simple aggregation does the job.
  1. Consider the perspective of the client programmer and the person maintaining the code;
  1. Watch out for "giant object syndrome."
  1. If you must do something ugly, at least localize the ugliness inside a class.
  1. If you must do something nonportable, make an abstraction for that service and localize it within a class.
  1. Objects should not simple hold some data.
  1. Choose composition first when creating new classes from existing classes;
  1. Use inheritance and method overriding to express differences in behavior, and fields to express variations in state.
  1. Watch out for variance;
  1. Watch out for limitation during inheritance.
  1. Using design patterns to eliminate "naked functionality.";
  1. Watch out for "analysis paralysis.";
  1. When you think you've got a good analysis, design, or implementation, do a walkthrough.

 

Implementation

  1. In general, follow the Microsoft coding conventions.
  1. Whatever coding style you use, it really does make a difference if your team (and even better, your company) standardizes on it.
  1. Follow standard capitalization rules.
  1. Don't create your own "decorated" private data member names;
  1. Follow a "canonical form" when creating a class for general-purpose use.
  1. Sometimes you need to inherit in order to access protected members of the base class.
  1. If two classes are associated with each other in some functional way (such as containers and iterators), try to make one an inner class of the other;
  1. Anytime you notice classes that appear to have high coupling with each other, consider the coding and maintenance improvements you might get by using inner classes;
  1. Don't fall prey to premature optimization;
  1. Keep scopes as small as possible so the visibility and lifetime of your objects are as small as possible.
  1. Use the containers in the .NET Framework SDK.
  1. For a program to be robust, each component must be robust.
  1. Prefer compile-time errors to run-time errors 尽可能地将错误在编译阶段解决;
  1. Watch for long method definitions;
  1. Keep things as "private as possible"; 应尽量少暴露外部接口给用户
  1. Use comments liberally, and use the comment-documentation syntax product your program documentation.
  1. Avoid using "magic numbers"; 应该使用常量来代替
  1. When creating constructors, consider exceptions.
  1. If your class requires any cleanup when the client programmer is finished with the object, make your class implement IDisposable interface;
  1. When you are creating a fixed-size container of objects, transfer them to an array;
  1. Choose interfaces over abstract classes;
  1. Inside constructors, do only what is necessary to set the object into the proper state;
  1. Watch out for accidental overloading;
  1. Watch out for premature optimization;
  1. Remember that code is read much more than it is written;

 

posted @ 2004-12-21 18:47 jeseeqing 阅读(2140) 评论(10) 编辑

2004年12月20日

摘要: Microsoft Encarta Reference Library体验        早就听说过Encarta的大名,直到我将这个大块头买回来,总共5张光盘。Encarta在金山词霸的翻译为“微软的电子百科全书”,安装完后总共占用了我硬盘1.96G的空间,加上其它的总共需要2G多。 ...阅读全文

posted @ 2004-12-20 10:10 jeseeqing 阅读(4609) 评论(8) 编辑

2004年12月17日

摘要: dudu,博客圆的人气已经越来越旺了,很多领域的专家都在上面建立了自已的blog。我建议dudu在博客圆中建立一个wiki资源区域,通过发挥各自的特长,建立一个协作的写作中心。我的想法具体如下: 1、内容可以类似于wiki大百科的形式或者CSDN的形式,或者博客圆自已的形式; 2、可以采用开源项目flexwiki来构建这个环境; 3、具体内容可以以专题的形式来组织,如OO技术、XP开发、开源技术,...阅读全文

posted @ 2004-12-17 11:12 jeseeqing 阅读(2488) 评论(20) 编辑

2004年11月25日

摘要:      来深圳二年了,我的软件开发技术在深圳飞越,我的理想在深圳慢慢地变得可以预见。在深圳有着太多的像我这样的求索者,带着自已的理想来到了这片热土,为这片土地奉献着自已仅有的一点微薄之力。     在深圳,让我知道了坐公交车原来可以享受星级的服务;     在深圳,让...阅读全文

posted @ 2004-11-25 10:29 jeseeqing 阅读(1418) 评论(7) 编辑

2004年11月23日

摘要: 在我的上一片文章中,我贴出了与帐号相关的ADHelper类,以下贴出了与Exchange部分操作相关的ExchangeHelper类 using System;using System.ComponentModel.Design;using System.DirectoryServices;using System.Collections;using...阅读全文

posted @ 2004-11-23 12:04 jeseeqing 阅读(4729) 评论(24) 编辑

摘要: 由于Exchange与AD实现了无缝的集成,所以某些对Exchange的开发实际上也是对AD的开发,以下是我在开发过程中的ADHelper类,主要完成AD操作的某些基本功能 在博客堂的Think Different and Think More的blog的文章http://blog.joycode.com/liuhuimiao/articles/20946.aspx 中有一个ADHelper类,另...阅读全文

posted @ 2004-11-23 11:45 jeseeqing 阅读(3183) 评论(3) 编辑