设计模式(4)---工厂方法模式

1.简单工厂模式(simple factory)

   简单工厂模式属于创建型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

1 package SimpleFactory;
2 
3 public abstract class Animal {
4 
5     public abstract void eat();
6 
7 }
 1 package SimpleFactory;
 2 
 3 public class Dog extends Animal{
 4 
 5     public Dog() {
 6         
 7     }
 8 
 9     @Override
10     public void eat() {
11         System.out.println("狗吃肉");    
12     }
13     
14 }
 1 package SimpleFactory;
 2 
 3 public class Cat extends Animal{
 4 
 5     public Cat() {
 6     }
 7 
 8     @Override
 9     public void eat() {
10         System.out.println("猫吃鱼");    
11     }
12     
13 }
 1 package SimpleFactory;
 2 
 3 public class AnimalFactory {
 4 
 5     private AnimalFactory() {
 6 
 7     }
 8     
 9     public static Animal createAnimal(String type){
10         if ("dog".equals(type)){
11             return new Dog() ;
12         }else if ("cat".equals(type)){
13             return new Cat() ;
14         }
15         return null;
16     }
17 
18 }
 1 package SimpleFactory;
 2 
 3 public class Test {
 4 
 5     public Test() {
 6         
 7     }
 8     
 9     public static void main(String[] args) {
10         Animal a = AnimalFactory.createAnimal("dog") ;
11         a.eat();
12         a = AnimalFactory.createAnimal("cat") ;
13         a.eat();
14     }
15 
16 }

优点:客户端不再负责对象的创建,从而明确了各个类的职责。

缺点:如果有新的产品类增加,要修改工厂类,不利于后期维护,违反了开闭原则。

 

 

2.工厂方法模式 Factory Method (创建型模式)

 

意图:定义一个用于创建对象的接口,让子类决定实例化哪一个类。 Factory Method使一个类的实例化延迟到其子类。

4. 适用性
在下列情况下可以使用 Factory Method模式:
• 当一个类不知道它所必须创建的对象的类的时候。
• 当一个类希望由它的子类来指定它所创建的对象的时候。
• 当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类
是代理者这一信息局部化的时候。

1 package Factory;
2 
3 public interface Animal {
4      public void eat();
5 }
 1 package Factory;
 2 
 3 public class Dog implements Animal {
 4 
 5     @Override
 6     public void eat() {
 7         System.out.println("狗吃肉");
 8 
 9     }
10 
11 }
 1 package Factory;
 2 
 3 public class Cat implements Animal {
 4 
 5     @Override
 6     public void eat() {
 7         System.out.println("猫吃鱼");
 8     }
 9 
10 }
1 package Factory;
2 
3 public interface Factory {
4     public Animal createAnimal();
5 }
 1 package Factory;
 2 
 3 public class CatFactory implements Factory {
 4 
 5     public CatFactory() {
 6         // TODO Auto-generated constructor stub
 7     }
 8 
 9     @Override
10     public Animal createAnimal() {
11         // TODO Auto-generated method stub
12         return new Cat();
13     }
14 
15 }
 1 package Factory;
 2 
 3 public class DogFactory implements Factory {
 4 
 5     public DogFactory() {
 6         // TODO Auto-generated constructor stub
 7     }
 8 
 9     @Override
10     public Animal createAnimal() {
11         
12         return new Dog();
13     }
14 
15 }
 1 package Factory;
 2 
 3 public class Test {
 4 
 5     public Test() {
 6         // TODO Auto-generated constructor stub
 7     }
 8     
 9     public static void main(String[] args) {
10         Factory f = new DogFactory() ;
11         Animal a = f.createAnimal();
12         a.eat();
13         
14         f = new CatFactory() ;
15         a = f.createAnimal() ;
16         a.eat();
17     }
18 
19 }

优点:如果有新的对象增加,只要增加一个具体的类和具体的工厂类即可,不影响原有的代码,提高了系统的维护性和可扩展性。

缺点:需要额外的代码。

原文地址:https://www.cnblogs.com/mengchunchen/p/5731107.html