大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> VB.Net中文教程(2) Composite样式

VB.Net中文教程(2) Composite样式

时间: 2021-07-31 作者:daque

1. 从whole-part联系谈起 回顾保守的软硬件师﹐常甚潜心于撰写步调(procedure) 来处置某些材料(data)﹐较少关怀软硬件的完全构造(architecture)。在此刻的oo软硬件中﹐把材料及其关系的步调贯串在一道﹐封装(encapsulate) 在东西之中。软硬件师在运用东西时﹐常常把东西视为黑箱(black-box) ﹐不会关怀于东西里面之详细﹔因之能潜心于东西之间的联系。软硬件师的重要处事﹐即是在乎创造东西之间的合作协作(collaboration) 联系﹐为东西安置应尽之脚色(role)﹐至于东西里面之详细﹐相反不顶要害。如许﹐可让软硬件师提防于软硬件的完全框架结构上﹐而不会一头栽进步调的实行详细之中。这制止了见树不见林的缺陷﹐放宽了软硬件师的眼界﹐为软硬件的多用处(reusability) 及弹性(flexibility) 设想﹐可创作长命的软硬件﹗ 东西之间的罕见联系有很多种﹐个中之一即是whole-part联系。像一朵花是由花蕊、花瓣、衬叶等所形成的﹐这朵花是个「完全」(whole)﹐而花蕊、花瓣等则是这完全的「一局部」(part)。再如﹐下图的windows画面上﹐form1 东西包括着3 个遏制东西(control) ﹐那些遏制东西变成form1 的一局部。因之﹐form1 是个完全﹐而各遏制东西则是form1 东西的一局部。 图1 、form1 东西包括3 个遏制东西咱们可运用uml图形表白为﹕ 图2、whole-part联系与接受联系 这图囊括了whole-part联系﹐以及接受联系。● 口形 标记表白whole-part联系﹔即是form1 东西可包括罕见个control 东西。● 箭镞 标记表白接受联系﹔即是control 东西可细分为数个品种。正文潜心于whole-part联系﹐为composite款式创造普通。whole-part联系可分为两种﹕◎「局部」东西之个数是决定的。比方﹐1 辆公共汽车含有1 个目标盘﹐1 个form东西含有1 个昂首(caption) 东西﹐1 只田鸡有4 条腿之类。在这种决定景象﹐可视为下述「可变」景象的惯例。◎「局部」东西之个数是可变的。比方﹐1 个form东西内含多个遏制东西﹐1 棵树含有不计其数叶子﹐1 位弟子的功效单上列有各科手段功效之类。这种whole-part联系常常得藉汇合(collection)东西来表白之﹐如vb 的arraylist东西就可派上用途了。2. 大略的whole-part联系 最简单的whole-part联系即是某东西「内含」(contain) 其它东西。比方﹐ s 1 张订单上头列示着多个购买的产物名目 s 1 支棒球队具有数字教授及多位球员 s 弟子的功效单上印有多项功效 s 1 篇作品是由很多句子所构成 s 屏幕上的采用表(menu)内含数个采用项 s 1 份试卷包括数十道课题 ......就拿订单(order form)的例子来说﹐订单上常列有多个购买名目(order line)。 图3、 订单的例子每个购买名目常常囊括有产物称呼、购买数目、金额等。为了大略起见﹐假如购买名目只含产物称呼及金额两个名目﹐可藉uml图表白「订单」与「购买名目」之间的whole-part联系﹐如次图所示﹕ 图4、以uml表白大略的whole-part联系 在以vb落实这个uml形式时,则可藉vb的arraylist汇合东西来实作(implement)「订单」与「购买名目」之间的whole-part联系﹐如次图所示﹕ 图5、以uml表白大略的whole-part联系 此刻的vb软硬件师须要很风气于控制这种whole-part联系﹐且常藉汇合东西来表白之。请看本质的vb步调﹐个中设置order 及orderline 两个类型﹕'ex01.basimports system.componentmodelimports system.drawingimports system.winformsimports system.collections'----------------------------------------------------class orderline private pname as string private amt as double public sub new(byval na as string, byval am as double) pname = na amt = am end sub public function name() as string name = pname end function public function amount() as double amount = amt end functionend classclass order private orderid as string private lines as arraylist public sub new(byval id as string) orderid = id lines = new arraylist() end sub public sub addline(byval ln as orderline) lines.add(ln) end sub public function amount() as double dim total as double = 0 dim ln as orderline for each ln in lines total = total + ln.amount() next amount = total end function public function getorderid() as string getorderid = orderid end functionend class'------------------------------------------------------------------------public class form1 inherits system.winforms.form public sub new()mybase.new() form1 = me 'this call is required by the win form designer. initializecomponent() 'todo: add any initialization after the initializecomponent() call end sub 'form overrides dispose to clean up the component list. public overrides sub dispose() mybase.dispose() components.dispose() end sub#region " windows form designer generated code " ......#end region protected sub form1_click(byval sender as object, byval e as system.eventargs) dim ord as new order("order777") dim pline as orderline pline = new orderline("pencil", 88.25) ord.addline(pline) pline = new orderline("ballpen", 110.5) ord.addline(pline) messagebox.show(ord.getorderid + "'s amount = " + str(ord.amount())) end subend class此步调输入: order777's cost = 198.75 在order 类型中设置了lines变量﹐其型态arraylist﹐表白lines将可代办1 个arraylist之东西﹐其实质上即是lines变量,内含一个参考值﹐参考到arraylist之东西。在order类型的建构步调 ---- new()里,出生了arraylist东西﹐并将参考值惠存lines里。各步调之设置如次﹕ order 类型的amount()步调计划出该订单的总金额。addline() 则在订单上新增一个购买名目。在form1_click()中﹐ord 东西内含一个lines汇合东西﹐它包含2 个orderline东西。如许就表白了订单与购买名目之间的whole-part联系了。上述步调十分于 -----'ex02.basimports system.componentmodelimports system.drawingimports system.winformsimports system.collections'----------------------------------------------------class order class orderline public pname as string public amt as double end class private orderid as string private lines as arraylist public sub new(byval id as string) orderid = id lines = new arraylist() end sub public sub addline(byval pna as string, byval am as double) dim ln as orderline ln = new orderline() ln.pname = pna ln.amt = am lines.add(ln) end sub public function amount() as double dim total as double = 0 dim ln as orderline for each ln in lines total = total + ln.amt next amount = total end function public function getorderid() as string getorderid = orderid end functionend class'-----------------------------------------------------public class form1 inherits system.winforms.form public sub new() mybase.new() form1 = me 'this call is required by the win form designer. initializecomponent() 'todo: add any initialization after the initializecomponent() call end sub 'form overrides dispose to clean up the component list. public overrides sub dispose() mybase.dispose() components.dispose() end sub#region " windows form designer generated code " ...... #end region protected sub form1_click(byval sender as object, byval e as system.eventargs) dim ord as new order("order777") ord.addline("pencil", 88.25) ord.addline("ballpen", 110.5) messagebox.show(ord.getorderid + "'s amount = " + str(ord.amount())) end subend class此步调输入: order777's cost = 198.753. 递归式whole-part联系 whole-part联系内含其余whole-part联系﹐且承诺有多档次的whole-part联系﹐通称为递归式的whole-part联系。在天然界中罕见这种联系﹐比方﹐树叶是树的一局部﹐但树叶又是个完全﹐其内含着叶肉、叶绿素等「局部」东西。 图6、天然界的多档次whole-part联系在企技术界﹐最典范的例子是「东西构造表」(bill of material简称bom)﹐如次﹕ 图7、企业物料表(bom)的whole-part联系 乍看之下,那些构造犹如很搀杂﹐但从那些图形中﹐可看出那些东西可依其脚色而分为两类﹕ 1. leaf东西。如上海图书馆里的「白色」类型之东西﹐它们不具备whole 之脚色﹐只具备part之脚色。这通称为「基础组件」(primitive component) 。 2. composite 东西。如上海图书馆中的「灰色」类型之东西﹐它们具备whole之脚色﹐也大概具备part之脚色。这通称为「复合组件」(composite component) 。因之﹐只需设置两个类型──leaf及composite 类型即行。4. 以vb落实composite款式 上述递归whole-part联系是很罕见的﹐是软硬件安排师习用的工夫。因之﹐在gamma 的"design patterns" 一书〔注1 〕中﹐也将之收录为要害的「款式」(pattern) 之1。 该书所画的款式构造图如次图: 图8、composite款式 这款式倡导咱们应设置add() 、remove()和getchild()三个基础的可再设置的(overridable) 步调﹐以及其它的步调。大师们把这表白法视为款式﹐就意谓着﹕这是大师们所觉得最理念的表白办法。此刻﹐本质以依循这个款式来表白上述bom 构造,必需设置下述类型:u part类型 ----- 对应到componentu piecepart类型 ----- 对应到leafu assemblypart类型 ----- 对应到composite再将之落实为vb步调,如次:'ex03.basimports system.componentmodelimports system.drawingimports system.winformsimports system.collections'----------------------------------------------------interface ipart sub add(byval p as ipart) function getchild(byval n as integer) as ipart function cost() as double function name() as stringend interfaceclass part private pname as string protected sub new(byval na as string) pname = na end sub protected function getname() as string getname = pname end functionend classclass piecepart implements ipart inherits part private pcost as double public sub new(byval na as string, byval c as double) mybase.new(na) pcost = c end sub public sub add(byval p as ipart) implements ipart.add messagebox.show("parts do not have subparts") end sub public function subpart(byval n as integer) as ipart implements ipart.getchild subpart = nothing end function public function cost() as double implements ipart.cost cost = pcost end function public function name() as string implements ipart.name name = mybase.getname() end functionend classclass assemblypart implements ipart inherits part private children as arraylist public sub new(byval na as string) mybase.new(na) children = new arraylist() end sub public sub add(byval p as ipart) implements ipart.add children.add(p) end sub public function subpart(byval n as integer) as ipart implements ipart.getchild dim obj as object obj = children.item(n) subpart = ctype(obj, ipart) end function public function cost() as double implements ipart.cost dim sum as double dim ps as ipart sum = 0 for each ps in children sum = sum + ps.cost() next cost = sum end function public function name() as string implements ipart.name name = mybase.getname() end functionend class'-----------------------------------------------------public class form1 inherits system.winforms.form public sub new() mybase.new() form1 = me 'this call is required by the win form designer. initializecomponent() 'todo: add any initialization after the initializecomponent() call end sub 'form overrides dispose to clean up the component list. public overrides sub dispose() mybase.dispose() components.dispose() end sub#region " windows form designer generated code " ......#end region protected sub form1_click( byval sender as object, byval e as system.eventargs ) dim aly1, aly2, p1, p2 as ipart dim ps as ipart aly1 = new assemblypart("bulb") p1 = new piecepart("body", 88.25) aly1.add(p1) p1 = new piecepart("head", 100.5) aly1.add(p1) aly2 = new assemblypart("light") aly2.add(aly1) p1 = new piecepart("cover", 10) aly2.add(p1) messagebox.show(aly2.name() + "'s cost = " + str(aly2.cost())) p1 = aly2.getchild(0) messagebox.show(p1.name + "'s cost = " + str(p1.cost())) p2 = aly2.getchild(1) messagebox.show(p2.name + "'s cost = " + str(p2.cost())) p2 = p1.getchild(0) messagebox.show(p2.name + "'s cost = " + str(p2.cost())) end subend class此步调输入: light's cost = 198.75 bulb's cost = 188.75 cover's cost = 10 body's cost = 88.25piecepart 代办最基层的基础东西。而assemblypart则代办中层的半制品东西组件﹐或代办制品。part为一个笼统类型,设置piecepart与assemblypart类型的共通部份(囊括属性和动作),供piecepart与assemblypart子类型来接受之。其余,供给一个接口ipart给client运用,以封装part、piecepart和assemblypart类型,创作part、piecepart和assemblypart类型的弹性安排空间,这利害常要害的。 form1_click()步调创造了相关「公共汽车车灯」的东西构造表﹕ 图8、vb步调所出生的东西联系图 p1 = aly2.getchild(0)掏出「灯胆」小东西﹐并由p1代办这个小东西。p2 = p1.getchild(0) 掏出「灯帽」小东西﹐并由p2代办之。依上述之款式﹐可表白出无穷档次的递归式whole-part联系。 为了让软硬件能永续存在下来﹐必需更加关心软硬件的构造与其完全框架结构(architecture)。所以﹐安排软硬件时,必需潜心于东西之间的合作协作联系。一旦创造了理念的联系﹐就不妨让东西之间彼此传播消息、彼此勾通了。比方﹐form1 东西传递cost消息给aly2东西﹐aly2东西就依循arraylist东西所创造的联系来将cost消息递给内含之各小东西。当各小东西汇报其本钱金额﹐aly2将之累计再传递回到form1 东西﹐而后表露在窗口画面上。现在﹐断定您仍旧进一步领会whole-part联系﹐善用arraylist汇合类型来表白之,并更会应用vb来落实款式,创作出更优美的软硬件。■[注1] erich gamma,design patterns: elements of reusable object-oriented software, addition-wesley, 1995.

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.