文档介绍:面向对象分析和设计讲座设计与代码映射
第一页,共55页
内容
可见性
设计类图
代码映射
测试驱动开发与代码重构
*
*
第二页,共55页
1. 对可见性进行设计
第三页,共55页
确定四种可见性
对设置可见性进行设计
*
*
第四页,共55页
可见性Visibility 是对象看到或引用其它对象的能力
为了使发送者对象能够向接收者对象发送消息,发送者必须具有接收者的可见性,即发送者必须拥有对接收者对象的某种引用或指针
*
*
第五页,共55页
: Register
enterItem
(itemID, quantity)
: ProductCatalog
spec := getSpecification( itemID )
{
public void enterItem( itemID, qty )
{
...
spec = (itemID)
...
}
}
class Register
{
...
private ProductCatalog catalog;
...
}
*
*
第六页,共55页
实现对象A到对象B的可见性通常有四种方式:
属性可见性Attribute Visibility: B是A的属性
参数可见性Parameter visibility: B是A方法中的参数
局部可见性Local Visibility: B是A中方法的局部对象(不是参数)
全局可见性Global Visibility: B具有某种方式的全局可见性
为了使对象A能够向对象B发送消息,对于A而言,B必须是可见的
*
*
第七页,共55页
(1)属性可见性
这是一种相对持久地可见性
: Register
enterItem
(itemID, quantity)
: ProductCatalog
spec := getSpecification( itemID )
{
public void enterItem(itemID, qty)
{
...
spec = (itemID)
...
}
}
class Register
{
...
private ProductCatalog catalog;
...
}
*
*
第八页,共55页
(2)参数可见性
这是一种相对暂时的可见性
2: makeLineItem(spec, qty)
enterItem(id, qty)
1: spec := getSpecification(id)
: create(spec, qty)
:Register
:Sale
:Product
Catalog
sl : SalesLineItem
{
makeLineItem(ProductSpecification spec, int qty)
{
...
sl = new SalesLineItem(spec, qty);
...
}
}
*
*
第九页,共55页
将参数可见性转化为属性可见性十分常见
2: makeLineItem(spec, qty)
enterItem(id, qty)
2: spec := getSpecification(id)
: create(spec, qty)
:Register
:Sale
:Product
Catalog
sl : SalesLineItem
// initializing method (., a Java constructor)
{
SalesLineItem(ProductSpecification spec, int qty)
{
...
productSpec = spec; // parameter to attribute visibility
...
}
}
*
*
第十页,共55页