Common Patterns in Static Design
description
Transcript of Common Patterns in Static Design
Common Patterns in Static Design
We focus on the static relationships between classes and ignore their dynamic or time-dependent behavior.
4.1 Collection Managers
This is one of the most common patterns in the static object design 。 A manager class manages a collection of objects, including the creation and removal of those objects.
1.Collection Managers
User ManagerUsers :User[*]
<<constructor>>// 构造程序UserManger()
<<get & query operations>>// 提取询问getUsers():User[*]
getUser(usename:String): UsergetUserCount():Integer
Contains(user:User):Boolean<<editing>> // 编辑修改addUser(usename:String)remouveUser(user:User)
remouveAllUsers()
Manages
( 管理)
UserUsername:String
用户 User 只能由用户管理器
UserManger 构造
2.Containers
The container only contains the objects and does not create them 。
Collection Manager is responsible for creating an object to be added to the collection.
Container accepts objects to be added into the collection.
Thus the add operation has an object as input argument. 。
1. Use Container as a servant class inside a collection manager class (delegation).
2. Derive a collection manager class from Container (inheritance)
Container
ContainerObject:Object[*]
<<constructor>>// 构造程序Container()
<<get & query opeartions>>// 提取询问getAll():Object
getCount():IntegerContains(object:Object):Boolean
<<editing>>// 编辑修改Add(object:Object)
remove(name:String)Remove(object:Object)
removeAll()
Contains容纳
Objectname:String
Container never create object.
1*
3.Self-Containing Classes, Hierarchies, and Networks
Object instance may contain other instance of the same class, leading to self-containment ,Such a recursive association yields a hierarchy or a tree 。An object class may be associated with itself 。
(a)A self-containing Container
Container
Contains ( 容纳 ) Folder
File
Contains容纳
1
*
*
11
*
(b)A directory hierarchy with folders and files
(a) A self-association with arbitary multiplicity on both
ends
Node
downup
*
1
San Francisco
Tokyo
Macao
HK
Chicago
Washington
LA
(b) A postal network.
Detailed design for the Node network
Node// 节点Name:String
upstreamNode:Node[*]// 上游节点downstreamNode:Node[*]// 下游节点
<<constructor>>// 构造程序Node(name:String)
<<get & query operations>>// 提取和询问运算getName():String// 提取名字
getUpstreamNodeCount():Integer// 提取上游节点数目getDownstreamNodeCount():Integer// 提取下游节点数目getUpstreamNodes():Node[*]// 提取所有上游节点数目
getDownstreamNodes():Node[*]// 提取所有下游节点数目getUpstreamNode(nodeName:String):Node// 提取上游节点
getDownstreamNode(nodeName:String):Node// 提取下游节点isUpstreamTo(node:Node):Boolean// 是否在输入节点的上游
continuation
<<editing>>// 编辑修改
addUpstreamNode(node:Node)// 增加上游节点
addDownstreamNode(node:Node)// 增加下游节点
removeUpstreamNode(node:Node)// 去除上游节点
removeDownstreamNode(node:Node)// 去除下游节点
removeUpstreamNode(nodeName:String)// 去除上游节点
removeDownstreamNode(nodeName:String)// 去除下游节点
removeAllNode()// 去除所有节点
4 Relationship Loops
When two classes are related by both aggregation and inheritance 。
Relationship Loop – Common with rich domain
How to identify? Seek out multiple aggregate relationships from the same base. Determine appropriate type of relationship. Backward or Forward containment. Node.
Two-tier relationship loops
A
B
A
B *
*
(A) backward containment (B) forward containment
The FolderItem-Folder relationship loop.
FolderItem
Folder Folder
FolderItem
FolderItem
Folder*
1
*
1
*1
Relationship fission breaks the loop into two parts.
A relationship loop with leaf node classes
Item
Group Circle Square
Contains
( 容纳 )
1..*
1
Relationship loops can be generalized to include leaf node classes, which are end objects in a recursive aggregate.
4.1 Relationship Fission
Relationship fission is the process of decomposing relationship loops into aggregation and inheritance relationships 。
Steps :
Start from the top aggregation classes, draw the aggregation between super and sub classes 。
Substitute superclasses with sunclasses one by one 。
Finnally, the taxonomic relationship is a simple copy of
the inheritance tree 。
4.2 Inheritance of Relationship Loops
Floder
SecuredFloderItem
SecuredFloder
FloderItem
Contains(容纳)
1*
Inheritance from a backward-containment relationship loop
4.3 Double Loops
Component
Part Attribute
Contains( 容纳)
Contains( 容纳)
* 1
1 *
A double relationship loop with both forward and backward containment.
4.4 Three Tiers Relationship Loops
A
B
C
BA
C
*
*
* *
A three-tier relationship loop with backward containment from subclass to superclass.
5. Binary Association Loops
Binary association classes often appear along with other object patterns. In addition, there may be more than one association between two classes 。
Supplier
Trade
Buyer
*
*
Examples of association class shown in UML notation.
5.1 Implementing BFC An implemenation of ageneric binary association class.
方法是把供应者和买家对象直接放到交易中,作为它的属性。
注意:图中任用可变阵列 trades 来装载多个交易。
SupplierCompany_name:String
Address:StringTrades:Trade[*]
Buyername:String
trade:Trade[*]
Tradeporduct:Stringqunity:Integer
total_price:Doubleupplier:Supplier
buyer:Buyer
1
1
*
*
5.2 Recursive Association Class
关联类可用于带有自容或递归的关联,成为递归关联类。
Part
Property
Part
Property
subPart( 下层零件 )
parent( 上层零件 )
parent( 上层零件 )
subPart( 下层零件 )*
* 11
*
*
A binary association class Property for the self-containing Part class.
6. The Handler-Body Pattern (bridge)
The essence of the handle-body pattern is the aggregation of multiple Property or Role objects 。
Person
SupplierBuyerUser
Rolehas
trade* *
1 *
A Role class is used as a servant class to the original superclass 。 If we consider that the aggregate is in effect defined by its collection of Property or Role objects, then this leads to dynamic schema.
7. Dynamic Schema By dynamic schema, we mean an object design that can classify object instances on the fly, with modifying the design or recompiling the source codes.
Part
FloatAtrribute
StringAtrribute
Atrribute Property
Categorybelongs to
belons to
1
*
* 1
1
** 1
The dynamic schema pattern
Part Category
Atrribute Property
CalalogSchemaManager
subpart*
11
*
1
1
1
1
*
belongto
belongto*
* 1*
* subCategory
1
*
A simplified Product Data Management System based on the dynamic schema pattern 。虚线框内是动态模式。 Part 和Categor 类各自形成层次状结构,各有部属零件。目录Catalog 负责管理零件,模式管理器 SchemaManger 则管理多个类型的模式。
8. Shared Object Pools (flyweight) 公用对象池中有一个集合管理器,负责管理一组数目有限对象。这些对象同时用在多个地方,使对象的属性可以很有效的被公用。
Catalog
Context
PartTool
shared objects
uses
1
* *
*
An object design using the shared object pool pattern.
Shared Object PoolsWe note that this pattern is in essence the same as the flyweight pattern. The shared objects are the flyweights and the collection manager is the flyweight factory, which create flyweights.
9. Object Model for XML
标记语言( markup language), 是指怎样在文件和文字数据中加插符号和标记的一种标种机制。
XML ( Extensible Markup Language ),是允许使用者自行定义标记。从而带有扩充能力的标记语言。
xml 衍生自一种更为普遍的标记语言,称为“标准广义标记语言。由国际标准组织认可,可让用者定义文件的类型,标记和结构。其虽广泛,但其复杂。于是在“国际网”协会的协助下,发展了 xml 。目的有几个:一是简化文件标记,二是可以让带有 xml 标记的文件在因特网上直接使用,三是使 xml 文件更为人懂。
9.1 XML Basicsxml 标记法则有如下五项:
(1) xml 文件以说明语句 <?xml…?> 起始。
(2) 文件中有三种标签( tag ):书( book ),章( chapter ),和节( section )。 元素的开始和结尾由起始标签( start tag )和终结标签( end tag )界定。
(3) 一个元素可以含有零或多个元素和文字串,要按一定的顺序排列。而且,元素之间要有正确的嵌套。
(4) 一个元素的起始标签中,可以附上一组属性。
(5) 如果在属性的植或文字数据中有个别的文字 xml 标记符号,则用下面的特殊符号代替它们。
作为值或数据出现的符号: ‘ “ < > &
换成对应的 xml 特殊符号: ' " < > &
9.2 XML Object ModelsConnect
Attrname:Stringvalue:String
Elementname:String
Texttext:String
*
1
*
1
{ 有序 }
Note that Connect is an abstract class ( as indicated by italicized name ) .
xml 合式文件的对象模型
To convert an objet model into an XML object model, one may employ the following procedure:
1. Decompose relationship loops into aggregation and inheritance relationship.
2. Tag the classes with UML sterotypes << Element>> and <<Attr>> . For classes that may contain other objects or itself, use <<Element>>. For classes containing only (name, value) pairs, or attributes of <<Element>> classes, use <<Attr>>
Object stereotype Class name Attribute
<<Element>> Maps to an element name Each attribute maps to an XML attribute
<<Attr>> Ignore The (name, value ) pair become an attribute of the parent.
The XML object model for the Catalog hierarchy<<Element>>
Catalogname:String
<<Atrr>>Attribute
name:Stringvalue:String
<<Element>>Part
name:String
1
*
1
*1
*
Using the rules, we get the sample XML document
<?xml version=“1.0”encoding=“GB2312”standalone=“yes”?>
<Catalog name=“electronics”>
<Part name=“resistorList”>
<Part name=“resistorList” partNumber=“123” resistance=“5.6 Ohm”power=“ 0.1”W/>
<Part name=“resistorList” partNumber=“323” resistance=“1.2 Ohm”power=“ 0.2”W/>
</Part>
<Part name=“resistorList”>
<Part name=“capacitorList” partNumber=“13” capacitance=“1.6mF”voltage=“ 20”V/>
<Part name=“capacitorList” partNumber=“14” capacitance=“0.1mF”voltage=“ 5”V/>
<Part name=“capacitorList” partNumber=“15” capacitance=“200mF”voltage=“ 10”V/>
</Part>
</Catalog>
9.3The Strengths and Weakness of XML
Xml can handle many cases of structured data, it is by no means a full object –oriented language.
In particular, XML :
( 1 ) xml does not provide genuine links between objects 。
( 2 ) xml cannot handle multiple parent cases (multiplicity more than one on the aggregate side) 。
( 3 ) xml does not provide object-oriented features like inheritance and methods 。
10 Case Study: ATM Systems-Part 1
10.1 Project Description
这个开发项目,目的是建立一个柜员机系统软件( ATM) 。整个 ATM 系统包括自动柜员机、中央服务器、自动柜员机系统元件、及有关的界面元件。
ATM 系统元件的工作,是处理系统中自动柜员机和银行电脑之间的事项。如查询、提款等。
对 ATM 系统软件的要求有多项,现排列如下:
( 1 )对所有使用 ATM 系统的事项,都要留有记录,且能每天作报告。
( 2 )系统要有安全措施。
( 3 )一个户口必须能够同时从多处接入作者查询或其他帐务。
( 4 )必须能兼顾未来的扩展,如增加出纳点,因特网客户机等。
在设计系统时,必须把以下限制考虑在内:
( 1 ) ATM 系统由银行协会拥有。
( 2 ) ATM 系统软件在中央服务器里运行。
( 3 )各银行自己发行提款机,并保管有关资料。一家银行发行的卡,头六个数字相同。
( 4 )所有自动柜员机有同样的程序界面。
( 5 )每一家银行的电脑有其程序界面,不一定与其他银行的相同。
10.2 State Analysis and Design
模型对象的分析和设计有两方面:静态和动态。静态分析包括认出对象类及其静态关系。动态分析则集中于对象间的相互作用和对外界面。以上列出的四条要求中,( 1 )和( 4 )直接影响着系统的静态设计。( 2 )和( 3 )则和动态设计更相关。这里要注意 ,通常涉及系统安全的,都和静态设计有关。但现在因为用户密码存放在银行电脑, ATM 系统软件只需作中继站,把有关数据传到银行电脑,故( 2 )对静态设计影响不大。
事项是指持提款卡的顾客,在柜员机所作事情的记录。所以,它是提款卡与柜员机之间的一个连接。也就是说,可以把事项看成二元关联类,并作为设计的核心对象。
Card
number:Integer
name:String
trx:Transaction[*]
Transaction
time:Date
content:String
card:Card
atm:ATM
ATM
serialNumber:Integer
lication:String
trx:Transaction[*]
*
*
事项( Transaction )是 ATM系统软件的核心设计,是一个二元关联类,把提款卡( Card )和自动柜员机( ATM )联系起来。
另加一个 ATM 管理器( ATMManager )管理所有银行资料和事项。银行资料则管理自己银行的提款卡( Card )。下图是以上核心设计的扩充。
ATM
Manager
Transaction
BankInfo
Card
ATM
manages
manages
1*1
1
*
*
*
*
ATM 系统软件的局部设计图
以照顾未来需要,如增加出纳点和因特网用户机等。可把 ATM 类换成抽象类“经济”( Agent),并由后者衍生三个子类,得到 ATM 系统软件设计总图。注意,图中的经济类也由 ATM 管理器管理。注意,图中“ Agent” 是一个抽象类。
ATM
Transttraction
Agent
Card
Internet
Cashier
BankInfoATM
Manager
1*
1*managers 1
*
managers
**
*
11. Case Study: Shared Whiteboard – Part 1
目的是建立一个公用讨论板,让联网的用户能一起讨论,并作图姓和文字标记。在本开发项目的第一阶段,先建立一个独立的“白板”;第二阶段再把这些“白板”连接起来,成为公用讨论板。
11.1 Introduction
“白板”是一个图形工具,他有一个背景图形,用户可以作图形或文字标记,覆盖在图像之上。“白板”对背景图象没有编辑能力,当能改变覆盖在上面的图形或文字,并存到一定格式的文件中。
公用讨论板是白板的扩充板,能让联网的用户对背景图象作讨论。
11.2 Functional Requirements
( 1 )公用讨论板可显示一背景图象,图像从 GIF 或JPEG格式的图象文件读进,而且不可用讨论板改变(第一阶段)。
( 2 )公用讨论板可让用户作文字或图形标记,后者包括:之线、圆、椭圆和矩形。
( 3 )利用公用讨论板,用户可增减、选择、或移动以上的文字和图形标记,也可改变标记的形式,如颜色等。
( 4 )公用讨论板可把文字和图形标记存储到CGM(computer graphics metafile)格式的文件中,并可读回该类文件,然后显示内含的标记。
( 5 )公用讨论板可形成一个组合,共看同一背景图象。用户可按一定秩序加上标记,并把标记播放到整个小组,让所有人都看到(第二阶段)。
( 6 )用户若要加入现行的合作小组,直要连到组里的任一个共用讨论板,即可。用户也可随意离开小组,而不影响其他人(第二阶段)。
( 7 )将来标记的类型可能会增加。
11.3 User Interface
用户界面设计应依循“视窗”的标准:
• 可利用鼠标来产生、改变和清除标记。
•给所有的操作提供下拉菜单。
•给常用的操作提供快速按钮。
11.4 System Attributes
( 1 )公用讨论板要求不依赖电脑平台,可在不同的操作系统上使用。
( 2 )公用讨论板将在一个安全内部实施中使用,故无需另加安全措施。
File Edit Draw Help (菜单横条)(按钮屏面) 2 red Courier Bold 16
DrawCanvas
第一阶段白板的版面设计示意图
根据以上版面设计,很容易得到以下图的对象模型。主框架有三个类复合而成:MainMenuBar 、 MainButton 和DrawCanvas 。<<Frame>>
Main
MainButtonPanel
Glyph
<<Canvas>>DrawCanvas
MainMenuBar
ImagePort
ImageButton
FontChoose
ColorChoose
LineChoose
11
11
11
11
1
1
1
1
1*
*
第一阶段白板的对象模型
下图是绘画布类的详细设计。绘画布类的构造程序以一个背景图象作输入量。显示运算包括绘画,更新和清屏。
DrawCanvasimage:Image
glyphs:ContainertheGlyph:Glyph
currentGlyphType:Integer<<constructor>>
DrawCanvas(image:Image)<<disply & editing>>
Paint(gr:Graphics)/refresh()/clera()Copy()/cut()/paste()/erase()
Shift(dx:Short, dy:Short)processMouseEvent(e:MouseEvent)
<<exernal linkage>>getGlyphsTo(imgPort:ImagePort)
sendGlyphsTo(imgPort:ImagePort)sendGlyphsFrom(file:String)
getGlyphsTo(file:String)getImageFrom(imgPort:ImagePort)sendImageTo(imgPort:ImagePort)
接下讲图符类,属性确定图符种类。Glyph
type:integerX,Y:Short[*]
line Width :Integercolor:Integertext:Stringkey:Integer
<<constructor>>Glyph(type:Integer,X:Short,Y:Short,…)
Clone():Object<<display & editing>>
draw(gr:graphics)drawBound(gr:Graphics)
intersect(x:Short,y:Short):Booleanshift(dx:Short,dy:Short)<<external linkage>>
toCGM(cgm Tool:CGMTool)
图符类的详细设计图
图符类的构造程序意图符的所有基本属性为输入变量,其他运算在下表中列出。
图符类的运算运算 作用复制( clone) 复制一个同样的图符 绘画 (draw) 把图符在绘画布上画出
画框 (drawBound) 画出图符的约束框
相交 (intesect) 检查输入坐标是否与图符相交
移位 (shift) 移动图符的位置
转成 CGM(toCGM) 把图符转换成 CGM 字节数据
已经构造了图符,则由绘画布中的绘画运算画出,下面是有关得伪码:
Void paint (Graphics gr){
对图符容器中的每一个图符,都可调用绘画运算。
如果“当前图符”有值,则调用画框运算; }
最后,图符和 CGM 数据之间的转换,由一组公用程序处理。如图 2-38 所示, CGM 类含有服务类 CGMTool ,后者一组产生 CGM 字节数据的运算。
CGM
Glyph
CGMTool1
11
*共用讨论板中 CGM共用程序的工具对象
CGM 类含有一组图符,另有一字节阵列,作为缓冲存储器。 CGM 类借用 CGM 工具,把图符转换成 CGM 数据,也可把 CGM转换成图符。 CGM 的细详细设计见图。CGM
cgmTool:CGMToolGlyphs:Container
Buffer:Byte[*]<<constructor>>
CGMCGM(glyph:Glyph[*])CGM(buffer:Byte[*])
<<editing>>add(glyphs:Glyph[*]
add(glyph:Glyph)<<external I/O>>
write(file:File)read(file:File)
要把一组图符转换成 CGM 数据,伪码列出如下:把一个图符阵列放到变量 glyphs,然后令 cgm=new CGM (glyph) ; cgm.add(glyph);// 增加一个图符
cgm,add(other_glyphs) 增加一个图符阵列
把 CGM 数据利用 cgm.write(file)写到文件中,或利用cgm.getBuffer() 把缓冲存储器的内容取出。
把 CGM 数据转换成一组图符可用:令 cgm=new CGM(buffer_with_CGM_data);
然后通过 cgm.getGlyphs(); 提取包含多个图符的图符阵列
读入文件,并把数据转换成一组图符,则有:
利用 cgm=new CGM(); 构造一个 CGM 对象,然后调用cgm.red(file); 来读取文件,最后通过 cgm.getGlyphs(); 提取包含多个图符的图符阵列。
12 Case Study : Access Control Lists - Part 1
访问控制列( ACL)通常出现在大的企业软件系统中,用来控制用户对数字资料的存取。访问控制列必须对个体用户和用户组两者作出控制。本案例探讨两个访问控制列的设计,各有自己的核心设计样式。第一个设计样式由图 2-40显示,其中有二元关联类样式。 ACL与多个用户有关,关联类为(UserAccess).
UserAccess
User GroupPrivilege
ACL
GroupAccess
contains
* *
11
*
* *
1 1*
访问空制列对象之下有多个用户和组,并有运算“是否授予特权”。该运算处理由用户发出的请求,对所要求的特权作出是否作出特权的决定。
要把访问控制列与一个文件夹单项对应起来,即可控制任意用户和组对该单项的访问。设计见图。
ACL
Folder
FolderItem *
1
1 1has
访问控制列的另一个设计见下图
ACL
UserManager
Group
Privilege
User
GroupManager
1
1
1
1 1*
**
has inner groups
含有一个正向容纳的关系环。组类包含多个用户和访问控制列,而每一个访问控制列对象,是带有特权的特殊组。注意,用户与特权之间无直接关系。该设计常用来作分类或粗略式访问。
利用图 2-42 的设计,可作多个层次的特权控制图所示。其中公司 A 和 B 合资经营,但各自保持公司专有权和商业秘密。
Company A Proprietary
Company A Trade Secrete
Company B Proprietary
Company B Trade Secrete
Joint Venture Proprietary
An example of access control by catagories using the cross-grained ACL design