软件需求分析与设计-简单实现_第1页
软件需求分析与设计-简单实现_第2页
软件需求分析与设计-简单实现_第3页
软件需求分析与设计-简单实现_第4页
软件需求分析与设计-简单实现_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、软件需求分析与设计简单实现2022-7-22简单实现 主要内容 对可见性进行设计 将设计映射为代码 测试驱动开发和重构2022-7-23可见性设计 对象之间的可见性 为了使发送者对象能够向接收者对象发送消息,发送者必须具有接受者的可见性 可见性 是对象看到或者引用其他对象的能力 与范围有关 四种类型 属性可见性B是A的属性 参数可见性B是A中方法的参数 局部可见性B是A中方法的局部对象不是参数 全局可见性B具有某种方法的全局可见性2022-7-24从Register到ProductCatalog的可见性是必须的: RegisterenterItem(itemID, quantity): Pro

2、ductCatalogdesc = getProductDesc( itemID )public void enterItem( itemID, qty ) . desc = catalog.getProductDesc(itemID) .class Register . private ProductCatalog catalog; .2022-7-25属性可见性: RegisterenterItem(itemID, quantity): ProductCatalogdesc = getProductDesc ( itemID )public void enterItem (itemID,

3、qty) . desc = catalog.getProductDesc(itemID) .class Register . private ProductCatalog catalog ; .2022-7-26参数可见性2: makeLineItem(desc, qty)enterItem(id, qty)1: desc = getProductDesc(id)2.1: create(desc, qty):Register:Sale:ProductCatalogsl : SalesLineItemmakeLineItem(ProductDescription desc , int qty)

4、. sl = new SalesLineItem (desc, qty); .2022-7-27属性可见性的参数2: makeLineItem(desc, qty)enterItem(id, qty)2: desc = getProductDesc(id)2.1: create(desc, qty):Register:Sale:ProductCatalogsl : SalesLineItem/ initializing method (e.g., a Java constructor)SalesLineItem(ProductDescription desc, int qty) .descri

5、ption = desc; / parameter to attribute visibility.2022-7-28局部可见性: RegisterenterItem(itemID, quantity): ProductCatalogdesc = getProductDesc( itemID )enterItem(id, qty) ./ local visibility via assignment of returning objectProductDescription desc = catalog.getProductDes(id);.实现局部可见性的两种方式:创立新的局部实例并将其分配

6、给局部变量将方法调用返回的对象分配给局部变量2022-7-29将设计映射为代码 将设计映射为代码 类和接口的定义 方法的定义 由 DCD创立类的定义 DCD描述了类或接口的名称、超类、操作的特征标记以及类的属性,可以从类图直接生成类的定义 从交互图创立方法 交互图中的一系列消息可以转化为方法定义中的一系列语句2022-7-210Java中的SaleLineItempublic class SalesLineItemprivate int quantity;private ProductDescription description;public SalesLineItem(ProductDes

7、cription desc, int qty) . public Money getSubtotal() . SalesLineItemquantity : IntegergetSubtotal() : MoneyProductDescription description : Textprice : MoneyitemID : ItemID.1description2022-7-211enterItem交互图2: makeLineItem(desc, qty)enterItem(id, qty)1: desc = getProductDesc(id)2.1: create(desc, qty

8、)1.1: desc = get(id):Register:Sale:ProductCatalogsl: SalesLineItemlineItems : List: Map2.2: add(sl)2022-7-212Register类ProductCatalog.getProductDesc(.)SaleisComplete : Booleantime : DateTimebecomeComplete()makeLineItem(.)makePayment(.)getTotal()Register.endSale()enterItem(id: ItemID, qty : Integer)ma

9、keNewSale()makePayment(cashTendered : Money)public class Registerprivate ProductCatalog catalog;private Sale currentSale;public Register(ProductCatalog pc) .public void endSale() .public void enterItem(ItemID id, int qty) .public void makeNewSale() .public void makePayment(Money cashTendered) .11cat

