java设计模式---工厂模式

来源:互联网 发布:linux file命令 源码 编辑:程序博客网 时间:2024/06/10 21:12

Java设计模式之工厂模式

java常用的工厂模式概论:

工厂模式分两种:1.工厂方法模式 2. 抽象工厂模式

1.工厂方法模式又分为三种:普通工厂模式多个工厂方法模式静态工厂方法模式

1.1 普通方法模式
就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。首先看下关系图:
这里写图片描述

接下来我们再结合代码看一下具体怎么实现的
首先定义一个接口

public interface SendMsg {    public void sendMsg();}

其次,创建实现类:

public class Phone implements SendMsg{    @Override    public void sendMsg() {        System.out.println("send msg by phone");    }}
public class Computer implements SendMsg{    @Override    public void sendMsg() {        System.out.println("send msg by computer");    }}

然后创建一个工厂类,根据参入的参数调用不用的方法

public class SendMsgFactory {    private  static Logger logger = Logger.getLogger(SendMsgFactory.class);    public SendMsg send(String type){        if(StringUtils.equals("phone",type)){            return new Phone();        }else if(StringUtils.equals("computer",type)){            return new Computer();        }else{            logger.info("传入的类型不对");            return null;        }    }}

然后我们创建一个测试类:

public class FactoryTest {    public static void main(String[] args) {        SendMsgFactory factory = new SendMsgFactory();        SendMsg send = factory.send("computer");        send.sendMsg();    }}

运行结果:send msg by computer

1.2 多个工厂方法模式
是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。关系图如下:
这里写图片描述

和普通工厂不同的是多个工厂方法模式的工厂类变了

public class SendMsgFactory {    public SendMsg sendByPhone(){        return new Phone();    }    public SendMsg sendByComputer(){        return new Computer();    }}

这样测试类就不用传相应的type

public class FactoryTest {    public static void main(String[] args) {        SendMsgFactory factory = new SendMsgFactory();        SendMsg send = factory.sendByPhone();        send.sendMsg();    }}

运行结果:send msg by phone

1.3 静态工厂方法模式
将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。

public class SendMsgFactory {    public static SendMsg sendByPhone(){        return new Phone();    }    public static SendMsg sendByComputer(){        return new Computer();    }}

测试类修改为不用new工厂类实例

public class FactoryTest {    public static void main(String[] args) {        SendMsg send = SendMsgFactory.sendByComputer();        send.sendMsg();    }}

2.抽象工厂模式
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这样一旦需要增加新的功能,直接增加新的工厂类就可以了,不需要修改之前的代码。关系图如下:
这里写图片描述

代码如下:
定义业务接口:

public interface SendMsg {    public void sendMsg();}

定义业务实现类:

public class Phone implements SendMsg{    @Override    public void sendMsg() {        System.out.println("send msg by phone");    }}
public class Computer implements SendMsg{    @Override    public void sendMsg() {        System.out.println("send msg by computer");    }}

定义工厂类接口:

public interface Factory {    public SendMsg sendBy();}

定义工厂接口实现类:

public class FactoryPhone implements Factory{    @Override    public SendMsg sendBy() {        return new Phone();    }}
public class FactoryComputer implements Factory{    @Override    public SendMsg sendBy() {        return new Computer();    }}

这样一来,我们测试类直接使用向上造型:

public class FactoryTest {    public static void main(String[] args) {        Factory factory = new FactoryComputer();        SendMsg sendMsg = factory.sendBy();        sendMsg.sendMsg();    }}

其实这个模式的好处就是,如果你现在想增加一个功能:发及时信息,则只需做一个实现类,实现SendMsg接口,同时做一个工厂类,实现Factory接口,就OK了,无需去改动现成的代码。这样做,拓展性较好!

===============
第一篇博客,很激动。希望对那些刚步入Java行业的小伙伴有些帮助author:头

1 0
原创粉丝点击