转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Simple Programming Tip #1 by Charlie Calvert         ★★★★

Simple Programming Tip #1 by Charlie Calvert

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2006 更新时间:2009/4/23 18:38:06
add a totally new type of functionality to your container. For instance, suppose you wanted to write some code that reported on the Deep Space Network (DSN) that is used to send commands and communicate with the Voyager spacecraft.

The worst case scenario would occur if a programmer starting adding code of this type to the original visual container:

public class MyVisualContainer
{
	void _SortByPlanet_actionPerformed(ActionEvent e)		
	void _SortBySpacecraft_actionPerformed(ActionEvent e)
	void SortByPlanet()
	void SortBySpacecraft()
	void SortByPlanetAndSpaceCraft_actionPerformed(ActionEvent e)
	void DSNCommand_actionPerformed(ActionEvent e)
	void DSNTransmission_actionPerformed(ActionEvent e)
	void SortDSNCommunicationsByCommand()
	void SortDSNCommunicationsByTransimmision()
}

Note the two methods at the end of the new version of the MyVisualContainer class. This poor over wrought class is now forced to take on a third (or is it a fourth?) responsibility! And as the program grows, yet more tasks will be assigned to MyVisualContainer. Pity the poor programmer who comes on board to sort all this out.

The obvious solution is to create three class: MyVisualContainer, MyVoyagerParser, and MyDSNParser. A new programmer brought on to this project could immediately find the code that is associated with the DSN part of the project, and could test and debug the code via an isolated set of routines.

The Tricky Bits

In all fairness, I should point out that there are some difficulties with the technique described here. In particular, to promote reuse, one should thoroughly separate the code in the working class from the visual programming code.

Suppose the Sort by Planet class needed to create a list of strings to be displayed in a list box. If the list box is in the visual container, how will the MyVoyagerParser class access it?

The worst solution to this problem is to pass in a copy of the list box itself. Instead, you should pass in the list that will be displayed in the list box. For Delphi and C++Builder programmers, that means that you need to pass in a TStrings object. Java programmers would want to pass in a DefaultListModel or perhaps an array of strings.

Other methods in the MyVoyagerParser class might be designed to return a string or integer that can be assigned to an edit control or label by the MyVisualContainer class. In other cases, you may need to come up with custom data structures. Languages like Java that support model view architectures make this part of your task easy.

In practice, I enjoy writing code that makes it possible for my working classes to share data with my visual containers. In most cases, solutions are easy to find. The results are easily reusable classes that fit together with clock like precision.

Conclusion

In this article you have learned one simple technique you can use to help you write code that is easy to maintain, and easy to reuse. The points shared in this article may seem obvious, but ironically, it is often the most intelligent programmers who are most prone to make these kinds of errors. A "natural" programmer with a great memory can easily sort out a huge amorphous chunk of code. For instance, they can cope with the class beginning to take shape in the last version of MyVisualContainer. Even if we added fifty more methods to the class, and had it take on five or six new roles, still, a really sharp witted programmer could sort it all out. They could make it all work, and they could find all the bugs in it and ship it so that it ran cleanly.

However, not even the smart programmer would be able to solve the problems that arose when it was time to morph the code so that it fit a new type of project. For instance, I''''ve seen some very smart programmers resort to horrendous hacks when trying to port Windows code to Linux, standard Delphi or C++ code to the .NET platform, or Delphi or C++ code to Java. In many cases, the primary problem was simply that they were working with visual containers that were hopelessly overburdened. At times like that, even the best programmers could benefit from the simple tip covered in this article: Never place a substantial body of code in a visual container! Instead write code in separate classes designed for a single, simple purpose.


Add or View comments on this article

NOTE: The views and information expressed in this document represent those of its author(s) who are solely responsible for its content. Borland does not make or give any representation or warranty with respect to such content.

Article ID: 30011   Publish Date: May 01, 2003  Last Modified: May 01, 2003

上一页  [1] [2] 


[系统软件]什么是Extreme Programming(极限编程,简称XP)  [Delphi程序]Why Pascal is Not My Favourite Programming Lan…
[VB.NET程序]Socket Programming with VB  [VB.NET程序]DAO Advanced Programming
[Web开发]Javascript Tip(1) 操作剪贴板  [MySql]learn linux programming with arden:00 铺垫
[MySql]Linux程式设计入门 - socket/inetd programming  [电脑技术]Programming in Lua翻译--Expressions
[电脑技术]Create a simple Delphi Expert(创建一个简单的De…  [Sql Server]使用sql的一些tip
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台