2006/11 27
由于sugarOS考虑了PHP4的兼容性,虽然代码架构中在设计模式上表现不错,但由于缺少很多的面向对象关键字,比如(public,private abstract)等,也缺少一些面向对象的功能,如(interface)等。这在很大程度上影响了代码的可阅读性以及限制了一些设计模式的使用。
比如如下代码段:
[PHP]
class DBManager
{
var $helper;
var $tableName;
var $database = null;
var $dieOnError = false;
var $encode = true;
var $query_time = 0;
var $lastmysqlrow = -1;
var $last_error = '';
var $lastResult = array ();
function DBManager()
{

global $sugar_config;

$my_db_helper = 'MysqlHelper';
if ($sugar_config['dbconfig']['db_type'] == "oci8")
{

}
if ($sugar_config['dbconfig']['db_type'] == "mssql")
{
$my_db_helper = 'MssqlHelper';
}
static $helper;
if (empty ($helper))
{
$helper = new $my_db_helper ();
}
$this->helper = $helper;
}

function & getInstance($instanceName = '')
{
global $sugar_config;
static $count;
static $old_count;
global $dbinstances;

$instanceName = 'db';
$config = $sugar_config['dbconfig'];

if (!isset ($dbinstances))
{
$dbinstances = array ();
}

if (!isset ($dbinstances[$instanceName]))
{

$my_db_manager = 'MysqlManager';
if ($config['db_type'] == "oci8")
{

}
else
if ($config['db_type'] == "mssql")
{
$my_db_manager = 'MssqlManager';
}
if (!empty ($config['db_manager']))
{
$my_db_manager = $config['db_manager'];
}

DBManagerFactory :: load_db_manager_class($my_db_manager);
$dbinstances[$instanceName] = new $my_db_manager ();
$dbinstances[$instanceName]->connect($config, true);
$dbinstances[$instanceName]->count_id = $count;
$dbinstances[$instanceName]->references = 0;
$dbinstances[$instanceName]->helper->db = $dbinstances[$instanceName];

}
else
{
$old_count++;
$dbinstances[$instanceName]->references = $old_count;

}

return $dbinstances[$instanceName];
}
//MORE
}
[/PHP]
DBmanager是一个抽象类,也是一个单例类,故DBmanager类声明应该是abstract class, 构造函数好像不该出现或者是private,getInstance函数也该是static等等,这样,对于设计模式的理解更方便些。
我准备对其重构了,不是为了使用或者速度,仅仅是把代码改一改,使它更象一个面向对象的系统。
当然,一开始改,PHP4支持肯定就挂掉了。
Defined tags for this entry: , , , ,

Posted by rollenc

Not modified
2006/08 27
面向对象模式小结









原文
翻译

The object-oriented paradigm (Figure 3-23) is summarized in the following.
• The real world consists of concrete and conceptual entities, including things,
relationships, and occurrences that have a purpose, structural characteristics,
and behavioral characteristics.
• The first level of abstraction within the object-oriented paradigm consists of
representational constructs of concrete and conceptual entities.
– Objects are abstracted entities that encapsulate state and behavior.
– Links are abstracted relationships among objects.
– Scenarios are abstracted message exchanges among objects.
• The second level of abstraction within the object-oriented paradigm consists of a set of representational constructs.
– Classes are descriptions of a set of objects with common attributes, operation implementations, semantics, associations, and interactions.
– Types are descriptions of a set of objects with common attributes, operation interfaces, semantics, associations, and interactions.
– Associations are descriptions of a set of links with common attributes, operation implementations, semantics, associations, and interactions.
– Interactions are descriptions of a set of scenarios with common message exchange sequences, classes, and associations.
• Objects may be combined to build larger or more involved components. This has become known as the component-oriented paradigm.

面向对象模式小结如下:
•现实世界由具体和抽象的实体组成,包括事物,关系,事件,具有一定用途和结构特性。
• 第一层抽象包括对有代表性的具体和抽象实体进行建模。
– 对象是包含状态何行为的抽象实体
– 关联是对象之间的关系。
–活动是对象间的抽象信息交流。
•第二层抽象包括对一组有代表性的结果进行建模。
– 类是对具有相同属性,操作实现,语义,关联和交互的一组对象的描述。
– 类型是对具有相同属性,操作接口,语义,关联和交互的一组对象的描述。
– 联系是对具有相同属性,操作实现,语义,关联和交互的一组关联的描述。
– 交互是对具有相同消息,交换顺序,类和关系的一组活动的描述。
•对象可以被组合成更大更复杂的组件,这在组件开发模式用经常涉及。