10、alogcurrentSale2022-7-213enterItem方法2: makeLineItem (desc, qty)enterItem(id, qty)1: desc := getProductDescription (id):Register:Sale:ProductCatalog ProductDescription desc = catalog.ProductDescription (id); currentSale .makeLineItem (desc, qty);2022-7-214代码中的集合类SalesLineItemquantity : IntegergetSubt

11、otal()1.*SaleisComplete : Booleantime : DateTimebecomeComplete()makeLineItem()makePayment()getTtotal()public class Sale.private List lineItems = new ArrayList();A collection class is necessary to maintain attribute visibility to all the SalesLineItems.lineItems2022-7-215定义Sale.makeLineItem方法 lineIte

12、ms.add( new SalesLineItem (desc, qty) );2: makeLineItem(desc, qty)enterItem(id, qty)2.1: create(desc, qty):Register:Salesl: SalesLineItemlineItems :List2.2: add(sl)2022-7-216实现和测试类的可能顺序SalesLineItemquantity : IntegergetSubtotal()ProductCatalog.getProductDesc (.)ProductDescriptiondescription : Textpr

13、ice : MoneyitemID : ItemID.Storeaddress : Addressname : TextaddSale(.)Paymentamount : Money.1.*1.*Register.endSale()enterItem(.)makeNewSale ()makePayment (.)SaleisComplete : Booleantime : DateTimebecomeComplete ()makeLineItem (.)makePayment (.)getTotal().111111*1234567转换后代码2022-7-217测试驱动开发和重构 测试驱动开发

14、TDD) 是迭代和敏捷XP方法提倡的优秀实践 首先编写测试,然后编写预计要测试的代码 优点 能够保证编写单元测试 使程序员获得满足感从而更始终如一地坚持编写测试 有助于澄清接口和行为的细节 可证明、可再现、自动的验证 改变事物的信息2022-7-218测试驱动开发和重构 重构 是重写和重新构建已有代码的结构化和规律性方法,但不会改变已有代码的外在行为,而是采用一系列少量转换的步骤,并且每一步都结合了重新执行的测试 重构的本质是一次实行一小步保存行为的转换 每次重构都是小量的 目标 去除冗余代码 改变清晰度 使过长的方法变的较短 去除硬编码字面常量2022-7-219测试驱动开发和重构 重构 坏

15、味代码 冗余的代码 大型方法 具有大量代码的类 明显相似的子类 在设计中很少使用或没有使用接口 许多对象之间有很多的耦合度 包含大量其他的垃圾代码2022-7-220重构样例重构描述提炼方法(Extract Method)将较长的方法转换为较短的方法,其中将原有方法中的部分内容分解外私有的帮助者方法(helper method)提炼常量( Extract Constant )使用常量变量(constant variable)替换字面常量引入解释变量(提炼局部变量的特化)将表达式的部分或完整结果置入临时变量,该变量的名字应该能购说明其目的使用工厂方法代替构造器调用例如在Java中调用创建对象的帮

16、助者方法(helper method)来代替对新操作和构造器的调用(隐藏细节)2022-7-221重构之前的takeTurn方法public class Playerprivate Piece piece;private Board board;private Die dice;/ public void takeTurn()/ roll diceint rollTotal = 0;for (int i = 0; i dice.length; i+)dicei.roll();rollTotal += dicei.getFaceValue();Square newLoc = board.getS

17、quare(piece.getLocation(), rollTotal);piece.setLocation(newLoc); / end of class2022-7-222提炼方法重构之后的代码public class Playerprivate Piece piece;private Board board;private Die dice;/ public void takeTurn()/ the refactored helper methodint rollTotal = rollDice();Square newLoc = board.getSquare(piece.getLo

18、cation(), rollTotal);piece.setLocation(newLoc);private int rollDice()int rollTotal = 0;for (int i = 0; i dice.length; i+)dicei.roll();rollTotal += dicei.getFaceValue();return rollTotal; / end of class2022-7-223引入解释变量之前/ good method name, but the logic of the body is not clearboolean isLeapYear( int year )return( ( ( year % 400 ) = 0 ) | ( ( ( year % 4 ) = 0 ) & ( ( y

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论