【设计模式】要点1
+public
-private
#protected
~package

创建型:
抽象工厂
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()}
总结:
1)interface接口类子类的方法,父类也有,父类方法无abstract,且可省略public
2)子类implements实现interface接口父类
生成器
17上
1)public abstract void buildParts()
2)this.pizzaBuilder=pizzaBuilder
3)pizzaBulider.builderParts()
4)waiter.setPizzaBuilder(hawaiian_pizzabuilder)
5)waiter.construct
总结:
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()
总结:
1)父类为interface,子类implements实现父类
2)(WorkExperience/Resume)均为强制转换
3)类 对象=new 类(具体)

结构型:
适配器
16上
1)private Address address
2)address.street()
3)address.zip()
4)address.city()
5)DutchAddress addrAdapter=new DutchAddressAdapter(addr)
总结:
1)类中第一行代码经常出现 (private或其他)父类 对象,例如:private Address address
2)适配器模式通常转接口,类似usb转type-c,
例如,DutchAddress中的 street转address.street(),postcode转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种操作系统,桥接需要设计17个类
总结:
1)桥接模式设计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()
总结:
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)
总结:
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)
总结:
1)<>当子类为多态无法选用公共时,使用父类,例如Company
2)层次结构,高套用低
例如,root.Add(comp),comp.Add(comp1)
3)super()调用父类方法
11上
1)abstract class Menu
2)public abstract void add(MenuComponent menuComponent)
3)menuComponents.add(menuComponent)
4)menuComponent.print()
5)allMenus.print()
总结:
1)注意看方法的(),父类()中没有子类就没有,()有子类就有,一般内部为具体对象
2)最好的方法一般为print,输出打印结果