摘自:
UML in a Nutshell (66-67)
by Sinan Si Alhir
Copyright © 1998 O’Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472.

Defined tags for this entry: , ,

Posted by rollenc

Not modified
2006/08 18
Table 8-1. Comparing sequence and communication diagrams
表8-1. 时间图与交互图比较

Feature
特征

Sequence diagrams
时间图

Communication diagrams
交互图

The result
比较结果

Shows participants effectively

有效的显示参与者

Participants are mostly arranged along the top of page, unless the drop-box participant creation notation is used. It is easy to gather the participants involved in a particular interaction.

如果没有使用构建符号时,参与者被排列在图顶层。很容易知道所有有联系的参与者。

Participants as well as links are the focus, so they are shown clearly as rectangles.

参与者被连成链状,在小范围区域内既可清楚的知道参与者间的联系。

Communication diagrams barely win. Although both types of diagram can show participants as effectively as each other, it can be argued that communication diagrams have the edge since participants are one of their main focuses.

交互图有些许优势,虽然两者都能有效的显示参与者间的关系,但交互图是以参与者为中心的。

Showing the links between participants

显示参与者间的联系

Links are implied. If a message is passed from one participant to another, then it is implied that a link must exist between those participants.

联系很隐晦。如果在两个参与者间有消息传递,即暗指两个参与者间有联系。

Explicitly shows the links between participants. In fact, this is the primary purpose of these types of diagram.

很清楚的显示了参与者间的联系,事实上,交互图的目的就是为了描述这种联系。

Communication diagrams win because they explicitly and clearly show the links between participants.

交互图有优势,因为它可以清楚明白的显示参与者间的关系。

Showing message signatures

显示消息

Message signatures can be fully described.

消息能够被完整的描述。

Message signatures can be fully described.

消息能够被完整的描述。

Draw! Both types of diagram can show messages as effectively as each other.

尽管去画,两种图都是有效的。

Supports parallel messages

显示并行信息

With the introduction of sequence fragments, sequence diagrams are much better.

使用时间小片断可以很好的显示并行信息。

Shown using the number-letter notation on message sequences.

使用数字-字母符号可以显示并行信息

Draw! Both types of diagram show parallel messages equally well.

尽管去画,两种图都是有效的。

Supports asynchronous messages (fire and forget)

支持异步信息

Achieved using the asynchronous arrow.

使用异步箭头完成

Communication diagrams have no concept of the asynchronous message since its focus is not on message ordering.

交互图并不关心异步信息传递,在交互图中没有异步的概念。

Sequence diagrams are a clear winner here because they explicitly support asynchronous messages.

时间图有明显优势,非常好的支持了异步信息。

Easy to read message ordering

信息顺序表达

This is a sequence diagram's forté. Sequence diagrams clearly show message ordering using the vertical placement of messages down the diagram's page.

这是一个时间图的特点。 时间图明确显示采用垂直图形清楚的现实了信息的顺序。

Shown using the number-point-nested notation.

使用数字字母组合符号显示。

Sequence diagrams are a clear winner here since they really show off message ordering clearly and effectively.

时间图清楚明白的显示了消息的顺序。

Easy to create and maintain the diagram

容易建立和维护

Creating a sequence diagram is fairly simple. However, maintaining sequence diagrams can be a nightmare unless a helpful UML tool is being used.

建立时间图是容易的,但是没有好的UML工具的话,维护时间图将是个恶梦。

Communication diagrams are simple enough to create; however, maintenance, especially if message numbering needs to be changed, still ideally needs the support of a helpful UML tool.

交互图很容易建立,但如果涉及到信息编号的话还是需要借助于一个好用的UML工具。

This is a difficult one to judge and is largely based on personal preference. However, communication diagrams do have the edge on the ease-of-maintenance stakes.

根据个人喜好,两种图优势表现不同。

原文来源:
Learning UML 2.0
By Kim Hamilton, Russell Miles
...............................................
Publisher: O'Reilly
Pub Date: April 2006
Print ISBN-10: 0-596-00982-8
Print ISBN-13: 978-0-59-600982-3
Pages: 286
Defined tags for this entry: , , , ,

Posted by rollenc

Last modified on 2007-04-27 15:38