【設(shè)計模式】要點1
+public
-private
#protected
~package

創(chuàng)建型:
抽象工廠
12下
1)void Insert(Department department)
2)Department GetDepartment(int id)
3)class SqlserverDepartment implements IDepartment
4)class AccessDepartement implements IDepartment
5)6)interface IFactory{
IDepartment CreateDepartment()}
總結(jié):
1)interface接口類子類的方法,父類也有,父類方法無abstract,且可省略public
2)子類implements實現(xiàn)interface接口父類
生成器
17上
1)public abstract void buildParts()
2)this.pizzaBuilder=pizzaBuilder
3)pizzaBulider.builderParts()
4)waiter.setPizzaBuilder(hawaiian_pizzabuilder)
5)waiter.construct
總結(jié):
1)子類繼承父類方法,父類為abstract抽象
2)同名對象 this.對象=對象
18上
1)public void buildPartA()
2)public Product getResult()
3)public void buildPartA(){
product.setPartA(“Component A”);}
4)public void buildPartB(){
product.setPartB(“Component B”):}
5)public void construct(){
builder.builderPartA()/B()}
原型
13上
1)class WorkExperience implements Cloneable
2)WorkExperience obj=new WorkExperience()
3)class Resume implements Cloneable
4)this.work=(WorkExperience)work.Clone()
5)Resume obj=new Resume(this.work)
6)(Resume)a.Clone()
總結(jié):
1)父類為interface,子類implements實現(xiàn)父類
2)(WorkExperience/Resume)均為強制轉(zhuǎn)換
3)類 對象=new 類(具體)

結(jié)構(gòu)型:
適配器
16上
1)private Address address
2)address.street()
3)address.zip()
4)address.city()
5)DutchAddress addrAdapter=new DutchAddressAdapter(addr)
總結(jié):
1)類中第一行代碼經(jīng)常出現(xiàn) (private或其他)父類 對象,例如:private Address address
2)適配器模式通常轉(zhuǎn)接口,類似usb轉(zhuǎn)type-c,
例如,DutchAddress中的 street轉(zhuǎn)address.street(),postcode轉(zhuǎn)address.zip()
3)類 對象=new 類(具體)
橋接
09上
1)this.imp=imp
2)protected ImageImp imp
3)imp.doPaint(m)
4)Image image1=new BMP()
5)ImageImp imageImp1=new WinImp()
6)image1.setImp(imageImp1)
7)10種圖像5種操作系統(tǒng),橋接需要設(shè)計17個類
總結(jié):
1)橋接模式設(shè)計m種+n種+2=17個類
2)父類 對象名=new 子類名()
13下
1)interface Drawing
2)void drawLine(double x1,double y1,double x2,double y2)
3)void drawCircle(double x,double y,double r)
4)DP1.draw_a_circle(x,y,r)
5)DP2.drawcircle(x,y,r)
6)abstract public void draw()
總結(jié):
1)interface接口父類,子類implements父類,父類方法無abstract抽象,可省略public
2)子類extensd繼承父類,子類繼承父類方法,父類一定abstract抽象,public不省略
public和abstract均可在句首
17下
1)abstract void doPaint(Matrix m)
2)imp.doPaint(m)
3)Image image=new GIFImage()
4)Implementor imageImp=new LinuxImp()
5)image.setImp(imageImp)
組合
09下
1)abstract class AbstractFile
2)public List<AbstractFile>getChildren(){
return null;}
3)4)public List<AbstractFile>getChildren(){
return childList;}
5)printTree(file)
總結(jié):
1)子類extends父類,父類為抽象類
2)List類型前綴無void,abstract,返回值為null等,不是true/false(boolean)
10下
1)abstract class Company
2)this.name=name
3)4)private List<Company>children=new ArrayList<Company>
5)public void Add(Company c){
children.add(c);}
6)public void Delete(Company c){
children.remove(c);}
7)root.Add(comp)
8)comp.Add(comp1)
總結(jié):
1)<>當子類為多態(tài)無法選用公共時,使用父類,例如Company
2)層次結(jié)構(gòu),高套用低
例如,root.Add(comp),comp.Add(comp1)
3)super()調(diào)用父類方法
11上
1)abstract class Menu
2)public abstract void add(MenuComponent menuComponent)
3)menuComponents.add(menuComponent)
4)menuComponent.print()
5)allMenus.print()
總結(jié):
1)注意看方法的(),父類()中沒有子類就沒有,()有子類就有,一般內(nèi)部為具體對象
2)最好的方法一般為print,輸出打印結(jié)果