编辑: star薰衣草 2019-10-05
第8讲 设计与代码映射 内容 可见性设计类图代码映射测试驱动开发与代码重构 1.

对可见性进行设计 1.1目标 确定四种可见性对设置可见性进行设计 1.2可见性 可见性Visibility 是对象看到或引用其它对象的能力为了使发送者对象能够向接收者对象发送消息,发送者必须具有接收者的可见性,即发送者必须拥有对接收者对象的某种引用或指针 : Register enterItem (itemID, quantity) : ProductCatalog spec := getSpecification( itemID ) { public void enterItem( itemID, qty ) { ... spec = catalog.getSpecification(itemID) ... } } class Register { ... private ProductCatalog catalog;

... } 1.3四种可见性 实现对象A到对象B的可见性通常有四种方式:属性可见性Attribute Visibility: B是A的属性参数可见性Parameter visibility: B是A方法中的参数局部可见性Local Visibility: B是A中方法的局部对象(不是参数)全局可见性Global Visibility: B具有某种方式的全局可见性为了使对象A能够向对象B发送消息,对于A而言,B必须是可见的 (1)属性可见性 这是一种相对持久地可见性 : Register enterItem (itemID, quantity) : ProductCatalog spec := getSpecification( itemID ) { public void enterItem(itemID, qty) { ... spec = catalog.getSpecification(itemID) ... } } class Register { ... private ProductCatalog catalog;

... } (2)参数可见性 这是一种相对暂时的可见性 2: makeLineItem(spec, qty) enterItem(id, qty) 1: spec := getSpecification(id) 2.1: create(spec, qty) :Register :Sale :Product Catalog sl : SalesLineItem { makeLineItem(ProductSpecification spec, int qty) { ... sl = new SalesLineItem(spec, qty);

... } } 将参数可见性转化为属性可见性十分常见 2: makeLineItem(spec, qty) enterItem(id, qty) 2: spec := getSpecification(id) 2.1: create(spec, qty) :Register :Sale :Product Catalog sl : SalesLineItem // initializing method (e.g., a Java constructor) { SalesLineItem(ProductSpecification spec, int qty) { ... productSpec = spec;

// parameter to attribute visibility ... } } (3)局部可见性 实现局部可见性的两种常见方式:创建新的局部实例并将其分配给局部变量将方法调用返回的对象分配给局部变量 : Register enterItem (itemID, quantity) : ProductCatalog spec := getSpecification( itemID ) { enterItem(id, qty) { ... // local visibility via assignment of returning object ProductSpecification spec = catalog.getSpecification(id);

... } } (4)全局可见性 这是一种相对持久的可见性将实例分配给全局变量,这在某些语言如C++中是可能的 1.4采用UML表示可见性 :A :B 1: msg() :C 2: msg() :D 3: msg() :E 4: msg() is used for attribute visibility 2.生成设计类图 2.1引言 交互图完成后,有可能来定义软件类(接口)的描述设计类图(Design Class Diagrams,DCD) 2.2何时生成DCD 实践中,交互图经常与DCD并行创建为了更清楚地说明DCD,我们选择了在交互图之后介绍DCD 2.3DCD例子 Register enterItem(...) Sale date isComplete : Boolean time makeLineItem(...) Captures Navigability

1 1 Three section box for class definition. methods;

there are parameters, but unspecified type information 2.4 DCD 和UP 术语 设计类图描述了应用中的软件类和接口类,关联和属性接口,包括操作和常量方法属性类型信息可见性依赖UP并没有专门的 设计类图 UP定义了设计模型,包括交互,包和类图.在UP设计模型中的类图包含 设计类 2.5领域模型 vs. 设计模型类 Register ... endSale() enterItem(...) makePayment(...) Sale date isComplete : Boolean time makeLineItem(...) Captures Register Sale date isComplete : Boolean time Captures software class

1 1

1 1 Domain Model Design Model Concept;

