體系結構資料:思考題_第1頁
體系結構資料:思考題_第2頁
體系結構資料:思考題_第3頁
體系結構資料:思考題_第4頁
體系結構資料:思考題_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、集合類型Programming to Interface題目:有一個數據列表DataList,其基本類型是3維向量ThreeD,1)用Java語言實現該數據列表的數據結構。假設有三個外部對象A,B,C,分別對其x、y、 z維度感興趣,希望訪問DataList在相應維 度的數據并進行處理。2)請定義其對外的數據接口。分別使用Iterator與Proxy。(PS:Iterator,Proxy需要自己實現)Iteratorimport java.util.*;public class Iter public static void main(String args) Aggregate ag = n

2、ew DataList(); (DataList)ag).add(new ThreeD(1, 2, 3); (DataList)ag).add(new ThreeD(1, 2, 3); (DataList)ag).add(new ThreeD(1, 2, 3); Iterator ia = ag.createIteratorA(); while (ia.next() != null) System.out.println(ia.currentItem(); Iterator ib = ag.createIteratorB(); while (ib.next() != null) System.

3、out.println(ib.currentItem(); Iterator ic = ag.createIteratorC(); while (ic.next() != null) System.out.println(ic.currentItem(); class ThreeD private T t; private V v; private W w; public ThreeD(T t, V v, W w) this.t = t; this.v = v; this.w = w; public T getA() return t; public V getB() return v; pu

4、blic W getC() return w;interface Aggregate public Iterator createIteratorA(); public Iterator createIteratorB(); public Iterator createIteratorC();interface Iterator public T first(); public T next(); public boolean isDone(); public T currentItem();abstract class IteratorABC implements Iterator prot

5、ected DataList ag; protected int i; public IteratorABC(DataList ag) this.ag = ag; i = -1; public T first() i = 0; return currentItem(); public T next() i += 1; return currentItem(); public boolean isDone() return i = ag.size() ; public abstract T currentItem();class IteratorA extends IteratorABC pub

6、lic IteratorA(DataList ag) super(ag); public T currentItem() if (isDone() return null; return (T)ag.get(i).getA();class IteratorB extends IteratorABC public IteratorB(DataList ag) super(ag); public T currentItem() if (isDone() return null; return (T)ag.get(i).getB();class IteratorC extends IteratorA

7、BC public IteratorC(DataList ag) super(ag); public T currentItem() if (isDone() return null; return (T)ag.get(i).getC();class DataList implements Aggregate private ArrayListThreeD list = new ArrayListThreeD(); private IteratorA ia = new IteratorA(this); private IteratorB ib = new IteratorB(this); pr

8、ivate IteratorC ic = new IteratorC(this); public void add(ThreeD d) list.add(d); public ThreeD get(int index) return list.get(index); public int size() return list.size(); public Iterator createIteratorA() return ia; public Iterator createIteratorB() return ib; public Iterator createIteratorC() retu

9、rn ic; Proxyimport java.util.*;public class Proxy public static void main(String args) class ThreeD private T t; private V v; private W w; public ThreeD(T t, V v, W w) this.t = t; this.v = v; this.w = w; public T getA() return t; public V getB() return v; public W getC() return w;interface IDataList

10、 public T getA(int i) throws Exception; public V getB(int i) throws Exception; public W getC(int i) throws Exception; public int size();abstract class DataListProxy implements IDataList protected IDataList real; public DataListProxy(IDataList real) this.real = real; public T getA(int i) throws Excep

11、tion throw new Exception(); public V getB(int i) throws Exception throw new Exception(); public W getC(int i) throws Exception throw new Exception(); public int size() return real.size(); class DataListProxyA extends DataListProxy public DataListProxyA(IDataList real) super(real); public T getA(int

12、i) throws Exception return real.getA(i); class DataListProxyB extends DataListProxy public DataListProxyB(IDataList real) super(real); public V getB(int i) throws Exception return real.getB(i); class DataListProxyC extends DataListProxy public DataListProxyC(IDataList real) super(real); public W get

13、C(int i) throws Exception return real.getC(i); class DataList implements IDataList private ArrayListThreeD list = new ArrayListThreeD(); public void add(ThreeD d) list.add(d); public ThreeD get(int index) return list.get(index); public int size() return list.size(); public T getA(int i) return list.

14、get(i).getA(); public V getB(int i) return list.get(i).getB(); public W getC(int i) return list.get(i).getC(); public IDataList getAProxy() return new DataListProxyA(this) public IDataList getBProxy() return new DataListProxyB(this) public IDataList getCProxy() return new DataListProxyC(this)Decorat

15、or,OCP題目:對如下繼承樹進行擴展,Border(Plain,3D, Fancy) ScrollBar(Horiz, Vert)abstract class VisualComponent void draw(); void resize();class TextView extends VisualComponent void draw() . void resize() .class StreamVidoView VisualComponent void draw() . void resize() .解答:abstract class Decorator extends Visual

16、Component VisualComponent vc; Decorator(VisualComponent vc) this.vc = vc; void draw() vc.draw(); void resize() vc.resize();abstract class Border extends Decorator void draw() super.draw(); drawBorder(); abstract void drawBorder();class PlainBorder extends Border void drawBorder() .Information Hiding

17、 & Common and Variation題目:比較 facade 與 controller 的異同相同:它們自己不進行一些邏輯處理,都只是進行一些任務轉發的功能它們都是對兩個模塊或外部和內部進行解耦都是抽出來的一層,屏蔽了底層的接口不同:facade 主要關注的是模塊之間的解耦和信息隱藏,為一個子系統提供一個簡單的,外在的接口,任意兩個之間,只要有一些解耦的需求,都可以用 facade 。而 controller 的目的是對應用戶需求,職責分配,主要處理當來了一個請求或什么的,具體要分配給那個對象取。(yy)facade由于是為了模塊的解耦,在兩個基本的類之間做一個 facade 是沒有

18、意義的。而 controller 目的是為了讓用戶的請求和處理之間彼此獨立,所以可以存在兩個類之間有一個controller。題目:在策略模式中, 為什么使用“聚合”而不是 “關聯”關系?聚合可以說是一種比較強的關聯,聚合指出在對象 A 中需要有 B 的應用,而關聯僅僅是一種使用關系。由于關聯沒有規定 A 中有B的引用,那么如果 策略模式使用關聯來實現,那么就無法解決綜合使用多個策略的問題,它要么使用一個策略,要么策略的個數是確定的。 ?Common and Variation問題:如果一個對象集之間除共性外,有超過2個的差異行為,如何處理?兩棵策略樹問題:如果一個對象集的部分行為組存在差異性

19、,如何處理?把行為組放在策略中問題:問題:如果一個對象集的部分屬性(以及依賴于這些屬性的方法)存在差異性,如何處理?把這些屬性和方法放在策略中問題:如果一個對象集的一個行為需要協作對象來完成,但是它們的協作對象存在差異性,如何處理?在協作對象上加上一個adapter組成策略(Command Pattern)問題:如果一個對象集的行為因為屬性的取值而存在差異性,如何處理?state patternCompare strategy and statefrom HYPERLINK /questions/1658192/what-is-the-difference-between-strategy-d

20、esign-pattern-and-state-design-pattern stackoverflowThe Strategy pattern is really about having a different implementation that accomplishes (basically) the same thing, so that one implementation can replace the other as the strategy requires. For example, you might have different sorting algorithms

21、 in a strategy pattern. The callers to the object does not change based on which strategy is being employed, but regardless of strategy the goal is the same (sort the collection).The State pattern is about doing different things based on the state, while leaving the caller releaved from the burden o

22、f accommodating every possible state. So for example you might have a getStatus() method that will return different statuses based on the state of the object, but the caller of the method doesnt have to be coded differently to account for each potential state.“1 of N” or “M of N”?strategy M of N, st

23、ate 通常是 1 of N ,但是在并發的情況下是 M of NWho control the change of variation?That is the difference between“Context decide changing of ConcreteState object”and“ConcreteState desice changing of Concrete State”有什么不同State 是由自己控制變化的,而 strategy 通常是由 context 控制變化的。State 自己控制變化的過程是隱式的,外部不知道現在的狀態和下一個狀態將要是什么,而由 cont

24、ext 控制變化的時候,是能夠控制變化的。通常 Strategy 都是完成的同一個功能,State 不一定。How to change? Fixed rules of configuration files都可以,但是個人認為 state 通常是 fixed rules,strategy 通常是 configuration filesCrating and destroying policy?state: 除了第一個之外,都是由 state 自己創建,并且是由 state 自己銷毀的。strategy: 是由 Client 創建,也由 Client 銷毀Common and Variation

25、使用Bridge Pattern實現一個畫圖程序的主體 框架圖形:點、線、矩形、圓線形:實線、虛線、雙線線粗細:1x、2x、4x顏色:RGB由于只有圖形和線形是可變的類型,因此 Bridge Pattern 的兩個繼承樹圖形和線形。問:題目中的后兩個沒有省略號是否意味著將來不會變化?如果不會變化的畫可不可以把線的粗細和顏色RGB當作draw的屬性? HYPERLINK mailto:eryuding 丁老師說:例子只是要你們思考一下Bridge的同時試著綜合一下差異性處理,所以需求沒有給的那么精確,如果實際寫代碼的話,估計線形、粗細和顏色都是draw方法的參數而已,但是如果考試的話,會專門要求

26、上述三個方面獨立處理的,也就是要有三個策略樹以解決三種相互獨立的差異性問:如果使用三個策略樹的話,還算是用了bridge pattern 嗎? HYPERLINK mailto:eryuding 丁老師說:嚴格上講,不是Bridge了,比Bridge更加復雜了,解決了抽象與實現分離的同時,做到了實現自身又包含多種相互獨立差異性的問題。Runtime Registration與真正自實現的Event Style相比,ObserverPattern的不同在哪里?Event Style 中需要管理多種 Event 事件;而 ObserverPattern 只通常只需要管理一種,即自己的變化Event

27、 Style 中 EventRoute 有儲存關于 Event 和 ObserverList 的一個 Dictionary;Observer 中 Subject 只有一個列表Event Style 中 EventRoute 只是一個事件路由,沒有除了一個 Dictionary 沒有其他的屬于自己的信息;而 Observer 中的 Subject 包含有 Subject 信息Event Style 中的事件發起的原因是第三方觸發了事件;而 Observer 中 update 觸發的原因是 Subject 自己的數據發生了變化,雖然是由于第三方的修改,但是時間是由自己觸發的如果眾多Observer

28、的接口不相同怎么辦? 不都是update接口,甚至不是同一種類型使用一個adapter使它們的接口統一用 observer 編寫程序要求:Model: student(ID, name, birthday)View:View1:顯示ID+nameView2:顯示ID+birthdayView3:修改student的三個列View12為observer,model為subject實現每次view3中實現修改后View12自動更新import java.util.*;public class Ob public static void main(String args) Student s = n

29、ew Student(1, A, B); Observer o1 = new View1(s); Observer o2 = new View2(s); View3 v = new View3(s); v.doSomething(); abstract class Observer protected T subject; public Observer(Subject s) s.register(this); this.subject = s; public abstract void update();abstract class Subject private List observer

30、s = new LinkedList(); protected void notifyObserver() for (Observer o : observers) o.update(); public void register(Observer o) observers.add(o); public void remove(Observer o) observers.remove(o); class Student extends Subject private int id; private String name; private String birth; public Studen

31、t(int id, String name, String birth) this.id = id; = name; this.birth = birth; public void setId(int id) this.id = id; notifyObserver(); public int getId() return id; public void setName(String name) = name; notifyObserver(); public String getName() return name; public void setBirth(String birth) this.birth = birth; notifyObserver(); public String getBirth() return birth;class View1 extends Observer int id; String name; public void update() this.id = subject.getId(); = subject.getName(); refresh(); public void r

溫馨提示

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

評論

0/150

提交評論