[原创]设计模型探索I2-Factory method parttern 之盘古开天
工厂方法模式:
该模式有一个抽象基类和若干个派生的具体工厂类,基类定义了一个虚工厂方法返回指定产品类的基类,派生类需要实现该虚方法并创建具体产品类返回。注意工厂方法的每个具体工厂只负责返回一种产品类。
工厂和产品是一种一一对应的平行的关系.
GOF对工厂方法模式的概扩是:定义一个创建对象的接口,让子类决定实例化哪一个类,使一个类的实例化沿迟到其子类中.
工厂方法的组成部份:
1.产品基类
2.具体产品
3.工厂基类
4.具体工厂基类
工厂方法组成部份之间的关系:
例子:
手机基类:
手机5233
手机5235
工厂基类
5233工厂
5235工厂
测试类
优点:
该模式有一个抽象基类和若干个派生的具体工厂类,基类定义了一个虚工厂方法返回指定产品类的基类,派生类需要实现该虚方法并创建具体产品类返回。注意工厂方法的每个具体工厂只负责返回一种产品类。
工厂和产品是一种一一对应的平行的关系.
GOF对工厂方法模式的概扩是:定义一个创建对象的接口,让子类决定实例化哪一个类,使一个类的实例化沿迟到其子类中.
工厂方法的组成部份:
1.产品基类
2.具体产品
3.工厂基类
4.具体工厂基类
工厂方法组成部份之间的关系:
例子:
手机基类:
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[Phone]</li>
* <li>Description: [手机抽象类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class Phone
{
/** *//** 手机型号 */
public String model;
/** *//**
*<ul>
*<li>Description:[构造方法]</li>
*<ul>
* @param model
*/
public Phone(String model)
{
this.model = model;
}
/** *//**
* <ul>
* <li>Description:[打电话]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void call();
/** *//**
* <ul>
* <li>Description:[听电话]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void answer();
/** *//**
* <ul>
* <li>Description:[发信息]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void sendMsg();
/** *//**
* <ul>
* <li>Description:[收信息]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void receiveMsg();
}
/** */ /**
* <ul>
* <li>Title:[Phone]</li>
* <li>Description: [手机抽象类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class Phone
{
/** *//** 手机型号 */
public String model;
/** *//**
*<ul>
*<li>Description:[构造方法]</li>
*<ul>
* @param model
*/
public Phone(String model)
{
this.model = model;
}
/** *//**
* <ul>
* <li>Description:[打电话]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void call();
/** *//**
* <ul>
* <li>Description:[听电话]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void answer();
/** *//**
* <ul>
* <li>Description:[发信息]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void sendMsg();
/** *//**
* <ul>
* <li>Description:[收信息]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public abstract void receiveMsg();
}
手机5233
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[Nokia5233]</li>
* <li>Description: [诺基亚5233]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5233 extends Phone
{
public Nokia5233()
{
super("5233");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#answer()
*/
public void answer()
{
System.out.println(this.model+"接听电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#call()
*/
public void call()
{
System.out.println(this.model+"打电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#receiveMsg()
*/
public void receiveMsg()
{
System.out.println(this.model+"收短信");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#sendMsg()
*/
public void sendMsg()
{
System.out.println(this.model+"发短信");
}
}
/** */ /**
* <ul>
* <li>Title:[Nokia5233]</li>
* <li>Description: [诺基亚5233]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5233 extends Phone
{
public Nokia5233()
{
super("5233");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#answer()
*/
public void answer()
{
System.out.println(this.model+"接听电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#call()
*/
public void call()
{
System.out.println(this.model+"打电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#receiveMsg()
*/
public void receiveMsg()
{
System.out.println(this.model+"收短信");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#sendMsg()
*/
public void sendMsg()
{
System.out.println(this.model+"发短信");
}
}
手机5235
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[Nokia5235]</li>
* <li>Description: [诺基亚5235]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5235 extends Phone
{
public Nokia5235()
{
super("5235");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#answer()
*/
public void answer()
{
System.out.println(this.model+"接听电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#call()
*/
public void call()
{
System.out.println(this.model+"打电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#receiveMsg()
*/
public void receiveMsg()
{
System.out.println(this.model+"收短信");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#sendMsg()
*/
public void sendMsg()
{
System.out.println(this.model+"发短信");
}
}
/** */ /**
* <ul>
* <li>Title:[Nokia5235]</li>
* <li>Description: [诺基亚5235]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5235 extends Phone
{
public Nokia5235()
{
super("5235");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#answer()
*/
public void answer()
{
System.out.println(this.model+"接听电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#call()
*/
public void call()
{
System.out.println(this.model+"打电话");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#receiveMsg()
*/
public void receiveMsg()
{
System.out.println(this.model+"收短信");
}
/**//* (non-Javadoc)
* @see simpleFactory.Phone#sendMsg()
*/
public void sendMsg()
{
System.out.println(this.model+"发短信");
}
}
工厂基类
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[PhoneFactory]</li>
* <li>Description: [手机工厂]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class PhoneFactory
{
/** *//**
* <ul>
* <li>Description:[创建手机]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract Phone createPhone();
}
/** */ /**
* <ul>
* <li>Title:[PhoneFactory]</li>
* <li>Description: [手机工厂]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public abstract class PhoneFactory
{
/** *//**
* <ul>
* <li>Description:[创建手机]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
*/
public abstract Phone createPhone();
}
5233工厂
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[Nokia5233Factory]</li>
* <li>Description: [诺基亚5233工厂]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5233Factory extends PhoneFactory
{
/**//* (non-Javadoc)
* @see methodFactory.PhoneFactory#createPhone()
*/
public Phone createPhone()
{
return new Nokia5233();
}
}
/** */ /**
* <ul>
* <li>Title:[Nokia5233Factory]</li>
* <li>Description: [诺基亚5233工厂]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Nokia5233Factory extends PhoneFactory
{
/**//* (non-Javadoc)
* @see methodFactory.PhoneFactory#createPhone()
*/
public Phone createPhone()
{
return new Nokia5233();
}
}
5235工厂
package
methodFactory;
public class Nokia5235Factory extends PhoneFactory
{
/**//* (non-Javadoc)
* @see methodFactory.PhoneFactory#createPhone()
*/
public Phone createPhone()
{
return new Nokia5235();
}
}
public class Nokia5235Factory extends PhoneFactory
{
/**//* (non-Javadoc)
* @see methodFactory.PhoneFactory#createPhone()
*/
public Phone createPhone()
{
return new Nokia5235();
}
}
测试类
package
methodFactory;
/** */ /**
* <ul>
* <li>Title:[测试类]</li>
* <li>Description: [type of functional description]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Test
{
/** *//**
* <ul>
* <li>Description:[method of functional description]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param args
*/
public static void main(String[] args)
{
Phone phone = new Nokia5233Factory().createPhone();
phone.call();
phone = new Nokia5235Factory().createPhone();
phone.receiveMsg();
}
}
/** */ /**
* <ul>
* <li>Title:[测试类]</li>
* <li>Description: [type of functional description]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Test
{
/** *//**
* <ul>
* <li>Description:[method of functional description]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param args
*/
public static void main(String[] args)
{
Phone phone = new Nokia5233Factory().createPhone();
phone.call();
phone = new Nokia5235Factory().createPhone();
phone.receiveMsg();
}
}
使用工厂方法可以让用户的代码和某一个特定类的了类的代码解耦.
工厂方法使用户不必知道它所使用的对象是如何被创建出来的,只需要知道该对象有哪些方法.