[原创]设计模型探索I1-Simple factory parttern 之盘古开天
面临问题:
由于需求的变化,这个对对象的具体实现经常需要剧烈变化,但是它却拥有着较稳定的接口.
解决办法:
如何应对这种变化,需要提供一种"封装机制"来隔离这种变化,从而保持系统中依赖该对象的对象随着需求变化而变化.
简单工厂模式:
严格说并不是一个设计模式。简单工厂没有抽象类,只有一个具体工厂类如MyFactory,然后MyFactory里面有个工厂方法CreateProduct返回一个基类产品,具体返回什么具体实例通过传入参数然后用case判断。
简单工厂模式的组成部份:
1.产品基类
2.具体产品
3.产品工厂
简单工厂各部份的组成关系
例子:
1. 手机基类
2. nokia5233
3. nokia5235
4.手机工厂
5.测试类
很明显的缺点就是 Nokia每发明一个新型号的手机都需要修改简单工厂类(增加if判断),违反了封闭修改,开放扩展原则。
由于需求的变化,这个对对象的具体实现经常需要剧烈变化,但是它却拥有着较稳定的接口.
解决办法:
如何应对这种变化,需要提供一种"封装机制"来隔离这种变化,从而保持系统中依赖该对象的对象随着需求变化而变化.
简单工厂模式:
严格说并不是一个设计模式。简单工厂没有抽象类,只有一个具体工厂类如MyFactory,然后MyFactory里面有个工厂方法CreateProduct返回一个基类产品,具体返回什么具体实例通过传入参数然后用case判断。
简单工厂模式的组成部份:
1.产品基类
2.具体产品
3.产品工厂
简单工厂各部份的组成关系
例子:
1. 手机基类
package
simpleFactory;
/** */ /**
* <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();
}
2. nokia5233
package
simpleFactory;
/** */ /**
* <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+"发短信");
}
}
3. nokia5235
package
simpleFactory;
/** */ /**
* <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+"发短信");
}
}
4.手机工厂
package
simpleFactory;
/** */ /**
* <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 class PhoneFactory
{
/** *//**
* <ul>
* <li>Description:[生产手机]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param model
* @return
*/
public static Phone createPhone(String model)
{
if ("5233".endsWith(model))
{
return new Nokia5233();
}
else if ("5235".endsWith(model))
{
return new Nokia5235();
}
return null;
}
}
/** */ /**
* <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 class PhoneFactory
{
/** *//**
* <ul>
* <li>Description:[生产手机]</li>
* <li>Created by [Huyvanpull] [2011-8-1]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @param model
* @return
*/
public static Phone createPhone(String model)
{
if ("5233".endsWith(model))
{
return new Nokia5233();
}
else if ("5235".endsWith(model))
{
return new Nokia5235();
}
return null;
}
}
5.测试类
package
simpleFactory;
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 = PhoneFactory.createPhone("5233");
phone.call();
phone = PhoneFactory.createPhone("5235");
phone.receiveMsg();
}
}
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 = PhoneFactory.createPhone("5233");
phone.call();
phone = PhoneFactory.createPhone("5235");
phone.receiveMsg();
}
}
很明显的缺点就是 Nokia每发明一个新型号的手机都需要修改简单工厂类(增加if判断),违反了封闭修改,开放扩展原则。