动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C/C++教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ C/C++教程 ]的信息

本月文章推荐
.C++程序设计语言编程风格演变史.
.为C++程序添加文件保存加载功能.
.实战JBOSS――教你写第一个EJB.
.扑克牌的发牌程序(用伪随机数实.
.一个简易网络嗅探器的实现.
.C的学习方法.
.新手入门:C++下的引用类型.
.C语言库函数 (B类字母).
..
.在CB6下基于api函数编写串口通信.
.C语言中可变参数的用法.
.并非偏见 也驳“驳‘C语言已经死.
.C++/CLI思辨录之再谈继承.
.自动化基础概念之“COM组件与接口.
.c++面向对象的编程入门篇-----类.
.运行中程序删除自己的方法.
.Perl的经典用法.
.数据结构学习(C++)之排序.
.水滴石穿C语言之内存使用.
.C++对象布局及多态实现之动态和强.

Creating Reusable Software Libraries

发表日期:2008-3-8 |



  By Rob Tougher

-----------------------------------------------------------------------

1. IntrodUCtion
2. Making It Easy To Use
2.1 Keeping It Simple
2.2 Being Consistent
2.3 Making It Intuitive
3. Testing Thoroughly
4. Providing Detailed Error Information
5. Conclusion
1. Introduction
Software libraries provide functionality to application developers. They consist of reusable code that developers can utilize in their projects. Software libraries targeted for Linux are usually available in both binary and source code form.

A well-written software library:

is easy to use
works flawlessly
provides detailed error information
This article describes the above principles of library creation, and gives examples in C++.

Is This Article For You?
Create software libraries only when you have to. Ask yourself these questions before proceeding:

Will anyone (including you) need functionality X in future applications?
If so, does a library implementing functionality X already exist?
If no one will need the functionality you are developing, or a software library implementing it already exists, don't create a new library.


2. Making It Easy To Use
The first step in creating a software library is designing its interface. Interfaces written in procedural languages, like C, contain functions. Interfaces written in object-oriented languages, like C++ and Python, can contain both functions and classes.

Remember this motto when designing your interface:

The easier to use, the better
As a library designer, I am constantly faced with finding the right balance between functionality and ease of use. The above motto helps me resist adding too much functionality into my designs.

Stick with the following guidelines, and you'll be fine.

2.1 Keeping It Simple
The more complex a library, the harder it is to use.

Keep It Simple, Stupid
I recently encountered a C++ library that consisted of one class. This class contained 150 methods. 150 methods! The designer was most likely a C veteran using C++ - the class acted like a C module. Because this class was so complex, it was very difficult to learn.

Avoid complexity in your designs, and your interfaces will be cleaner and easier to understand.


2.2 Being Consistent
Users learn consistent interfaces more easily. After learning the rules once, they feel confident in applying those rules across all classes and methods, even if they haven't used those classes and methods before.

One example I am guilty of involves public Accessors for private variables. I sometimes do the following:

class point
{
public:
int get_x() { return m_x; }
int set_x ( int x ) { m_x = x; }

int y() { return m_y; }

private:
int m_x, m_y;
};

Do you see the problem here? For the m_x member, the public accessor is "get_x()", but for the m_y member, the public accessor is "y()". This inconsistency generates more work for the users - they have to look up the definition of each accessor before using it.

Here's another example of an inconsistent interface:

class DataBase
{
public:

recordset get_recordset ( const std::string sql );
void RunSQLQuery ( std::string query, std::string connection );

std::string connectionString() { return m_connection_string; }

long m_sError;

private:

std::string m_connection_string;
};

Can you spot its problems? I can think of at least these items:

Methods and variables are not named consistently
Two different terms, sql and query, are used to denote a SQL string
m_sError does not have a public accessor
get_recordset() does not have a connection in its argument list
Here is a revised version that solves these problems:

class database
{
public:

recordset get_recordset ( const std::string sql );
void run_sql_query ( std::string sql );

std::string connection_string() { return m_connection_string; }
long error() { return m_error; }

private:

std::string m_connection_string;
long m_error;
};

Keep your interfaces as consistent as possible - your users will find them much easier to learn.

2.3 Making It Intuitive
Design an interface how you would eXPect it to work from a user's point of view - don't design it with the internal implementation in mind.

I find that the easiest way to design an intuitive interface is to write code that will use the library before actually writing the library. This forces me to think about the library from the user's standpoint.


Let's look at an example. I was recently considering writing an encryption library based on OpenSSL. Before thinking about the library design, I wrote some code snippets:

crypto::message msg ( "My data" );
crypto::key k ( "my key" );

// blowfish algorithm
msg.encrypt ( k, crypto::blowfish );
msg.decrypt ( k, crypto::blowfish ):

// rijndael algorithm
msg.encrypt ( k, crypto::rijndael );
msg.decrypt ( k, crypto::rijndael ):

This code helped me think about how I should design the interface - it put me in the user's shoes. If I decide to implement this library, my design will flow from these initial ideas.

3. Testing Thoroughly
A software library should work flawlessly. Well not flawlessly, but as close to flawless as possible. Users of a library need to know that the library is performing its tasks correctly.

Why use a software library if it doesn't work correctly?
I test my software libraries using automated scripts. For each library, I create a corresponding application that exercises all features of the library.

For example, say I decided to develop the encryption library I introduced in the previous section. My test application would look like the following:

#include "crypto.hpp"

int main ( int argc, int argv[] )
{
//
// 1. Encrypt, decrypt, and check
// message data.
//
crypto::message msg ( "Hello there" );
crypto::key k ( "my key" );

msg.encrypt ( k, crypto::blowfish );
msg.decrypt ( k, crypto::blowfish );

if ( msg.data() != "Hello there" )
{
// Error!
}

//
// 2. Encrypt with one algorithm,
// decrypt with another, and check
// message data.
//

// etc....
}

I would occasionally run this application to make sure that my software library did not have any major errors.

4. Providing Detailed Error Information
Users need to know when a software library cannot perform its tasks correctly.

Alert the user when there is a problem
Software libraries written in C++ use exceptions to pass information to its users. Consider the following example:

#include <string>
#include <io
上一篇:C程序开发经典实例之1 人气:392
下一篇:DOS界面下通用图形编辑软件的设计 人气:590
浏览全部C/C++的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-10-10 企业网站智能管理系统(TZIMS) v6
2008-10-10 拓文asp.net网站内容管理系统 v6
2008-10-10 动网论坛PHP版 v2.0++ Build 081
2008-10-10 免费时代CMS v5.0
2008-10-10 wodig第四季中文DIGG社区 v4.1 b
2008-10-10 老Y文章管理系统 v2.2 bulid 081
2008-10-10 魔法盒动感相册 ASP+SQL版 v2.0
2008-10-10 Asoft签到管理系统 v3.0 Pack1
2008-10-10 哥特人音乐网潮流留言本 v1.1
2008-10-11 联系人分组工具 v1.1 中文破解版
2008-10-11 FaceMelter变脸 v2.0 汉化破解版
2008-10-11 PathTracker道路跟踪仪 v1.2 破解
2008-10-11 Rooms手机聊天室 v0.6.7 破解版
2008-10-11 RemoteDesktop远程桌面 v1.0 破解
2008-10-11 ProRemote远程调音台 v1.0.1 破解
2008-10-11 PicShare照片共享 v1.0.0 破解版
2008-10-11 Photogene照片编辑器 v1.5 汉化破
2008-10-11 WriteRoom共享文档 v1.0 破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号