Professional Documents
Culture Documents
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus
Lecture Session-13
21- 09-13
2
Objectives
Map design artifacts to code in an object-oriented language Class definitions from DCDs Methods from interaction diagrams
Implementation Model
Interactions and DCDs complete for an iteration Sufficient detail exists for code generation Implementation model includes: Source code, database definitions, JSP/XML/HTML pages, etc.
ProductDescription SalesLineItem quantity : Integer getSubtotal() : Money ... description description : Text price : Money 1 itemID : ItemID
Reference Attributes
Reference Attributes are suggested by associations and navigability in a class diagram. Note that reference attributes may be implied rather than explicit on a class diagram. You may have to ask How is this association accomplished.
Role Names
Each end of an association is a role. Reference Attributes are often suggested by role names.
SalesLineItem productSpec
Described-by productSpec
Interaction Diagrams
Interaction Diagrams and Operation Contracts are used to specify methods. They give most of the details for what the method does.
Sale Register ... endSale() enterItem(id: ItemID, qty : Integer) makeNewSale() makePayment(cashTendered : Money) currentSale 1 isComplete : Boolean time : DateTime
1: desc := getProductDescription(id)
:Product Catalog
Containers
Where an object must maintain visibility to a group of other objects, such as a group of Sales Line Items in a Sale, object-oriented languages often use an intermediate container or collection. These will be suggested by a multiplicity value greater than one on a class diagram.
Collection classes
public class Sale { ... private List lineItems = new ArrayList(); } Sale isComplete : Boolean time : DateTime lineItems becomeComplete() makeLineItem() makePayment() getTtotal() 1..* SalesLineItem quantity : Integer getSubtotal()
7
1 ProductCatalog
Sale isComplete : Boolean time : DateTime becomeComplete() makeLineItem(...) makePayment(...) getTotal() ...
5
SalesLineItem 1..* quantity : Integer getSubtotal()
*
1 ...
Iteration-2
Monopoly Iteration-2
Expand basic players moving around board scenario to handle some special square rules: Each player starts with $1500 When player lands of Go, player receives $200 When player lands on Go-To-Jail, move to Jail When player lands on Income-Tax, player pays minimum of $200 or 10% of worth
makeNewSale
enterItem(itemID, quantity)
description, total
endSale taxLineItems = getTaxes( sale ) total with taxes makeCreditPayment (credNum, expiryDate) reply = requestApproval(request) postReceivable( receivable ) postSale( sale )
Fig. 24.1
Generalisation- POS
Generalisation-Specialisation
Subclass-Associations Of Interest
Refactoring
Refactoring: changing a software system by improving its internal structure without changing its external behavior I.e., restructure code in a disciplined way Make code easier to understand and cheaper to modify Counteracts entropy of software by promoting more order Identify heavily used or time consuming code Refactoring leads to design patterns: why? Smalltalk community used refactoring to develop the Model-View-Controller and other frameworks
When to Refractor?
When you do a Code Review or Walkthrough
When you add a function Helps you to understand the code you are modifying Sometimes existing design does not allow you to easily add the feature When you need to fix a bug A bug report is a sign code needs refactoring: Why? Code was not clear enough to see the bug in the first place
Refactoring Goals
Remove duplicate code Improve clarity Shorten methods Remove hard-coded constants Move methods to another class
Summary
Mapping design to code Class definitions from DCDs Methods from interaction diagrams Analysis artifact changes Refactoring
Gang of Four
Pattern-based design was introduced into architecture and engineering in the 1950's Almost immediately, software engineers began using patterns for designing software It wasn't until a group of four researchers combined forces that pattern-based design became well-known and commonplace
Gang of Four
The gang of four (GoF) is:
Erich Gamma Richard Helm Ralph Johnson John Vlissides
They are the authors of the famous text "Design Patterns: Elements of Reusable Object-Oriented Software"
Eventually, best known solutions are collected and documented as Design Patterns.
Design Patterns are named to facilitate people using and discussing them.
Structural patterns:
Composition of classes or objects Decouple interface and implementation of classes
Behavioral patterns:
Deal with dynamic interactions among societies of objects How they distribute responsibility
Structural patterns
Assemble objects to realize new functionality Exploit flexibility of object composition at run-time Not possible with static class composition Structure patterns are concerned with how classes and objects are composed to form large structures. Different categories Adapter Bridge Composite Decorator Faade Flyweight Proxy
Adapter Pattern
Adapters are used to enable objects with different interfaces to communicate with each other. Adapter Pattern tells us how to wrap up existing classes inside a new target interface, when the need arises to reuse existing class implementations but with a different interface for the clients.
Solution
Adapter Description
Intent
Convert the interface of a class to an interface expected by the users of the class. Allows classes to work together even though they expect incompatible interfaces.
Adapter - Intent
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Wrap an existing class with a new interface. Also Known As -> Wrapper
BITS Pilani, Pilani Campus
Motivation
Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application. We can not change the library interface, since we may not have its source code. Even if we did have the source code, we probably should not change the library for each domainspecific application.
Variations in Adapters
Class Adapters Use multiple inheritance to compose classes Object Adapters Object adapters use a compositional technique to adapt one interface to another.
BITS Pilani, Pilani Campus
Motivation
Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application We can not change the library interface, since we may not have its source code Even if we did have the source code, we probably should not change the library for each domain-specific application
Motivation
Participants
Target (Shape) defines the domain-specific interface that Client uses. Client (DrawingEditor) collaborates with objects conforming to the Target interface. Adaptee (TextView) defines an existing interface that needs adapting. Adapter (TextShape) adapts the interface of Adaptee to the Target interface.
Structure
A class adapter uses multiple inheritance to adapt one interface to another: adapts Adaptee to Target by committing to a concrete Adapter class. As a consequence, a class adapter won't work when we want to adapt a class and all its subclasses.
Structure
An object adapter relies on object composition: lets a single Adapter work with many Adapteesthat is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
Collaborations
Clients call operations on the Adapter instance and Adapter delegates request to Adaptee.
BITS Pilani, Pilani Campus
Adaptability
Use the adapter when
Want to use an existing class and its interface doesnt match the one we need. (Object Adapters only) we need to use several existing subclasses, but its impractical to adapt their interface by sub classing every one. An object adapter can adapt the interface of its parent class.
Consequences
Class Adapter
Adapts Adaptee to Target by committing to a concrete Adapter class. Lets Adapter override some of the Adaptees behavior by subclassing. Introduces only one object and no additional pointer indirection is needed to get the adaptee.
Object Adapter
Lets a single adapter work with a group of adaptees such as a base class and all its sub classes. The adapter can add functioanlity to all adaptees at once. Makes it harder to override Adaptee behavior as the Adapter may not know with what Adaptee it is working with.
BITS Pilani, Pilani Campus
TaxMasterAdapter
GoodAsGoldTaxPro Adapter
Fig. 26.1