conceptual class 2.6创建NextGen POS DCD 确定软件类并展示它们检查交互图并列出提到的所有类 Payment SalesLineItem Store ProductSpecification ProductCatalog Sale Register 给这些类画类图,列出通过领域模型中识别出来,并在设计中采用的属性 The Cashier is not present in the design since for the current iteration it is not necessary to model it in software. 添加方法名通过分析交互图,每一个类具有的方法可以确定 :Register :Sale 2: makeLineItem(spec, qty) Sale ... makeLineItem(...) SalesLineItem - quantity + getSubtotal() ProductCatalog ... + getSpecification(...) ProductSpecification - description - price - itemID ... Store - address - name + addSale(...) Payment - amount ... Register ... + endSale() + enterItem(...) + makeNewSale() + makePayment(...) Sale - date - isComplete - time + becomeComplete() + makeLineItem(...) + makePayment(...) + getTotal() 2.7方法的命名 一些注意点create 消息的解释访问方法多对象消息与语言无关的语法 (1)方法名-create 每一种语言有各自特定的实例化instantiation或者初始化initialization的方法在C++, 它包含了通过跟随一个构造函数的new操作进行自动内存分配在Java中, 它包含了对new 运算符的调用,后面跟构造函数调用 (2)Accessing 方法 获取或者设置属性我们可以将所有属性定义为私有,并对它们定义accessor和mutator这些方法一般在类图中不显示- (3)多对象 给多对象的消息一般解释为发送给一个container/collection 对象,例如 Java Map, a C++ map 或者Smalltalk Dictionary 1: spec := getSpecification(id) 1.1: spec := find(id) :Product Catalog :Product Specification The find message is to the container object, not to a ProductSpecification. 2.8增加更多的类型信息 属性,方法参数和返回值的类型可以有选择的显示什么时候显示? 取决于目的: Code Generation or only Read SalesLineItem quantity : Integer getSubtotal() : Money ProductCatalog ... getSpecification(id: ItemID) : ProductSpecification ProductSpecification description : Text price : Money itemID : ItemID ... Store address : Address name : Text addSale(s : Sale) Payment amount : Money ... Register ... endSale() enterItem(id : ItemID, qty : Integer) makeNewSale() makePayment(cashTendered : Money) Sale date : Date isComplete : Boolean time : Time becomeComplete() makeLineItem(spec : ProdSpecification , qty : Integer) makePayment(cashTendered : Money) getTotal() : Money Return type of method void;

no return value 2.9添加关联和方向 关联的每一端称为一个角色,在DCD中,角色可以用方向箭头修饰 Captures Register currentSale : Sale endSale() enterItem(...) makeNewSale() makePayment(...) Sale date isComplete time becomeComplete() makeLineItem(...) makePayment(...) getTotal() Navigability arrow indicates Register objects are connected uni-directionally to Sale objects. Absence of navigability arrow indicates no connection from Sale to Register. Register class will have an attribute pointing to a Sale object.

1 1 the currentSale attribute is often excluded, as it is implied by the navigable association from Register to Sale. 关联上的方向箭头一般解释为从源到目标类的属性可见性在OOP中, 它一般被实现为源类中有一个指向目标类对象的属性 一些需要定义从A指向B的方向性修饰的情形:A发送消息给BA创建B的实例A 需要保持与B的连接 方向性Navigability 通过交互图来识别i :Store :Register pc: ProductCatalog create() 2: create(pc) 1: create() 1.2: loadProdSpecs() :Product Specification 1.1: create() 1.2.2*: add(ps) 1.2.1*: create(id, price, description) ps: ProductSpecification SalesLineItem quantity : Integer getSubtotal() ProductCatalog ... getSpecification(...) ProductSpecification description : Text price : Money itemID : ItemID ... Store address : Address name : Text addSale(...) Payment amount : Money ... Contains 1.. * Contains 1.. * Register endSale() enterItem(...) makeNewSale() makePayment(...) Sale date : Date isComplete : Boolean time : Time becomeComplete() makeLineItem(...) makePayment(...) getTotal() Captures Houses Uses Looks-in Paid-by Describes

1 1

1 1

1 1

1 1

1 1

1 1

1 * Logs-completed

4 *

1 2.10 添加依赖关系 依赖关系表明一个元素(包括类,用例,等等)知道另外一个元素用虚线箭头表示在类图中,依赖关系可以表示非属性可见性,即:参数,全局或者局部可见性 SalesLineItem quantity : Integer getSubtotal() ProductCatalog ... getSpecification(...) ProductSpecification description : Text price : Money itemID: ItemID ... Store address : Address name : Text addSale(...) Payment amount : Money ... Contains 1.. * Contains 1.. * Register ... endSale() enterItem(...) makeNewSale() makePayment(...) Sale date : Date isComplete : Boolean time : Time becomeComplete() makeLineItem(...) makePayment(...) getTotal() Captures Houses Uses Looks-in Paid-by Describes

1 1

1 1

1 1

1 1

1 1

1 1

1 * A dependency of Register knowing about ProductSpecification. Recommended when there is parameter, global or locally declared visibility. Logs-completed

4 *

1 成员细节的表示 SampleClass classAttribute + publicAttribute - privateAttribute attributeWithVisibilityUnspecified attribute1 : type burgers : List of VeggieBurger attribute2 : type = initial value finalConstantAttribute : int =

5 { frozen } /derivedAttribute classMethod() + constructor?SampleClass(int) methodWithVisibilityUnspecified() methodReturnsSomething() : Foo abstractMethod() abstractMethod2() { abstract } // alternate + publicMethod() - privateMethod() # protectedMethod() ~ packageVisibleMethod() finalMethod() { leaf } methodWithoutSideEffects() { query } synchronizedMethod() { guarded } method1WithParms(in parm1:String, inout parm2:int) method2WithParms(parm1:String, parm2:f........

下载(注:源文件不在本站服务器,都将跳转到源网站下载)
备用下载
发帖评论
相关话题
发布一个新话题