------------------------------ 享元模式(Flyweight) -------------------------------
(1)主要用于创建对象时,运用共享技术,减少对象对内存的占用.一个提高程序效率和性能的模式,会大大加快程序的运行速度.
就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。
Flyweight(享元)模式中常出现Factory模式。Flyweight的内部状态是用来共享的,Flyweight factory负责维护一个对象存储池(Flyweight Pool)来存放内部状态的对象。
Flyweight的关键思路,在于:
新建对象时:
先到hashtable中进行获取-->判断取得对象是否为空-->若是,则新建此对象,且放回hashtable -->若存在,则共享原来的对象.
(2)实例: (与静态工厂模式进行对比)
public interface Car { public void showCarName(); } class BMWCar implements Car { public void showCarName() { System.out.println("this is the BMWCar ."); } } class FordCar implements Car { public void showCarName() { System.out.println("this is the FordCar ."); } } class CarFactory { public static Car car; public static Car getCar(String name) { if("BMW".equals(name)) { car = new BMWCar(); } if("Ford".equals(name)) { car = new FordCar(); } return car; } } class CarFlyWeightFactory { public Car car; private Hashtable<String,Car> carPool=new Hashtable<String,Car>(); public Car getCar(String name) { if("BMW".equals(name)) { car=carPool.get(name); if(car==null) { car=new BMWCar(); carPool.put(name, car); } } if("Ford".equals(name)) { car=carPool.get(name); if(car==null) { car=new FordCar(); carPool.put(name, car); } } return car; } public int getNumber(){ return carPool.getSize(); } } public class Test { public static void main(String[] args) { CarFlyWeightFactory carFlyWeightFactory=new CarFlyWeightFactory(); Car carf1=carFlyWeightFactory.getCar("Ford"); carf1.showCarName(); Car carf2=carFlyWeightFactory.getCar("Ford"); carf2.showCarName(); if(carf1==carf2) { System.out.println("同一部车来的"); } else { System.out.println("不同一部车来的"); } System.out.println("车的数量是:"+carFlyWeightFactory.getNumber()); } } |
输出:
this is the FordCar .
this is the FordCar .
---------------------- 职责链模式(Chain of Responsibility) -----------------------
(1)Chain of Responsibility职责链模式:
为了避免请求的发送者和接收者之间的耦合关系,使多个接受对象都有机会处理请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
-->要沿着链转发请求,并保证接受者为隐式的,每个链上的对象都有一致的处理请求和访问链上后继者的接口(即如下实例中,在自己方法中再调用一次相同的方法)。
(2)
public class Boy { private boolean hasCar; // 是否有车 private boolean hasHouse; // 是否有房 private boolean hasResponsibility; // 是否有责任心 public Boy() { } public Boy(boolean hasCar, boolean hasHouse, boolean hasResponsibility) { this.hasCar = hasCar; this.hasHouse = hasHouse; this.hasResponsibility = hasResponsibility; } public boolean isHasCar() { return hasCar; } public void setHasCar(boolean hasCar) { this.hasCar = hasCar; } public boolean isHasHouse() { return hasHouse; } public void setHasHouse(boolean hasHouse) { this.hasHouse = hasHouse; } public boolean isHasResponsibility() { return hasResponsibility; } public void setHasResponsibility(boolean hasResponsibility) { this.hasResponsibility = hasResponsibility; } } public interface Handler { public void handleRequest(Boy boy); } public class HouseHandler implements Handler { private Handler handler; public HouseHandler(Handler handler) { this.handler = handler; } public Handler getHandler() { return handler; } public void setHandler(Handler handler) { this.handler = handler; } public void handleRequest(Boy boy) { if (boy.isHasHouse()) { System.out.println("没想到吧,我还有房子"); } else { System.out.println("我也没有房"); handler.handleRequest(boy); } } } public class CarHandler implements Handler { private Handler handler; public CarHandler(Handler handler) { this.handler = handler; } public Handler getHandler() { return handler; } public void setHandler(Handler handler) { this.handler = handler; } public void handleRequest(Boy boy) { if (boy.isHasCar()) { System.out.println("呵呵,我有辆车"); } else { System.out.println("我没有车"); handler.handleRequest(boy); } } } public class ResponsibilityHandler implements Handler { private Handler handler; public ResponsibilityHandler(Handler handler) { this.handler = handler; } public Handler getHandler() { return handler; } public void setHandler(Handler handler) { this.handler = handler; } public void handleRequest(Boy boy) { if (boy.isHasResponsibility()) { System.out.println("我只有一颗带Responsibility的心"); } else { System.out.println("更没有责任心"); handler.handleRequest(boy); } } } public class Girl { public static void main(String[] args) { // 这个boy没有车,也没有房,不过很有责任心 Boy boy = new Boy(false, false, true); // 也可以使用setHanlder方法 Handler handler = new CarHandler(new HouseHandler( new ResponsibilityHandler(null))); handler.handleRequest(boy); } } |
同一部车来的