《UML和模式应用》读书笔记

初读了《UML和模式应用》收获很大,最重要的事这本书能教你了什么是OOA/D的思想,怎么认识对象,以及UML的威力。

UML作为模式语言用于设计和思考。

如何赋予对象的职责:

Doing responsibilities of an object include:
o doing something itself, such as creating an object or
doing a calculation
o initiating action in other objects
o controlling and coordinating activities in other objects


Knowing responsibilities of an object include:
o knowing about private encapsulated data
o knowing about related objects
o knowing about things it can derive or calculate

Responsibilities are implemented using methods
that either act alone or collaborate with other methods and objects

一个职责的实现或者是单独完成,或者是通过和其他对象协作来完成。

从uml交互图的创建中发掘对象职责:

Within the UML artifacts, a common context where these
responsibilities (implemented as methods) are considered is during the creation
of interaction diagrams。

GRASP九种pattern:

1、 Information Expert:分析对象知道什么--know what,即Expert Information Pattern,对象掌握了什么信息,就隐含了它应有何种职责。
2、 Creator:创建者

如果存在以下的关系:

. B aggregates A objects.
. B contains A objects.
. B records instances of A objects.
. B closely uses A objects.
. B has the initializing data that will be passed to A when it is created (thus B
is an Expert with respect to creating A).
B 就是A的一个创建者.B因此就具有创建的职责。

By expert information principle, sometimes a creator is found by looking for the class that has the initializing
data that will be passed in during creation。


3、 Low Coupling:低耦合

常见的耦合关系:

1).TypeX has an attribute (data member or instance variable) that refers to a TypeY instance, or TypeY itself.
2). A TypeX object calls on services of a TypeY object.
3). TypeX has a method that references an instance of TypeY, or TypeY itself, by any means. These typically include a parameter or local variable of type TypeY, or the object returned from a message being an instance of TypeY.
4).TypeX is a direct or indirect subclass of TypeY.

5).TypeY is an interface, and TypeX implements that interface.

Low Coupling encourages assigning a responsibility so that its placement does
not increase the coupling to such a level that it leads to the negative results that
high coupling can produce.
Low Coupling supports the design of classes that are more independent, which
reduces the impact of change. It can’t be considered in isolation from other patterns
such as Expert and High Cohesion, but rather needs to be included as one of
several design principles that influence a choice in assigning a responsibility.

4、 High Cohesion:高内聚

As a rule of thumb, a class with high cohesion has a relatively small number of
methods, with highly related functionality, and does not do too much work. It
collaborates with other objects to share the effort if the task is large.
A class with high cohesion is advantageous because it is relatively easy to maintain,
understand, and reuse. The high degree of related functionality, combined
with a small number of operations, also simplifies maintenance and enhancements.
The fine grain of highly related functionality also supports increased
reuse potential.

Bad cohesion usually begets bad coupling, and vice versa

类比于现实生活中,如果一个人承担太多职责,事情可能都办不好,很多经理不知道委任和放权,承担太多不需要自己承担的职责,结果搞得焦头烂额。要懂得委任和放权。

Modular Design:

We promote a modular design by creating methods and classes with high cohesion.
At the basic object level, modularity is achieved by designing each method
with a clear, single purpose, and grouping a related set of concerns into a class。

5、 Controller:使用controller接收和处理系统事件,Controller定义系统操作。

怎样创建 Domain Model:


Apply the following steps to create a domain model: 

a.  List the candidate conceptual classes using the Conceptual Class Category
b. List and noun phrase identification techniques related to the current
    requirements under consideration.
b. Draw them in a domain model.
c.  Add the associations necessary to record relationships for which there is a  
     need to preserve some memory (discussed in a subsequent chapter).
d.  Add the attributes necessary to fulfill the information requirements (discussed
     in a subsequent chapter).

6. Polymorphism 多态

7. Indirection

8. Pure Fabrication

9. Protected Variations

发现Conceptual Class:

conceputal class 和属性的区别:

如果一个对象不是简单的number, string, boolean, date, time

,那么它就应该设计成一个类.

1、通过Conceptual Class Category表来检查所有的可能:

Conceptual Class Category Examples
physical or tangible objects Register Airplane
specifications, designs, or descriptions
of things
ProductSpecification
FlightDescription
places Store
Airport
transactions Sale, Payment
Reservation
transaction line items SalesLineItem
roles of people Cashier
Pilot
containers of other things Store, Bin
Airplane
things in a container Item
Passenger
other computer or electro-mechanical
systems external to the system
CreditPaymentAuthorizationSystem
AirTrafficControl
abstract noun concepts Hunger
Acrophobia
organizations SalesDepartment
ObjectAirline
events Sale, Payment, Meeting
Flight, Crash, Landing
processes
(often not represented as a concept,
,but may be)
SellingAProduct
BookingASeat
rules and policies RefundPolicy
CancellationPolicy
catalogs ProductCatalog
PartsCatalog
records of finance, work, contracts,
legal matters
Receipt, Ledger, EmploymentContract
MaintenanceLog
financial instruments and services LineOfCredit
Stock
manuals, documents, reference
papers, books
DailyPriceChangeList
RepairManual


