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:
- package com.bean;
- /**
- * Animals
- * @author Lyon Yao
- *
- */
- public abstract class Animal {
- private String name;
- public Animal() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Animal(String name) {
- super();
- this.name = name;
- }
- public abstract void eat();
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
Cats:
- package com.bean;
- /**
- * Feline
- * @author Lyon Yao
- *
- */
- public class Cat extends Animal {
- public Cat() {
- // TODO Auto-generated constructor stub
- }
- public Cat(String name) {
- super(name);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void eat() {
- // TODO Auto-generated method stub
- System.out.println("I like to eat fish!");
- }
- }
Dogs: package com.bean;
- /**
- * Dogs
- * @author Lyon Yao
- *
- */
- public class Dog extends Animal {
- public Dog() {
- // TODO Auto-generated constructor stub
- }
- public Dog(String name) {
- super(name);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void eat() {
- // TODO Auto-generated method stub
- System.out.println("I like to eat bone!");
- }
- }
Static factory class:
- package com.factory.sta;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.InvocationTargetException;
- /**
- * Static factory creates an object. Objects generated by static factory classes generally share common characteristics, inherit a class, or refer to interfaces, etc.
- * No seemingly nonexistent, but it is undeniable that they are all subclasses of Object or Object.
- * @author Lyon Yao
- *
- */
- public class StaticFatory {
- public static Object getInstance(String className){
- Object instance=null;
- try {
- Class cls=Class.forName(className);
- instance= cls.newInstance();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return instance;
- }
- public static Object getInstance(String className,Object ...agrs) {
- Class cls=null;
- try {
- cls = Class.forName(className);
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- return null;
- }
- Constructor[] constructors = cls.getConstructors();
- Object instance=null;
- for(Constructor cons:constructors){
- Class <?>[] clses=cons.getParameterTypes();
- if(clses.length>0){
- boolean isThisConstructor=true;
- for(int i=0;i<clses.length;i++){
- Class c=clses[i];
- if(! c.isInstance(agrs[i]) ){
- isThisConstructor=false;
- }
- }
- if(isThisConstructor){
- try {
- instance=cons.newInstance(agrs);
- break;
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InstantiationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else{
- continue;
- }
- }
- }
- return instance;
- }
- }
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:
- package com.factory;
- import com.bean.Animal;
- /**
- * Mother interface
- * @author Lyon
- *
- */
- public interface AnimalMother {
- /**
- * Reproductive animals
- * @return
- */
- public Animal giveBirth();
- }
Bitches:
- package com.factory.impl;
- import com.bean.Animal;
- import com.bean.Dog;
- import com.factory.AnimalMother;
- /**
- * Mother Dog
- * @author Lyon Yao
- *
- */
- public class DogMother implements AnimalMother {
- @Override
- public Animal giveBirth() {
- // TODO Auto-generated method stub
- Animal dog=new Dog();
- System.out.println("The mother of the dog gave birth to a puppy.);
- return dog;
- }
- }
Female cat:
- package com.factory.impl;
- import com.bean.Animal;
- import com.bean.Cat;
- import com.factory.AnimalMother;
- /**
- * Mother cat
- * @author Lyon Yao
- *
- */
- public class CatMother implements AnimalMother {
- @Override
- public Animal giveBirth() {
- // TODO Auto-generated method stub
- Animal cat=new Cat();
- System.out.println("Mother cat gave birth to a kitten.);
- return cat;
- }
- }
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:
- package com.factory;
- import com.bean.Milk;
- /**
- * Milk-producing mothers
- * 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.
- * @author Lyon Yao
- *
- */
- public interface MilkAnimalMother extends AnimalMother {
- /**
- * Milk production
- * @return
- */
- public Milk produceMilk();
- }
Milk goat:
- package com.factory.impl;
- import com.bean.Animal;
- import com.bean.Milk;
- import com.bean.Sheep;
- import com.bean.SheepMilk;
- import com.factory.MilkAnimalMother;
- /**
- * Dairy goat
- * @author Lyon Yao
- *
- */
- public class SheepMilkMother implements MilkAnimalMother{
- @Override
- public Animal giveBirth() {
- // TODO Auto-generated method stub
- Animal sheep=new Sheep();
- System.out.println("The dairy sheep gave birth to a lamb.);
- return sheep;
- }
- @Override
- public Milk produceMilk() {
- // TODO Auto-generated method stub
- Milk milk=new SheepMilk();
- System.out.println("Dairy goats produce goat's milk.);
- return milk;
- }
- }
Cows:
- package com.factory.impl;
- import com.bean.Animal;
- import com.bean.Cattle;
- import com.bean.CattleMile;
- import com.bean.Milk;
- import com.factory.MilkAnimalMother;
- /**
- * Cow ()
- * @author Lyon Yao
- *
- */
- public class CattleMilkMother implements MilkAnimalMother {
- @Override
- public Animal giveBirth() {
- // TODO Auto-generated method stub
- Cattle cattle=new Cattle();
- System.out.println("The cow gave birth to a calf.);
- return cattle;
- }
- @Override
- public Milk produceMilk() {
- // TODO Auto-generated method stub
- Milk milk=new CattleMile();
- System.out.println("Dairy cows produce milk.);
- return milk;
- }
- }
Below is test Example:
- package com.test;
- import org.junit.Test;
- import com.bean.Animal;
- import com.bean.Cat;
- import com.bean.Dog;
- import com.factory.AnimalMother;
- import com.factory.MilkAnimalMother;
- import com.factory.impl.CatMother;
- import com.factory.impl.CattleMilkMother;
- import com.factory.impl.DogMother;
- import com.factory.impl.SheepMilkMother;
- import com.factory.sta.StaticFatory;
- /**
- * Test class
- *
- * @author Lyon Yao
- *
- */
- public class TestCase {
- /**
- * Static Factory Class Testing
- */
- @Test
- public void staticFactoryTest() {
- Animal ani1=(Animal) StaticFatory.getInstance(Cat.class.getName());
- System.out.println(ani1.getName());
- ani1.eat();
- ani1=(Animal) StaticFatory.getInstance(Dog.class.getName(),"dog");
- System.out.println(ani1.getName());
- ani1.eat();
- }
- /**
- * Factory Method Model Testing
- */
- @Test
- public void methodFactoryTest(){
- AnimalMother mother=new CatMother();
- mother.giveBirth();
- mother=new DogMother();
- mother.giveBirth();
- }
- /**
- * Abstract factory pattern testing
- */
- @Test
- public void abstrFactoryTest(){
- MilkAnimalMother mother=new SheepMilkMother();
- mother.giveBirth();
- mother.produceMilk();
- mother=new CattleMilkMother();
- mother.giveBirth();
- mother.produceMilk();
- }
- }
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.