Adapter

classAdapteea...{
publicvoidkk()...{}
}

interfaceTargeta...{
Stringvv(inti,intk);
}

classAdapteraimplementsTargeta...{
Adapteeaade;

publicAdaptera(Adapteeaade)...{
this.ade=ade;
}

publicStringvv(inti,intk)...{
//具体的业务方法实现在Adaptee中,这个方法
//只起到了接口转换的作用
//调用此方法是通过引用
ade.kk();
returnnull;
}
}
//使用继承形式的
classAdapteeb...{
publicvoidkk()...{}
}

interfaceTargetb...{
Stringvv(inti,intk);
}

classAdapterbextendsAdapteebimplementsTargetb...{
publicStringvv(inti,intk)...{
//调用此方法是通过继承
kk();
returnnull;
}
}
Proxy

interfaceSubject...{
voidrequest();
}

classrealSubjectimplementsSubject...{
publicvoidrequest()...{
//dotherealbusiness
}
}

classProxyimplementsSubject...{
Subjectsubject;

publicProxy(Subjectsubject)...{
this.subject=subject;
}

publicvoidrequest()...{
System.out.println("dosomething");
subject.request();
System.out.println("dosomething");
}
}
Bridge

interfaceImp...{
voidoperation();
}

classCimp1implementsImp...{
publicvoidoperation()...{
System.out.println("1");
}
}

classCimp2implementsImp...{
publicvoidoperation()...{
System.out.println("2");
}
}

classInvoker...{
Impimp=newCimp1();

publicvoidinvoke()...{
imp.operation();
}
}
Composite

interfaceComponent...{
voidoperation();
voidadd(Componentcomponent);
voidremove(Componentcomponent);
}

classLeafimplementsComponent...{
publicvoidoperation()...{
System.out.println("anoperation");
}

publicvoidadd(Componentcomponent)...{
thrownewUnsupportedOperationException();
}

publicvoidremove(Componentcomponent)...{
thrownewUnsupportedOperationException();
}
}

classCompositeimplementsComponent...{
Listcomponents=newArrayList();

publicvoidoperation()...{
Componentcomponent=null;
Iteratorit=components.iterator();
while(it.hasNext())...{
//不知道此component对象是leaf还是composite,
//如果是leaf则直接实现操作,如果是composite则继续递归调用
component=(Component)it.next();
component.operation();
}
}

publicvoidadd(Componentcomponent)...{
components.add(component);
}

publicvoidremove(Componentcomponent)...{
components.remove(component);
}
}
//对一个类的功能进行扩展时,我可以使用继承,但是不够灵活,所以选用了
//另外的一种形式,引用与继承都可活得对对象的一定的使用能力,而使用引用将更灵活
//我们要保证是对原功能的追加而不是修改,否则只能重写方法,或使用新的方法
//注意concrete的可以直接new出来,
//而decorator的则需要用一个另外的decorator对象才能生成对象
//使用对象封装,和公用接口
//Decorator链上可以有多个元素

interfaceComponenta...{
voidoperation();
}

classConcreteComponentimplementsComponenta...{
publicvoidoperation()...{
System.out.println("dosomething");
}
}

classDecoratorimplementsComponenta...{
privateComponentacomponent;

publicDecorator(Componentacomponent)...{
this.component=component;
}

publicvoidoperation()...{
//dosomethingbefore
component.operation();
//dosomethingafter
}
}
Facade

classObj1...{
publicvoidope1()...{}
publicvoidope2()...{}
}

classObj2...{
publicvoidope1()...{}
publicvoidope2()...{}
}

classFacade...{
//我得到了一个简洁清晰的接口
publicvoidfdMethod()...{
Obj1obj1=newObj1();
Obj2obj2=newObj2();
obj1.ope1();
obj2.ope2();
}
}
//先定义一个抽象的Flyweight类
packageFlyweight;
importjava.util.Hashtable;

publicabstractclassFlyweight...{
publicabstractvoidoperation();
}
//实现一个具体类
publicclassConcreteFlyweightextendsFlyweight

被折叠的 条评论
为什么被折叠?