2、从用例中提取名词作为conceptual class的候选:

Main Success Scenario (or Basic Flow):
1. Customer arrives at a POS checkout with goods and/or services to purchase.
2. Cashier starts a new sale.
3. Cashier enters item identifier.
4. System records sale line item and presents item description, price, and running
total. Price calculated from a set of price rules.
Cashier repeats steps 2-3 until indicates done.
5. System presents total with taxes calculated.
6. Cashier tells Customer the total, and asks for payment.
7. Customer pays and System handles payment.
8. System logs the completed sale and sends sale and payment information to the
external Accounting (for accounting and commissions) and Inventory systems (to
update inventory).
9. System presents receipt.
10.Customer leaves with receipt and goods (if any).
Extensions (or Alternative Flows):
7a. Paying by cash:
1. Cashier enters the cash amount tendered.
135
10 - DOMAIN MODEL: VISUALIZING CONCEPTS
2. System presents the balance due, and releases the cash drawer.
3. Cashier deposits cash tendered and returns balance in cash to Customer.
4. System records the cash payment.

设计Attributes原则:

1、Keep Attributes Simple

2、Attributes should generally be data types:见primative和non-primative datatype

3、No Attributes as Foreign Keys:不要用属性作为外键来关联两个对象,应该直接引用这个对象。

primative和non-primative datatype:

non-primative:

These data type values are also known as value objects.

It is composed of separate sections.
such as: phone number, name of person
?There are operations usually associated with it, such as parsing or validation.
such as: social security number
?It has other attributes.
such as: promotional price could have a start (effective) date and end
date
?It is a quantity with a unit.
such as: payment amount has a unit of currency
?It is an abstraction of one or more types with some of these qualities.
item identifier in the sales domain is a generalization of types
such as Universal Product Code (UPC) or European Article
Number (EAN)

These data type values are also known as value objects.

设定Associations:

1、运用Common Associations List

Category Examples
A is a physical part of B Drawer — Register (or more specifically,
a POST)
Wing — Airplane
A is a logical part of B SalesLineItem — Sale
FlightLeg—FlightRoute
A is physically contained in/on B Register — Store, Item — Shelf
Passenger — Airplane
A is logically contained in B ItemDescription — Catalog
Flight— FlightSchedule
A is a description for B ItemDescription — Item
FlightDescription — Flight
A is a line item of a transaction or report B SalesLineItem — Sale
Maintenance Job — Maintenance-
Log
A is known/logged/recorded/reported/captured
in B
Sale — Register
Reservation — FlightManifest
A is a member of B Cashier — Store
Pilot — Airline
A is an organizational subunit of B Department — Store
Maintenance — Airline
A uses or manages B Cashier — Register
Pilot — Airplane
A communicates with B Customer — Cashier
Reservation Agent — Passenger
A is related to a transaction B Customer — Payment
Passenger — Ticket
A is a transaction related to another transaction
B
Payment — Sale
Reservation — Cancellation
A is next to B SalesLineItem — SalesLineItem
City— City
A is owned by B Register — Store
Plane — Airline
A is an event related to B Sale — Customer, Sale — Store
Departure — Flight

Interaction diagrams (交互图):Sequence and Collaboration Diagrams

1.Sequence:

System Sequence Diagrams

System behavior
is a description of what a system does, without explaining how it does it. One
part of that description is a system sequence diagram. Other parts include the
use cases, and system contracts (to be discussed later).

2.Collaboration:

Collaboration diagrams illustrate object interactions in a graph or network
format, in which objects can be placed anywhere on the diagram

书中强调一定要重视交互图,花费更多的时间在交互图分析上,因为创建交互图是OOA/D中一个非常creative的步骤。it will provide a thoughtful, cohesive, common starting point for inspiration during programming.

Relatively speaking, the creation of use cases, domain models, and other artifacts is easier than the assignment of responsibilities and the creation of well-designed interaction diagrams.This is because there is a larger number of subtle design principles and "degrees of freedom" that underlie a well-designed interaction diagram than
most other OOA/D artifacts. 

设计user case和domain model要比职责分配创建良好的交互图要容易些。因为后者包含了大量的微妙的设计原则和设计自由度在里面。

ADDING DETAIL WITH OPERATION CONTRACTS

Use cases are the primary mechanism in the UP to describe system behavior,
and are usually sufficient. However, sometimes a more detailed description of
system behavior has value. Contracts describe detailed system behavior in
terms of state changes to objects in the Domain Model, after a system operation
has executed.
posted @ 2011-06-12 13:03  babykick  阅读(565)  评论(0编辑  收藏  举报