java中工廠模式在java ee中常常遇見,是一種常見的模式,其可分爲三種:靜態工廠模式、工廠方法模式、抽象工廠模式。一下作一簡單講述和例子。java
靜態工廠模式:顧名思義就知道他是用靜態方法實現的,其建立的對象具備必定的特性,譬如說是某類的派生或者某接口的實現。其比較簡單。例子以下ide
Animal類:測試
package com.bean; /** * 動物類 * @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; } }
貓類:this
package com.bean; /** * 貓類 * @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!"); } }
狗類:package com.bean;spa
/** * 狗類 * @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!"); } }
靜態工廠類:orm
package com.factory.sta; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; /** * 靜態工廠建立一個對象 靜態工廠類產生的對象通常都有共同的特性,繼承某一類,或者引用接口之類,在此 * 沒有看似沒有,但不能否認他們都是Object或者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; } }
工廠方法模式:其主要是對各個類型的東西分類生產,但分類生產的對象仍然是具備某一特性的。譬如說:前面的靜態工廠類是一個綜合造車工廠,無論是汽車仍是火車都能生產,而工廠方法模式就是工廠具體分工,造汽車的工廠只造汽車,造火車的只造火車,無論造汽車仍是火車但造出來的仍是車。對象
具體代碼例子以下:(這裏的例子和上面的例子是一塊兒的,具狗生好多小狗,貓生好多小貓的例子,若是不具體那麼是母動物都能生的)繼承
母動物接口:接口
package com.factory; import com.bean.Animal; /** * 母親接口 * @author Lyon * */ public interface AnimalMother { /** * 生育動物 * @return */ public Animal giveBirth(); }
母狗類:get
package com.factory.impl; import com.bean.Animal; import com.bean.Dog; import com.factory.AnimalMother; /** * 狗母親 生狗 * @author Lyon Yao * */ public class DogMother implements AnimalMother { @Override public Animal giveBirth() { // TODO Auto-generated method stub Animal dog=new Dog(); System.out.println("狗母親生了一隻小狗"); return dog; } }
母貓類:
package com.factory.impl; import com.bean.Animal; import com.bean.Cat; import com.factory.AnimalMother; /** * 貓母親 生貓咪 * @author Lyon Yao * */ public class CatMother implements AnimalMother { @Override public Animal giveBirth() { // TODO Auto-generated method stub Animal cat=new Cat(); System.out.println("貓母親生了一隻小貓眯"); return cat; } }
抽象工廠模式:前面工廠方法模式是比較具體的,是貓確定生的是小貓,這是不會有問題的,是具體的,那麼抽象工廠它所產生的就不是那麼具體,產生的對象多是沒有共同特性的。譬如說 一隻奶羊不單單可以生小羊,同時也能生產羊奶,但小羊是動物,羊奶是食物。
例子以下:
總工廠:
package com.factory; import com.bean.Milk; /** * 能產奶的 動物母親 * 這裏繼承 AnimalMother 實現 生育小動物 產奶在此接口聲明 構成抽象工廠總接口 * @author Lyon Yao * */ public interface MilkAnimalMother extends AnimalMother { /** * 產奶 * @return */ public Milk produceMilk(); }
奶羊:
package com.factory.impl; import com.bean.Animal; import com.bean.Milk; import com.bean.Sheep; import com.bean.SheepMilk; import com.factory.MilkAnimalMother; /** * 奶羊 * @author Lyon Yao * */ public class SheepMilkMother implements MilkAnimalMother{ @Override public Animal giveBirth() { // TODO Auto-generated method stub Animal sheep=new Sheep(); System.out.println("奶羊生了一隻小羊"); return sheep; } @Override public Milk produceMilk() { // TODO Auto-generated method stub Milk milk=new SheepMilk(); System.out.println("奶羊生產了羊奶"); return milk; } }
奶牛:
package com.factory.impl; import com.bean.Animal; import com.bean.Cattle; import com.bean.CattleMile; import com.bean.Milk; import com.factory.MilkAnimalMother; /** * 奶牛() * @author Lyon Yao * */ public class CattleMilkMother implements MilkAnimalMother { @Override public Animal giveBirth() { // TODO Auto-generated method stub Cattle cattle=new Cattle(); System.out.println("奶牛生了一隻小牛"); return cattle; } @Override public Milk produceMilk() { // TODO Auto-generated method stub Milk milk=new CattleMile(); System.out.println("奶牛生產了牛奶"); return milk; } }
下面是測試例子:
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; /** * 測試類 * * @author Lyon Yao * */ public class TestCase { /** * 靜態工廠類 測試 */ @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(); } /** * 工廠方法模式測試 */ @Test public void methodFactoryTest(){ AnimalMother mother=new CatMother(); mother.giveBirth(); mother=new DogMother(); mother.giveBirth(); } /** * 抽象工廠模式測試 */ @Test public void abstrFactoryTest(){ MilkAnimalMother mother=new SheepMilkMother(); mother.giveBirth(); mother.produceMilk(); mother=new CattleMilkMother(); mother.giveBirth(); mother.produceMilk(); } }
控制檯輸出:
null
I like to eat fish!
dog
I like to eat bone!
貓母親生了一隻小貓眯
狗母親生了一隻小狗
奶羊生了一隻小羊
奶羊生產了羊奶
奶牛生了一隻小牛
奶牛生產了牛奶