工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory)-----基于JAVA语言

来源:互联网 发布:php curl发送get请求 编辑:程序博客网 时间:2024/05/16 11:00
  工厂方法模式分为三种:普通工厂模式   多个工厂方法模式   静态工厂方法模式
   1.普通工厂模式,就是建立一个工厂类,对实现了同一接口的产品类进行实例的创建

    例子:
  
  //发送短信和邮件的接口    public interface Sender {          public void Send();      }    //发送邮件的实现类    public class MailSender implements Sender {          public void Send() {              System.out.println("发送邮件!");          }      }      //发送短信的实现类    public class SmsSender implements Sender {          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();          }      }



    2.多个工厂方法模式 是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。

    
  //发送短信和邮件的接口    public interface Sender {          public void Send();      }    //发送邮件的实现类    public class MailSender implements Sender {          public void Send() {              System.out.println("发送邮件!");          }      }      //发送短信的实现类    public class SmsSender implements Sender {          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();          }      }




    3.静态工厂方法模式,将上面的多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。
  public class SendFactory {                public static Sender produceMail(){              return new MailSender();          }                    public static Sender produceSms(){              return new SmsSender();          }      }      //测试类    public class FactoryTest {                public static void main(String[] args) {                  Sender sender = SendFactory.produceMail();              sender.Send();          }      }      



抽象工厂模式(Abstract Factory)

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

           例子:

    
       //发送短信和邮件的接口    public interface Sender {          public void Send();      }    //发送邮件的实现类    public class MailSender implements Sender {          public void Send() {              System.out.println("发送邮件!");          }      }      //发送短信的实现类    public class SmsSender implements Sender {          public void Send() {              System.out.println("发送短信!");          }      }      //给工厂类一个接口    public interface Provider {          public Sender produce();      }      //两个工厂的实现类    public class SendMailFactory implements Provider {          public Sender produce(){              return new MailSender();          }      }      public class SendSmsFactory implements Provider{          public Sender produce() {              return new SmsSender();          }      }      //测试类    public class Test {                public static void main(String[] args) {              Provider provider = new SendMailFactory();              Sender sender = provider.produce();              sender.Send();          }      }  

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


阅读全文
0 0
原创粉丝点击