Introduction and simple examples of factory pattern (static factory pattern, factory method pattern, abstract factory pattern) in java va

Keywords: Java Junit

Java Medium Factory Model Java ee is a common pattern, which can be divided into three types: static factory mode, factory method mode and abstract factory mode. Let's give a brief introduction and examples.

 

Static Factory Mode: As its name implies, it is realized by static method. The objects it creates have certain characteristics, such as derivation of a class or implementation of an interface. It's relatively simple. Examples are as follows

 

Class Animal:

 

  1. package com.bean;  
  2.   
  3. /** 
  4.  * Animals 
  5.  * @author Lyon Yao 
  6.  * 
  7.  */  
  8. public abstract class Animal {  
  9.       
  10.     private String name;  
  11.       
  12.     public Animal() {  
  13.         super();  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.     public Animal(String name) {  
  17.         super();  
  18.         this.name = name;  
  19.     }  
  20.     public abstract void eat();  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name) {  
  25.         this.name = name;  
  26.     }  
  27.       
  28. }  

Cats:

 

  1. package com.bean;  
  2.   
  3. /** 
  4.  * Feline 
  5.  * @author Lyon Yao 
  6.  * 
  7.  */  
  8. public class Cat extends Animal {  
  9.   
  10.     public Cat() {  
  11.         // TODO Auto-generated constructor stub  
  12.     }  
  13.   
  14.     public Cat(String name) {  
  15.         super(name);  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.   
  19.     @Override  
  20.     public void eat() {  
  21.         // TODO Auto-generated method stub  
  22.         System.out.println("I like to eat fish!");  
  23.     }  
  24.   
  25. }  
 

 

Dogs: package com.bean;

  1. /** 
  2.  * Dogs 
  3.  * @author Lyon Yao 
  4.  * 
  5.  */  
  6. public class Dog extends Animal {  
  7.   
  8.     public Dog() {  
  9.         // TODO Auto-generated constructor stub  
  10.     }  
  11.   
  12.     public Dog(String name) {  
  13.         super(name);  
  14.         // TODO Auto-generated constructor stub  
  15.     }  
  16.   
  17.     @Override  
  18.     public void eat() {  
  19.         // TODO Auto-generated method stub  
  20.         System.out.println("I like to eat bone!");  
  21.     }  
  22.   
  23. }  
 

 

Static factory class:

 

  1. package com.factory.sta;  
  2.   
  3. import java.lang.reflect.Constructor;  
  4. import java.lang.reflect.InvocationTargetException;  
  5.   
  6. /** 
  7.  * Static factory creates an object. Objects generated by static factory classes generally share common characteristics, inherit a class, or refer to interfaces, etc. 
  8.  * No seemingly nonexistent, but it is undeniable that they are all subclasses of Object or Object. 
  9.  * @author Lyon Yao 
  10.  * 
  11.  */  
  12. public class StaticFatory {  
  13.     public static Object getInstance(String className){  
  14.         Object instance=null;  
  15.         try {  
  16.             Class cls=Class.forName(className);  
  17.             instance= cls.newInstance();  
  18.         } catch (ClassNotFoundException e) {  
  19.             // TODO Auto-generated catch block  
  20.             e.printStackTrace();  
  21.         } catch (InstantiationException e) {  
  22.             // TODO Auto-generated catch block  
  23.             e.printStackTrace();  
  24.         } catch (IllegalAccessException e) {  
  25.             // TODO Auto-generated catch block  
  26.             e.printStackTrace();  
  27.         }  
  28.         return instance;  
  29.           
  30.     }  
  31.     public static Object getInstance(String className,Object ...agrs) {  
  32.         Class cls=null;  
  33.         try {  
  34.             cls = Class.forName(className);  
  35.         } catch (ClassNotFoundException e1) {  
  36.             // TODO Auto-generated catch block  
  37.             return null;  
  38.         }  
  39.         Constructor[] constructors = cls.getConstructors();  
  40.         Object instance=null;  
  41.         for(Constructor cons:constructors){  
  42.             Class <?>[] clses=cons.getParameterTypes();  
  43.             if(clses.length>0){  
  44.                 boolean isThisConstructor=true;  
  45.                 for(int i=0;i<clses.length;i++){  
  46.                     Class c=clses[i];   
  47.                     if(! c.isInstance(agrs[i]) ){  
  48.                         isThisConstructor=false;  
  49.                     }  
  50.                 }  
  51.                 if(isThisConstructor){  
  52.                     try {  
  53.                         instance=cons.newInstance(agrs);  
  54.                         break;  
  55.                     } catch (IllegalArgumentException e) {  
  56.                         // TODO Auto-generated catch block  
  57.                         e.printStackTrace();  
  58.                     } catch (InvocationTargetException e) {  
  59.                         // TODO Auto-generated catch block  
  60.                         e.printStackTrace();  
  61.                     } catch (InstantiationException e) {  
  62.                         // TODO Auto-generated catch block  
  63.                         e.printStackTrace();  
  64.                     } catch (IllegalAccessException e) {  
  65.                         // TODO Auto-generated catch block  
  66.                         e.printStackTrace();  
  67.                     }  
  68.                 }else{  
  69.                     continue;  
  70.                 }  
  71.                   
  72.             }  
  73.         }  
  74.         return instance;  
  75.     }  
  76. }  

 

 

 

 

Factory Method Model: It mainly classifies the production of different types of things, but the object of classified production still has certain characteristics. For example, the static factory in front of us is a comprehensive car factory. Whether cars or trains can be produced, and the factory method mode is the specific division of labor. The factory that makes cars only makes cars, and the factory that makes trains only makes trains, whether cars or trains but cars.

Specific code examples are as follows: (The example here is the same as the example above, with dogs many puppies, cats many kittens, if not specific, then the mother can be born)

 

Maternal animal interface:

 

  1. package com.factory;  
  2.   
  3. import com.bean.Animal;  
  4.   
  5. /** 
  6.  * Mother interface 
  7.  * @author Lyon 
  8.  * 
  9.  */  
  10. public interface AnimalMother {  
  11.     /** 
  12.      * Reproductive animals 
  13.      * @return 
  14.      */  
  15.     public  Animal giveBirth();  
  16. }  

 

Bitches:

 

  1. package com.factory.impl;  
  2.   
  3. import com.bean.Animal;  
  4. import com.bean.Dog;  
  5. import com.factory.AnimalMother;  
  6.   
  7. /** 
  8.  * Mother Dog 
  9.  * @author Lyon Yao 
  10.  * 
  11.  */  
  12. public class DogMother implements AnimalMother {  
  13.   
  14.     @Override  
  15.     public Animal giveBirth() {  
  16.         // TODO Auto-generated method stub  
  17.         Animal dog=new Dog();  
  18.         System.out.println("The mother of the dog gave birth to a puppy.);  
  19.         return dog;  
  20.     }  
  21.   
  22. }  
 

 

Female cat:

 

  1. package com.factory.impl;  
  2.   
  3. import com.bean.Animal;  
  4. import com.bean.Cat;  
  5. import com.factory.AnimalMother;  
  6.   
  7. /** 
  8.  * Mother cat 
  9.  * @author Lyon Yao 
  10.  * 
  11.  */  
  12. public class CatMother implements AnimalMother {  
  13.   
  14.     @Override  
  15.     public Animal giveBirth() {  
  16.         // TODO Auto-generated method stub  
  17.         Animal cat=new Cat();  
  18.         System.out.println("Mother cat gave birth to a kitten.);  
  19.         return cat;  
  20.     }  
  21.   
  22. }  

 

 

 

 

Abstract factory model: the former factory method model is more specific, is that the cat must have a kitten, this is not a problem, is specific, then the abstract factory it produces is not so specific, the object may not have common characteristics. For example, a dairy sheep can produce not only lambs, but also goat milk, but lambs are animals and goat milk is food.

Examples are as follows:

 

Main factory:

 

  1. package com.factory;  
  2.   
  3. import com.bean.Milk;  
  4.   
  5. /** 
  6.  * Milk-producing mothers 
  7.  * Here we inherit Animal Mother to realize the reproduction of small animals. Milk production declares in this interface that constitutes the abstract factory general interface. 
  8.  * @author Lyon Yao 
  9.  * 
  10.  */  
  11. public interface MilkAnimalMother extends AnimalMother {  
  12.     /** 
  13.      * Milk production 
  14.      * @return 
  15.      */  
  16.     public Milk produceMilk();  
  17. }  
 

Milk goat:

 

  1. package com.factory.impl;  
  2.   
  3. import com.bean.Animal;  
  4. import com.bean.Milk;  
  5. import com.bean.Sheep;  
  6. import com.bean.SheepMilk;  
  7. import com.factory.MilkAnimalMother;  
  8.   
  9. /** 
  10.  * Dairy goat 
  11.  * @author Lyon Yao 
  12.  * 
  13.  */  
  14. public class SheepMilkMother implements MilkAnimalMother{  
  15.   
  16.     @Override  
  17.     public Animal giveBirth() {  
  18.         // TODO Auto-generated method stub  
  19.         Animal sheep=new Sheep();  
  20.         System.out.println("The dairy sheep gave birth to a lamb.);  
  21.         return sheep;  
  22.     }  
  23.   
  24.     @Override  
  25.     public Milk produceMilk() {  
  26.         // TODO Auto-generated method stub  
  27.         Milk milk=new SheepMilk();  
  28.         System.out.println("Dairy goats produce goat's milk.);  
  29.         return milk;  
  30.     }  
  31.   
  32. }  

Cows:

 

  1. package com.factory.impl;  
  2.   
  3. import com.bean.Animal;  
  4. import com.bean.Cattle;  
  5. import com.bean.CattleMile;  
  6. import com.bean.Milk;  
  7. import com.factory.MilkAnimalMother;  
  8.   
  9. /** 
  10.  * Cow () 
  11.  * @author Lyon Yao 
  12.  * 
  13.  */  
  14. public class CattleMilkMother implements MilkAnimalMother {  
  15.   
  16.     @Override  
  17.     public Animal giveBirth() {  
  18.         // TODO Auto-generated method stub  
  19.         Cattle cattle=new Cattle();  
  20.         System.out.println("The cow gave birth to a calf.);  
  21.         return cattle;  
  22.     }  
  23.   
  24.     @Override  
  25.     public Milk produceMilk() {  
  26.         // TODO Auto-generated method stub  
  27.         Milk milk=new CattleMile();  
  28.         System.out.println("Dairy cows produce milk.);  
  29.         return milk;  
  30.     }  
  31.   
  32. }  

 

 

Below is test Example:

 

  1. package com.test;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import com.bean.Animal;  
  6. import com.bean.Cat;  
  7. import com.bean.Dog;  
  8. import com.factory.AnimalMother;  
  9. import com.factory.MilkAnimalMother;  
  10. import com.factory.impl.CatMother;  
  11. import com.factory.impl.CattleMilkMother;  
  12. import com.factory.impl.DogMother;  
  13. import com.factory.impl.SheepMilkMother;  
  14. import com.factory.sta.StaticFatory;  
  15.   
  16. /** 
  17.  * Test class 
  18.  *  
  19.  * @author Lyon Yao 
  20.  *  
  21.  */  
  22. public class TestCase {  
  23.     /** 
  24.      * Static Factory Class Testing 
  25.      */  
  26.     @Test  
  27.     public void staticFactoryTest() {  
  28.         Animal ani1=(Animal) StaticFatory.getInstance(Cat.class.getName());  
  29.         System.out.println(ani1.getName());  
  30.         ani1.eat();  
  31.         ani1=(Animal) StaticFatory.getInstance(Dog.class.getName(),"dog");  
  32.         System.out.println(ani1.getName());  
  33.         ani1.eat();  
  34.     }  
  35.     /** 
  36.      * Factory Method Model Testing 
  37.      */  
  38.     @Test  
  39.     public void methodFactoryTest(){  
  40.         AnimalMother mother=new CatMother();  
  41.         mother.giveBirth();  
  42.         mother=new DogMother();  
  43.         mother.giveBirth();  
  44.     }  
  45.     /** 
  46.      * Abstract factory pattern testing 
  47.      */  
  48.     @Test  
  49.     public void abstrFactoryTest(){  
  50.         MilkAnimalMother mother=new SheepMilkMother();  
  51.         mother.giveBirth();  
  52.         mother.produceMilk();  
  53.         mother=new CattleMilkMother();  
  54.         mother.giveBirth();  
  55.         mother.produceMilk();  
  56.     }  
  57. }  

Console output:

 

 

null

I like to eat fish!

dog

I like to eat bone!

Mother cat gave birth to a kitten

The mother of the dog gave birth to a puppy.

The dairy sheep gave birth to a lamb

Dairy goats produce goat milk

The cow gave birth to a calf

Cows produce milk.

Posted by cbrooks on Wed, 29 May 2019 11:46:17 -0700