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
|