您的位置:首页 > 编程语言 > Java开发

几种常见设计模式的JAVA实现例子

2011-02-17 19:05 781 查看
重看head first设计模式,为了加深理解,练手把一些设计模式实现了一遍,

贴出来供大家批评指正。

装饰者模式:

package silenceburn;

abstract class Componet{
abstract public void disp();
}

class Wood extends Componet{

@Override
public void disp() {
// TODO Auto-generated method stub
System.out.print(" wood ");
}
}

abstract class Decorator extends Componet{
Componet cp;

public Decorator(Componet cp){
this.cp = cp;
}

public void disp(){
this.decoratorDisp();
cp.disp();
}
abstract protected void decoratorDisp();
}

class RedDecorator extends Decorator{

public RedDecorator(Componet cp) {
super(cp);
// TODO Auto-generated constructor stub
}

@Override
protected void decoratorDisp() {
// TODO Auto-generated method stub
System.out.print(" red ");
}

}

class SmallDecorator extends Decorator{

public SmallDecorator(Componet cp) {
super(cp);
// TODO Auto-generated constructor stub
}

@Override
protected void decoratorDisp() {
// TODO Auto-generated method stub
System.out.print(" Small ");
}

}
public class ConcreteMode {

public static void main(String args[]){
Wood w  = new Wood();
RedDecorator r = new RedDecorator(w);
SmallDecorator s = new SmallDecorator(r);
s.disp();
}
}


说明:可以给Wood加上任意多的装饰者实现,比如此例子中实现了两个装饰者,

一个是红色RED装饰,一个是小SMALL装饰,最终得到的是 红色的 小的 木头。

适配器模式:

package silenceburn;

class M1911{
public void singleFire(){
System.out.print("da! ");
}
}

class M16{
public void threeFire(){
System.out.println("da da da ");
}
}

class M16Adapter extends M16{
M1911 m;
M16Adapter(M1911 m){this.m = m;}

public void threeFire() {

m.singleFire();m.singleFire();m.singleFire();

};
}

public class AdapterMode {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

M1911 m = new M1911();
M16 m16 = new M16Adapter(m);

m16.threeFire();

}

}


说明:M1911是手枪,只能单发“DA!”,M16是突击步枪,可以三连发,“DA DA DA”

通过一个M16适配器,可以让手枪表现的和M16相似,好像是一把M16一样。

代理模式:

package silenceburn;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Read{
public void print();
}

class ReadProxy implements Read{

Read realRead;

ReadProxy(Read a){this.realRead = a;}

@Override
public void print() {
// TODO Auto-generated method stub
int reading = 5;
while(reading -- >0) System.out.println("wait " + reading);
this.realRead.print();
}
}

class DynProxy implements InvocationHandler{

Read real;

DynProxy(Read real){this.real = real;}

@Override
public Object invoke(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
// TODO Auto-generated method stub
int reading = 5;
while(reading -- >0) System.out.println("wait " + reading);

arg1.invoke(real, arg2);

return null;
}

}

class ReadReal implements Read{
@Override
public void print() {
// TODO Auto-generated method stub
System.out.println("this is real content");
}
}

public class ProxyMode {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

//java Dynmanic Proxy
Read r = new ReadReal();
Read rp = (Read) Proxy.newProxyInstance(r.getClass().getClassLoader(), r.getClass().getInterfaces(), new DynProxy(r));
rp.print();

// my define Proxy
Read r2  = new ReadReal();
Read r2p = new ReadProxy(r2);
r2p.print();

}

}


说明:实现了两个代理,功能都是在真正的打印前,打印倒数4,3,2,1,0。

第一个代理自己写代码实现。第二个代理是用JAVA的动态代理功能实现。

其余还有策略模式,观察者模式,状态模式,工厂模式的代码,比较常见,就不贴了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: