Java设计模式为: 二十三种设计模式,本文介绍十二个常用设计模式: strategy(策略模式)、static FactoryMethod(静态工厂)、singelton(单例模式)、Observer(观察者模式)、Iterator(迭代器模式)、Facade(外观模式)、adapter(适配器模式)、Proxy(代理模式)、state(状态模式)、Flyweight(享元模式)、Chainof Responsibility(职责链模式)、Memento(备忘录模式)。
【博文字符限制,接"Java常用设计模式1"(
http://blog.youkuaiyun.com/zms2100/article/details/23579901)的后四个设计模式:】
------------------- Flyweight(享元模式)---------------------------
(1)主要用于创建对象时,运用共享技术,减少对象对内存的占用.一个提高程序效率和性能的模式,会大大加快程序的运行速度.就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。
Flyweight(享元)模式中常出现Factory模式。Flyweight的内部状态是用来共享的,Flyweightfactory负责维护一个对象存储池(Flyweight Pool)来存放内部状态的对象。
Flyweight的关键思路,在于:
新建对象时:
先到hashtable中进行获取-->判断取得对象是否为空-->若是,则新建此对象,且放回hashtable-->若存在,则共享原来的对象.
新建对象时:
先到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 .
同一部车来的
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);
}
}
==>
如何实例使请求沿着链在各接受对象中传递,当没被第一个接受对象接受时,会传递给第二个对象,若被第一个对象接受了,则不传递下去:
1.各具体的接受对象采用这样的构造方法:
public CarHandler(Handler handler) { this.handler = handler;}
2.各具体的接受对象实现接口的方法handleRequest()中.在调用时,若被接受,则执行true的内容,若不被接受,则执行false的内容,并继续调用再调用handleRequest()方法.
3.在最后的测试类中,生成具体的handler时,用多层包含的形式.这样,在调用了上一层car的方法后,会调用house的相应方法,最后再调用ResponsibilityHandler的方法.
如何实例使请求沿着链在各接受对象中传递,当没被第一个接受对象接受时,会传递给第二个对象,若被第一个对象接受了,则不传递下去:
1.各具体的接受对象采用这样的构造方法:
public CarHandler(Handler handler) { this.handler = handler;}
2.各具体的接受对象实现接口的方法handleRequest()中.在调用时,若被接受,则执行true的内容,若不被接受,则执行false的内容,并继续调用再调用handleRequest()方法.
3.在最后的测试类中,生成具体的handler时,用多层包含的形式.这样,在调用了上一层car的方法后,会调用house的相应方法,最后再调用ResponsibilityHandler的方法.
==>前两个handler是采用了有参数的构造方法,最后一个是采用了为NULL的构造方法
---------------------- Memento(备忘录模式)------------------------
(1)备忘录模式属于行为型模式,其意图是在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这
个状态,这样以后就可以将对象恢复到原先保存的状态。
(2)实例如下:
有一个对象Employee.除了属性外,还需要一个保存,还原状态的方法.
有一个对象Memento,用来记录Employee每一个时刻的状态,
CareTaker,用来保存,拿回Memento.需要一个保存,还原状态的方法.->需要一个指针,一个容器.
有一个对象Employee.除了属性外,还需要一个保存,还原状态的方法.
有一个对象Memento,用来记录Employee每一个时刻的状态,
CareTaker,用来保存,拿回Memento.需要一个保存,还原状态的方法.->需要一个指针,一个容器.
package memento;
public class Memento{
String name;
int age;
public Memento(String name,int age){
this.name = name;
this.age = age;
}
}
Employee模式:
package memento;
public class Employee{
private String name;
private int age;
public Employee(String aName,int aAge){
name = aName;
age = aAge;
}
public void setName(String aName){
name = aName;
}
public void setAge(int aAge){
age = aAge;
}
public Memento saveMemento(){
return new Memento(name,age);
}
public void restoreMemento(Memento memento){
age = memento.age;
name = memento.name;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
CareTaker代码:
package memento;
import java.util.Vector;
public class CareTaker{
private Vector v;
private int current;
public CareTaker(){
current = -1;
v = new Vector();
}
public void setMemento(Memento mem){
current ++;
v.add(mem);
}
public Memento getMemento(){
if(current>0){
current --;
return(Memento) v.get(current);
}
return null;
}
}
Client代码:
package memento;
public class Client{
public static void show(Employee e){
System.out.println("-----------------------------------");
System.out.println("Name:"+e.getName());
System.out.println("Age:" + e.getAge());
System.out.println("-----------------------------------");
}
public static void main(String[] args){
Employee e = new Employee("lili",25);
CareTaker ct = new CareTaker();
show(e);
ct.setMemento(e.saveMemento());
e.setName("litianli");
show(e);
ct.setMemento(e.saveMemento());
e.setAge(45);
show(e);
ct.setMemento(e.saveMemento());
//restore
e.restoreMemento(ct.getMemento());
show(e);
e.restoreMemento(ct.getMemento());
show(e);
}
}
三人行,必有我师。 Bad artists copy;Goodartist steal.
转自 “Changes we need!” 博客:http://shenzhenchufa.blog.51cto.com/730213/161581