第9章-組合模式課件_第1頁
第9章-組合模式課件_第2頁
第9章-組合模式課件_第3頁
第9章-組合模式課件_第4頁
第9章-組合模式課件_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第9章組合/合成模式(Composite)從前有個山,山里有個廟,廟里有個老和尚在給小和尚講故事,講的什么故事呢?從前有個山,山里有個廟……。奶奶的故事要循環(huán)多少次,根據(jù)你多長時間睡著而定。在故事中有山、有廟、有和尚、有故事從和尚的故事談起從和尚的故事談起組合模式組合模式定義將對象組合成樹形結(jié)構(gòu)以表示“整體-部分”的結(jié)構(gòu)層次。組合模式對單個對象(即葉子對象)和組合對象(即容器對象)的使用具有一致性應(yīng)用組合模式來解決的思路引入一個抽象組件對象,作為組合對象和葉子對象的父對象用戶使用時,不區(qū)分是在操作組合對象或是葉子對象,即對單個對象和組合對象的使用具有一致性組合模式UML類圖

Component抽象組件對象為組合中的對象聲明接口,讓客戶端可以通過這個接口來訪問和管理整個對象結(jié)構(gòu),可以在里面為定義的功能提供缺省的實現(xiàn)Leaf

葉子節(jié)點對象定義和實現(xiàn)葉子對象的行為,不再包含其它的子節(jié)點對象Composite組合對象通常會存儲子組件,定義包含子組件的那些組件的行為,并實現(xiàn)在組件接口中定義的與子組件有關(guān)的操作。Client

通過Component接口操縱組合部件的組件對象。組合模式分析組合模式的關(guān)鍵是定義一個抽象構(gòu)件類??蛻舳酸槍υ摮橄髽?gòu)件類進行編程,無須知道它到底表示的是葉子還是組合,可以對其進行統(tǒng)一處理同時組合對象與抽象構(gòu)件類之間還建立一個聚合關(guān)聯(lián)關(guān)系,在組合對象中既可以包含葉子,也可以包含組合對象,以此實現(xiàn)遞歸組合,形成一個樹形結(jié)構(gòu)組合模式可以不提供父對象的管理方法,但組合模式必須在合適的地方提供子對象的管理方法(如:add、remove、getChild等)組合模式的實現(xiàn)根據(jù)所實現(xiàn)接口的區(qū)別分為兩種形式:安全模式和透明模式組合模式類別透明組合模式UML在Component里面聲明所有的用來管理子類對象的方法,包括add、remove,以及getChild。好處:所有的構(gòu)件類都有相同的接口??蛻舳?/p>

可以等同的對待所有的對象。這就是透明形式的合成模式。缺點:不夠安全,因為樹葉類對象和合成類對

象在本質(zhì)上是有區(qū)別的。

樹葉類對象不可能有下一個層次的對象,因此add、remove以及getChild方法沒有意義.在編譯時期不會出錯,而只會在運行時期才會出錯。publicabstract

class

Component

{

//

Fields

protected

String

name;

//

Constructors

public

Component(

String

name

)

{

=

name;

}

//

Methods

public

abstract

void

Add(Component

c);

public

abstract

void

Remove(

Component

c

);

public

abstract

void

Display(

int

depth

);

}publicclass

Leaf

extends

Component

