版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、教學(xué)內(nèi)容復(fù)習(xí),提綱,一、基本概念 二、面向?qū)ο笤O(shè)計原則 三、框架、模式、類庫 四、典型軟件體系結(jié)構(gòu) 五、設(shè)計模式分類 六、典型設(shè)計模式 七、模式的應(yīng)用,一、基本概念,軟件體系結(jié)構(gòu) 軟件體系結(jié)構(gòu)是具有一定形式的結(jié)構(gòu)化元素,即構(gòu)件的集合,包括處理構(gòu)件、數(shù)據(jù)構(gòu)件和連接構(gòu)件。 設(shè)計模式 設(shè)計模式(pattern)是從許多優(yōu)秀的軟件系統(tǒng)中總結(jié)出的成功的可復(fù)用的設(shè)計方案。 軟件框架 在給定問題領(lǐng)域范圍內(nèi)建立起可重用解決設(shè)計問題而互相間協(xié)作的一組類的集合(它典型包括了具備缺省行為的大量對象),二、面向?qū)ο笤O(shè)計原則,面向抽象原則 當(dāng)設(shè)計一個類時,不讓該類面向具體的類,而是面向抽象類或接口,即所設(shè)計類中的重要數(shù)
2、據(jù)是抽象類或接口聲明的變量,而不是具體類聲明的變量。 開-閉原則 所謂 “開-閉原則”(Open-Closed Principle)就是讓你的設(shè)計應(yīng)當(dāng)對擴(kuò)展開放,對修改關(guān)閉。,二、面向?qū)ο笤O(shè)計原則,多用組合少用繼承原則 之所以提倡多用組合,少用繼承,是因為在許多設(shè)計中,人們希望系統(tǒng)的類之間盡量是低耦合的關(guān)系,而不希望是強(qiáng)偶合關(guān)系。即在許多情況下需要避開繼承的缺點,而需要組合的優(yōu)點。,三、框架、模式和類庫,框架和模式的關(guān)系 設(shè)計模式研究的是一個設(shè)計問題的解決方法,一個模式可應(yīng)用于不同的框架和被不同的語言所實現(xiàn);而框架則是一個應(yīng)用的體系結(jié)構(gòu),是一種或多種設(shè)計模式和代碼的混合體雖然它們有所不同,但卻
3、共同致力于使人們的設(shè)計可以被重用,在思想上存在著統(tǒng)一性的特點,因而設(shè)計模式的思想可以在框架設(shè)計中進(jìn)行應(yīng)用。,三、框架、模式和類庫,框架和模式的關(guān)系 框架是軟件,而設(shè)計模式是軟件的知識體,提升框架的設(shè)計水平。 框架面向領(lǐng)域,設(shè)計模式是通用的。 設(shè)計模式可以在框架,三、框架、模式、類庫,框架和類庫的關(guān)系 行為相對于協(xié)議 “不是訪問我們,而是我們訪問你” 實現(xiàn)相對于設(shè)計,四、典型軟件體系結(jié)構(gòu),客戶/服務(wù)器 C/S體系結(jié)構(gòu)有三個主要組成部分:數(shù)據(jù)庫服務(wù)器、客戶應(yīng)用程序和網(wǎng)絡(luò)。,四、典型軟件體系結(jié)構(gòu),四、典型軟件體系結(jié)構(gòu),三層客戶/服務(wù)器,四、典型軟件體系結(jié)構(gòu),瀏覽器/服務(wù)器 B/S體系結(jié)構(gòu)主要是利用不
4、斷成熟的WWW瀏覽器技術(shù),結(jié)合瀏覽器的多種腳本語言,用通用瀏覽器就實現(xiàn)了原來需要復(fù)雜的專用軟件才能實現(xiàn)的強(qiáng)大功能,并節(jié)約了開發(fā)成本。從某種程度上來說,B/S結(jié)構(gòu)是一種全新的軟件體系結(jié)構(gòu)。,四、典型軟件體系結(jié)構(gòu),四、典型軟件體系結(jié)構(gòu),反射式體系結(jié)構(gòu),四、典型軟件體系結(jié)構(gòu),Structural Reflection Structural reflection is concerned with the ability of the language to reflect both about the structure of the program as well as the program c
5、urrently executed.,四、典型軟件體系結(jié)構(gòu),Behavioral Reflection Behavioral reflection is the ability of the programming language to reflect about its own semantics and implementation as well as the data and implementation of the runtime system.,五、設(shè)計模式分類,創(chuàng)建型模式 工廠方法:定義一個用于創(chuàng)建對象的接口,讓子類決定實例化哪一個類。 生成器模式:將一個復(fù)雜對象的構(gòu)建與它的
6、表示分離。 原型模式:通過復(fù)制原型創(chuàng)建新的對象。 單件模式:保證一個類僅有一個實例。,六、設(shè)計模式分類,行為型模式 責(zé)任鏈模式:避免請求的發(fā)送者和接收者之間的耦合關(guān)系。 中介者模式:用一個中介對象來封裝一系列的對象交互。 觀察者模式:定義對象間的一種一對多的依賴關(guān)系,當(dāng)一個對象的狀態(tài)發(fā)生變化時,所有依賴于它的對象都得到通知并被自動更新。 狀態(tài)模式:允許一個對象在其內(nèi)部狀態(tài)改變時改變它的行為。 策略模式:定義一系列算法,把它們一個個封裝起來,并且使它們可相互替換。,四、設(shè)計模式分類,結(jié)構(gòu)型模式 適配器模式:將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口。 組合模式:將對象組合成樹形結(jié)構(gòu)以表示“部分-
7、整體”的層次關(guān)系。 裝飾模式:動態(tài)地給對象添加一些額外的職責(zé)。,七、典型設(shè)計模式,組合模式,意圖 將對象組合成數(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。Composite使得用戶對單個對象和組合對象的使用具有一致性。,組合模式,結(jié)構(gòu),組合模式,適用場合 想表示對象的部分-整體層次結(jié)構(gòu) 希望用戶用一致的方式處理個體對象和組合對象。,示例,import java.applet.*; import java.awt.*; public class Composite extends Applet Picture pict = new Picture(); public void init() pict
8、.add(new Circle(new Point(10,10),50); pict.add(new Rectangle(10,80,60,140); pict.add(new Line(10,150,60, 170); pict.add(new Circle(new Point(100,80),20); / This method is called whenever the applet needs to be drawn or redrawn public void paint(Graphics g) pict.draw(g); ,import java.awt.*; public ab
9、stract class shape extends Object Color fill, outline, textcolor; / The various colors we use public shape() / Initialize color resources. Note the use of the Color() constructor / and the use of pre-defined color constants. fill = new Color(200, 200, 200); / Equal red, green, and blue = gray outlin
10、e = Color.blue; / Same as new Color(0, 0, 255) textcolor = Color.red; / Same as new Color(255, 0, 0) public void setFillColor(Color c) fill = c; public void setOutlineColor(Color c) outline = c; public void setTextColor(Color c) textcolor = c; public abstract void draw(Graphics g); ,import java.awt.
11、*; import java.util.*; public class Picture extends shape / ISA relationship Vector list = new Vector(); / HASA relationship public void add(shape s) list.addElement(s); public void draw(Graphics g) Iterator it = list.iterator(); while (it.hasNext() it.next().draw(g); ,import java.awt.*; public clas
12、s Circle extends shape int radius; Point center; public Circle(Point c,int r) center = c; radius = r; public void draw(Graphics g) g.setColor(fill); g.fillOval(center.x, center.y, radius, radius); g.setColor(outline); g.drawOval(center.x, center.y, radius, radius); ,import java.awt.*; public class L
13、ine extends shape Point start,end; public Line(int sx,int sy,int ex,int ey) start = new Point(sx,sy); end = new Point(ex,ey); public void draw(Graphics g) g.setColor(outline); g.drawLine(start.x,start.y,end.x,end.y); ,import java.awt.*; public class Rectangle extends shape Point ul,lr; public Rectan
14、gle(Point u,Point l) ul = u; lr = l; public Rectangle(int x1,int y1,int x2, int y2) ul = new Point(x1,y1); lr = new Point(x2,y2); public void draw(Graphics g) g.setColor(fill); g.fillRect(ul.x, ul.y, lr.x - ul.x, lr.y - ul.y); g.setColor(outline); g.drawRect(ul.x, ul.y, lr.x - ul.x, lr.y - ul.y); ,p
15、ublic class Point public int x; public int y; public Point() x = y = 0; public Point(Point p) x = p.x; y = p.y; public Point(int i,int j) x = i; y = j; ,工廠方法模式,意圖 定義一個用于創(chuàng)建對象的接口,讓子類決定實例化哪一個類。Factory Method使一個類的實例化延遲到其子類。,工廠方法模式,結(jié)構(gòu),適用場合 用戶需要一個類的子類的實例,但不希望與該類的子類形成耦合。 用戶需要一個類的子類的實例,但用戶不知道該類有哪些子類可用。,工廠方法
16、模式,示例,有一系列的自動車產(chǎn)品Bus(公交車),Truck(卡車);它們需要由不同的工廠生產(chǎn),但具有相同的生產(chǎn)流程。為了以后擴(kuò)展其他流水線,比如摩托車,我們需要一種柔軟的方案來實現(xiàn)自動車生產(chǎn)的過程。,設(shè)計目標(biāo),一方面,可以不用關(guān)心產(chǎn)品對象的具體實現(xiàn),簡化和統(tǒng)一Client調(diào)用過程; 另一方面,可以讓整個系統(tǒng)具有靈活的可擴(kuò)展性,若要擴(kuò)展生產(chǎn)摩托車的流水線,只要再實現(xiàn)一個摩托車類以及摩托車工廠類就行,而Client的調(diào)用方法完全不需改變。,實現(xiàn)方法一,abstract class CarFactory /定義生產(chǎn)自動車生產(chǎn)的接口規(guī)范 public CarcreateCar(); ,class B
17、usFactory extends CarFactory public CarcreateCar() /生產(chǎn)公交車 return new CarBus(); ,class TruckFactory extends CarFactory public CarcreateCar() /生產(chǎn)卡車 return new TruckBus(); ,public class Car public StringgetType() return Car ; ,public class CarBus extends Car public StringgetType() return Bus ; ,public
18、class CarTruck extends Car public StringgetType() return Truck ; ,public class Client public static void main(String args) CarFactory factory = new BusFactory(); /生產(chǎn)公交車 Car bus = factory.createCar(); System.out.println(生產(chǎn)了: + bus.getType(); factory = new TruckFactory(); /生產(chǎn)卡車 Car truck = factory.cre
19、ateCar(); System.out.println(生產(chǎn)了: + truck.getType(); ,實現(xiàn)方法二,class CarFactory public CarcreateCar(String carType) if(carType.equals(“BUS”) Return new CarBus(); if(carType.equals(“TRUCK”) Return new CarTruck(); ,public class Client public static void main(String args) CarFactory factory = new CarFacto
20、ry(); /生產(chǎn)公交車 Car bus = factory.createCar(“BUS”); System.out.println(生產(chǎn)了: + bus.getType(); /生產(chǎn)卡車 Car truck = factory.createCar(“TRUCK”); System.out.println(生產(chǎn)了: + truck.getType(); ,觀察者模式,意圖 定義對象間的一種一對多的依賴關(guān)系,當(dāng)一個對象的狀態(tài)發(fā)生變化時,所有依賴于它的對象都得到通知并被自動更新。,觀察者模式,結(jié)構(gòu),觀察者模式,適用場合 當(dāng)一個對象的數(shù)據(jù)更新時需要通知其他對象,但這個對象又不希望和被通知的那些對象
21、形成緊耦合。 當(dāng)一個對象的數(shù)據(jù)更新時,這個對象需要讓其他對象也各自更新自己的數(shù)據(jù),但這個對象不知道具體有多少對象需要更新數(shù)據(jù)。,示例,public interface Subject public void attach(Observer o); public void detach(Observer o); public void notice(); ,package observer; public interface Observer public void update(); ,public class Teacher implements Subject private String
22、 phone; private Vector students; public Teacher() phone = ; students = new Vector(); public void attach(Observer o) students.add(o); public void detach(Observer o) students.remove(o); public void notice() for(int i=0;istudents.size();i+) (Observer)students.get(i).update(); public void setPhone(Strin
23、g phone) this.phone = phone; notice(); public String getPhone() return phone; ,public class Student implements Observer private String name; private String phone; private Teacher teacher; public Student(String name,Teacher t) = name; teacher = t; public void show() System.out.println(Name:
24、+name+nTeachersphone:+phone); public void update() phone = teacher.getPhone(); ,public class Client public static void main(String args) Vector students = new Vector(); Teacher t = new Teacher(); for(int i= 0 ;i10;i+) Student st = new Student(lili+i,t); students.add(st); t.attach(st); t.setPhone(888
25、03807); for(int i=0;i10;i+) (Student)students.get(i).show(); t.setPhone(88808880); for(int i=0;i10;i+) (Student)students.get(i).show(); ,策略模式,意圖 定義一系列算法,把它們一個個封裝起來,并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶而變化。,策略模式,結(jié)構(gòu),策略模式,適用場合 一個類定義了多種行為,并且這些行為在這個類中以多個條件語句的形式出現(xiàn),可以使用策略模式避免在類中使用大量的條件語句。 程序不需要暴露復(fù)雜的、與算法相關(guān)的數(shù)據(jù)結(jié)構(gòu),可以使用
26、策略模式封裝算法。 需要使用一個算法的不同變體。,示例,把數(shù)組導(dǎo)出為表格的算法,輸入一個數(shù)組,導(dǎo)出一個表格,當(dāng)用戶想改變導(dǎo)出的表格時,便可以通過改變輸出的算法改變輸出的結(jié)果。如果輸出的內(nèi)容用以網(wǎng)頁顯示,則輸出 JackMayaMikesShadow 如果輸出的結(jié)果用以直接的屏幕輸出,則可以輸出: +-+-+-+-+ |Jack|Maya|Mikes|Shadow| +-+-+-+-+,public interface TableExporter public String getExported(String data); ,public class HtmlExporter implemen
27、ts TableExporter public String getExported(String data) if(data = null) return ; StringBuffer sb = new StringBuffer(); sb.append(); for(int i=0;i+datai+); sb.append(); return sb.toString(); ,public class LineExporter implements TableExporter public String getExported(String data) if(data = null) ret
28、urn ; StringBuffer top = new StringBuffer(+); StringBuffer mid = new StringBuffer(|); for(int i =0;idata.length;i+) String str = datai; for(int j=0;jstr.length();j+) top.append(-); mid.append(str); top.append(+); mid.append(|); top.append(n); mid.append(n); return top.toString()+mid.toString()+top.t
29、oString(); ,public class Client public static void main(String args) String data = new StringJack,Maya,Mikes,Shadow; TableExporter tb; tb = new HtmlExporter(); System.out.println(tb.getExported(data); tb = new LineExporter(); System.out.println(tb.getExported(data); ,使用上下文,public class Context Table
30、Exporter t; public void setExporter(TableExporter t) this.t = t; public String getExported(String data) return t. getExported(data); ,public class Client public static void main(String args) String data = new StringJack,Maya,Mikes,Shadow; Context c = new Context(); TableExporter tb = new HtmlExporte
31、r(); c.setExporter(tb); System.out.println(c.getExported(data); ,狀態(tài)模式,意圖 允許一個對象在其內(nèi)部狀態(tài)改變時改變它的行為。對象看起來似乎修改了它的類。,狀態(tài)模式,結(jié)構(gòu),狀態(tài)模式,適用場合 一個對象的行為依賴于它的狀態(tài),并且它必須在運行時根據(jù)狀態(tài)改變它的行為。 需要編寫大量的條件語句來決定一個操作的行為,而且這些條件恰好表示對象的一種狀態(tài)。,示例,Consider a class that has two methods, push() and pull(), whose behavior changes depending o
32、n the state of the object. To send the push and pull requests to the object, well use the following GUI with Push and Pull buttons:,The state of the object will be indicated by the color of the canvas in the top part of the GUI The states are: black, red, blue and green,public class ContextNoSP priv
33、ate Color state = null; public ContextNoSP(Color color) state = color; public ContextNoSP() this(Color.red); public Color getState() return state; public void setState(Color state) this.state = state; public void push() if (state = Color.red) state = Color.blue; else if (state = Color.green) state =
34、 Color.black; else if (state = Color.black) state = Color.red; else if (state = Color.blue) state = Color.green; public void pull() if (state = Color.red) state = Color.green; else if (state = Color.green) state = Color.blue; else if (state = Color.black) state = Color.green; else if (state = Color.
35、blue) state = Color.red; ,public class TestNoSP extends Frame implements ActionListener private Button pushButton = new Button(Push Operation); private ContextNoSP context = null; public TestNoSP() super(No State Pattern); context = new ContextNoSP(); setupWindow(); private void setupWindow() / Setu
36、p GUI public void actionPerformed(ActionEvent event) Object src = event.getSource(); if (src = pushButton) context.push(); canvas.setBackground(context.getState(); else if (src = pullButton) context.pull(); canvas.setBackground(context.getState(); else if (src = exitButton) System.exit(0); ,/ Main m
37、ethod. public static void main(String argv) TestNoSP gui = new TestNoSP(); gui.setVisible(true); ,Now lets use the State pattern!,public abstract class State public abstract void handlePush(Context c); public abstract void handlePull(Context c); public abstract Color getColor(); ,public class BlackState extends State / Next state for the Black state: / On a push(), go to red / On a pull(), go to green public void handlePush(Context c) c.setState(new RedState(); public void handlePull(Context c) c.setState(new GreenState(); public Color getColor() return (Color.
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年大學(xué)生物學(xué)(遺傳學(xué))試題及答案
- 2025年大學(xué)水利環(huán)境化學(xué)(水利工程環(huán)保)試題及答案
- 2025年高職水產(chǎn)養(yǎng)殖學(xué)(水產(chǎn)動物營養(yǎng))試題及答案
- 2025年大學(xué)(農(nóng)學(xué))植物生理學(xué)代謝階段測試題及解析
- 2025年中職歷史(世界近現(xiàn)代史)試題及答案
- 2025年高職秘書學(xué)(商務(wù)秘書實務(wù))試題及答案
- 2025年大學(xué)消防工程(消防設(shè)計)試題及答案
- 2025年中職飛機(jī)機(jī)電設(shè)備維修(航電系統(tǒng)故障排除)試題及答案
- 2025年大學(xué)(機(jī)械電子工程)液壓與氣動技術(shù)模擬試題及解析
- 2025年高職(云計算技術(shù)應(yīng)用)云遷移技術(shù)試題及解析
- 2026年遼寧地質(zhì)工程職業(yè)學(xué)院單招綜合素質(zhì)考試題庫附答案
- 炎德·英才·名校聯(lián)考聯(lián)合體2026屆高三年級1月聯(lián)考語文試卷(含答及解析)
- 小紅書2025年9-10月保險行業(yè)雙月報
- 麥當(dāng)勞行業(yè)背景分析報告
- 2025至2030中國電腦繡花機(jī)行業(yè)深度研究及發(fā)展前景投資評估分析
- 可靠性驗證與評估流程
- 云南民族大學(xué)附屬高級中學(xué)2026屆高三聯(lián)考卷(四)英語+答案
- 機(jī)械設(shè)計課程設(shè)計報告蝸桿齒輪二級減速器
- 主題班會PPt-敬畏規(guī)則
- (卓越績效)質(zhì)量獎申報材料
- 樂業(yè)彎里金礦采礦權(quán)評價報告廣西壯族自治區(qū)國土資源廳
評論
0/150
提交評論