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

??????????????