{

//

Constructors

public

Leaf(

String

name

){super(name);}

//

Methods

public

void

Add(

Component

c

)

{

System.out.println("Cannot

add

to

a

leaf");

}

public

void

Remove(

Component

c

)

{

System.out.println("Cannot

remove

from

a

leaf");

}

public

void

Display(

int

depth

)

{

System.out.println(

name

);

}

}publicclass

Composite

extendsComponent{

private

ArrayList

children

=

new

ArrayList();

public

Composite(

string

name

)

{super(name);}

public

void

Add(

Component

component

)

{

children.Add(

component

);

}

public

void

Remove(

Component

component

)

{

children.Remove(

component

);

}

public

void

Display(

int

depth

)

{

System.out.println(

name

);

foreach(

Component

component

in

children

)

component.Display(

depth

+

2

);

}

}public

class

Client{

public

static

void

main(

String[]

args

)

{

Composite

root

=

new

Composite(

"root"

);

root.Add(

new

Leaf(

"Leaf

A"

));

root.Add(

new

Leaf(

"Leaf

B"

));

Composite

comp

=

new

Composite(

"Composite

X"

);

comp.Add(

new

Leaf(

"Leaf

XA"

)

);

comp.Add(

new

Leaf(

"Leaf

XB"

)

);

root.Add(

comp

);

root.Add(

new

Leaf(

"Leaf

C"

));

Leaf

l

=

new

Leaf(

"Leaf

D"

);

root.Add(

l

);

root.Remove(

l

);

root.Display(

1

);

}

}安全組合模式UML在Composite類里面聲明所有的用來管理子類對象的方法合成類和樹葉類具有不同的實現(xiàn)。因為樹葉類型的對象根本沒有管理子類對象的方法,因此,如果客戶端對樹葉類對象使用這些方法時,程序會在編譯時期出錯缺點是不夠透明,因為樹葉類和合成類將具有不同的接口publicabstract

class

Component

{

//

Fields

protected

String

name;

//

Constructors

public

Component(

String

name

)

{

=

name;

}

public

abstract

void

Display(

int

depth

);

}publicclass

Leaf

extends

Component

{

//

Constructors

public

Leaf(

String

name

){super(name);}

//

Methods

public

void

Display(

int

depth

)

{

System.out.println(

name

);

}

}

publicclass

Composite

extendsComponent{

private

ArrayList

children

=

new

ArrayList();

public

Composite(

string

name

)

{super(name);}

public

void

Add(

Component

component

)

{

children.Add(

component

);

}

public

void

Remove(

Component

component

)

{

children.Remove(

component

);

}

public

void

Display(

int

depth

)

{

System.out.println(

name

);

foreach(Component

component

in

children)

component.Display(

depth

+

2

);

}

}public

class

Client{

public

static

void

main(

String[]

args

)

{

Composite

root

=

new

Composite(

"root"

);

root.Add(

new

Leaf(

"Leaf

A"

));

root.Add(

new

Leaf(

"Leaf

B"

));

Composite

comp

=

new

Composite(

"Composite

X"

);

comp.Add(

new

Leaf(

"Leaf

XA"

)

);

comp.Add(

new

Leaf(

"Leaf

XB"

)

);

root.Add(

comp

);

root.Add(

new

Leaf(

"Leaf

C"

));

Leaf

l

=

new

Leaf(

"Leaf

D"

);

root.Add(

l

);

root.Remove(

l

);

root.Display(

1

);

}

}兩種實現(xiàn)方法的選擇安全性合成模式是指:從客戶端使用合成模式上看是否更安全,如果是安全的,那么就不會有發(fā)生誤操作的可能,能訪問的方法都是被支持的透明性合成模式是指:從客戶端使用合成模式上,是否需要區(qū)分到底是“樹枝對象”還是“樹葉對象”。如果是透明的,那就不用區(qū)分,對于客戶而言,都是Compoent對象,具體的類型對于客戶端而言是透明的,是無須關(guān)心的。兩種實現(xiàn)方法的選擇對于合成模式而言,在安全性和透明性上,會更看重透明性,畢竟合成模式的目的是:讓客戶端不再區(qū)分操作的是樹枝對象還是樹葉對象,而是以一個統(tǒng)一的方式來操作JSP組合頁面實例JSP組合頁面實例Java中的應(yīng)用JDK的AWT/Swing是組合模式在Java類庫中的一個典型實際應(yīng)用。Java

SE6對腳本語言的支持腳本語言又叫動態(tài)語言。是一種編程語言控制軟件應(yīng)用程序。腳本通常以文本(如ASCII)保存,只在被調(diào)用時進行解釋或編譯。PHP、Python、JavaScript、Perl、Ruby均為腳本級語言

python是最出色的腳本語言,它可以很容易的內(nèi)嵌某個軟件中,作為那個軟件的擴充,例如著名的3D設(shè)計軟件blender使用內(nèi)嵌的python腳本語言來設(shè)計復(fù)雜動畫

對于初學(xué)者,python很適合于學(xué)習(xí)編程思想3)javax.script.SimpleBindingsSimpleBindings通過組合模式實現(xiàn)Map接口,它提供了兩個構(gòu)造函數(shù)。

無參構(gòu)造函數(shù)在內(nèi)部構(gòu)造一個HashMap實例來實現(xiàn)Map接口要求的功能;以Map接口作為參數(shù)的構(gòu)造函數(shù),允許任何實現(xiàn)Map接口的類作為其組合的實例TomcatHttpServletRequest實現(xiàn)類中,組合了org.apache.catalina.Context的實現(xiàn)or

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論