结构型模式

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);
}

}

Decorator
//对一个类的功能进行扩展时,我可以使用继承,但是不够灵活,所以选用了
//另外的一种形式,引用与继承都可活得对对象的一定的使用能力,而使用引用将更灵活
//我们要保证是对原功能的追加而不是修改,否则只能重写方法,或使用新的方法
//注意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
//先定义一个抽象的Flyweight类
packageFlyweight;
importjava.util.Hashtable;

publicabstractclassFlyweight...{
 
publicabstractvoidoperation();
}


//实现一个具体类
publicclassConcreteFlyweightextendsFlyweight
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值