- 什么是简单工厂模式
一个工厂方法,依据传入的参数,生成对应的具体产品对象; - 补充说明
不属于23种GOF设计模式;
工厂方法一般设成静态方法,返回值一般是抽象类或接口,具体的产品类一般继承或实现抽象类、接口;
优点:产品使用者不需要关心产品类的创建过程,与具体产品的实现类达到解耦的效果;
缺点:违背"开放–封闭"原则(OCP),因为新增一个产品类的时候,需要修改原先的工厂方法;
适用场合:当工厂类负责创建的对象比较少的时候; - 角色
抽象产品
具体产品
具体工厂
产品使用者
具体产品继承抽象产品;
单个具体工厂负责生产不同的具体产品;
产品使用者使用单个具体工厂生产的不同的具体产品; - 例子
类关系图:
代码:
【抽象产品类】首先,定义一个抽象父亲类,拥有一个name属性和打印name的方法,其次还有若干doSomething方法:
package com.pichen.dp.creationalpattern.simplefactory;
public abstract class Father {
private String name;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public abstract void printName();
public abstract void doSomething01();
public abstract void doSomething02();
public abstract void doSomething03();
}
【具体产品类】定义具体的中国父亲类和美国父亲类,继承抽象父亲类,并实现printName等方法:
package com.pichen.dp.creationalpattern.simplefactory;
public class ChineseFather extends Father{
public ChineseFather(String name) {
this.setName(name);
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.getName());
}
@Override
public void doSomething01() {
// TODO Auto-generated method stub
}
@Override
public void doSomething02() {
// TODO Auto-generated method stub
}
@Override
public void doSomething03() {
// TODO Auto-generated method stub
}
}
------------------------------------------------------------------------------------
package com.pichen.dp.creationalpattern.simplefactory;
public class AmericanFather extends Father{
public AmericanFather(String name) {
this.setName(name);
}
@Override
public void printName() {
System.out.println(this.getClass().getName() + ":" + this.getName());
}
@Override
public void doSomething01() {
// TODO Auto-generated method stub
}
@Override
public void doSomething02() {
// TODO Auto-generated method stub
}
@Override
public void doSomething03() {
// TODO Auto-generated method stub
}
}
【简单工厂类】定义一个简单工厂类,并定义一个静态生产方法,根据传进来的type参数生成对应的具体父亲类:
package com.pichen.dp.creationalpattern.simplefactory;
import com.pichen.dp.common.exception.DPException;
public class SimpleFactory {
public static Father produce(String type, String name) throws DPException{
if("cn".equals(type)){
return new ChineseFather(name);
}else if("us".equals(type)){
return new AmericanFather(name);
}else{
throw new DPException("invalid param~");
}
}
}
【产品使用类】开始测试使用产品,首选写个makeMoney方法:由工厂类生产一个父亲类,并开始赚钱,最后返回由工厂生产的具体父亲类实例;
main方法:调用makeMoney方法,并打印具体父亲类的name;
具体看代码:
package com.pichen.dp.creationalpattern.simplefactory;
import com.pichen.dp.common.exception.DPException;
public class Main {
public static Father makeMoney(String type, String name) {
Father father = null;
/* 调用共产方法,创建具体产品类 */
try {
father = SimpleFactory.produce(type, name);
} catch (DPException e) {
System.out.println("exception happend when produce " + type + " obj~");
e.printStackTrace();
}
/* 具体赚钱的步骤 */
if(father != null){
father.doSomething01();
father.doSomething02();
father.doSomething03();
}
return father;
}
public static void main(String[] args) {
Father cnFather = Main.makeMoney("cn", "cn father");
if (cnFather != null)
cnFather.printName();
Father usFather = Main.makeMoney("us", "us father ");
if (usFather != null)
usFather.printName();
Father apple = Main.makeMoney("fruit","apple");
if (apple != null)
apple.printName();
}
}
【其它】异常类DPException:
package com.pichen.dp.common.exception;
public class DPException extends Exception{
/**
*
*/
private static final long serialVersionUID = 8110345414312602844L;
public DPException(String msg){
super(msg);
System.out.println("DPException~");
}
}
普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。
举例如下:(我们举一个发送邮件和短信的例子)
创建二者的共同接口:
public interface Sender {
public void Send();
}
创建实现类:
public class MailSender implemente Sender {
@Override
public void Send(){
System.out.println("这是发送邮件");
}
}
public class SmsSender implemente Sender {
@Override
public void Send(){
System.out.println("这是发送短信!");
}
}
建工厂类:
public class SendFactory {
public Sender produce(String type) {
if ("mail".equals(type)) {
return new MailSender();
} else if ("sms".equals(type)) {
return new SmsSender();
} else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
测试:
public class FactoryTest {
public static void main(String[] args) {
SendFactory factory = new SendFactory();
Sender sender = factory.produce("sms");
sender.Send();
}
}
输出:
这是发送短信!