學習工廠模式的時候就曾思考過這么寫的好處,再手動敲了代碼后發現自己更糊塗了,后來搜索例子和各種文案才有點概念,特此記錄一下個人的理解
工廠模式的好處:
1.減少了重復代碼
2.讓創建對象於使用方法分離,代碼解耦
3.利於后期的維護,事實上你創建的工廠的作用就是給你提供你需要的對象,不用在代碼中 一個個new Class,后期如果需要換對象不用在調用的地方去改,修改工廠類中實例化對象的方法就可以,
這些都是個人理解,有什么不對留言指正,下面是實現工廠模式的三種方法
一,簡單工廠模式
1.創建一個接口或者抽象方法
1 /** 2 * 被創建者的公共接口 3 * @author lql29 4 * 5 */ 6 public interface Sender { 7 //公有方法 8 public void Send(); 9 }
2.創建兩個實現類
1 public class MailSend implements Sender{ 2 3 @Override 4 public void Send() { 5 // TODO Auto-generated method stub 6 //實現接口方法 7 System.out.println("MailSender"); 8 } 9 10 }
public class SmsSend implements Sender{ @Override public void Send() { // TODO Auto-generated method stub //實現接口方法 System.out.println("SmsSend"); } }
3.創建一個工廠類
1 /** 2 * 工廠類 3 * @author lql29 4 * 5 */ 6 public class SenderFactory { 7 public Sender produce(String Type){ 8 if("mail".equals(Type)){ 9 return new MailSend(); 10 }else if("sms".equals(Type)){ 11 return new SmsSend(); 12 }else{ 13 System.out.println("請輸入正確的類型!"); 14 return null; 15 } 16 } 17 }
最后實例化工廠類調用工廠類的produce方法,參數是想要返回被創建者對象的類型
二,工廠方法模式
1.創建被創建者接口
1 /** 2 * 被創建者接口 3 * @author lql29 4 * 5 */ 6 public interface PrudactInterface { 7 //定義公共方法 8 public void production(); 9 }
2.創建工廠接口
1 /** 2 * 定義工廠類接口 3 * @author lql29 4 * 5 */ 6 public interface FactoryInterface { 7 //創建對象方法 8 public PrudactInterface found(); 9 }
3.實現被創建者接口
1 /** 2 * 產品實體類 3 * @author lql29 4 * 5 */ 6 public class PrudactPojo implements PrudactInterface{ 7 8 @Override 9 public void production() { 10 // TODO Auto-generated method stub 11 //定義生成什么產品 12 System.out.println("生產芒果飲料。。。。。"); 13 } 14 15 }
1 /** 2 * 定義產品實體類 3 * @author lql29 4 * 5 */ 6 public class PrudactPojo_2 implements PrudactInterface{ 7 8 @Override 9 public void production() { 10 // TODO Auto-generated method stub 11 System.out.println("生成蘋果飲料。。。。。。。"); 12 } 13 14 }
4.實現工廠接口
1 /** 2 * 定義PrudactPojo工廠實體類 3 * @author lql29 4 * 5 */ 6 public class FactoryPojo implements FactoryInterface{ 7 8 @Override 9 public PrudactInterface found() { 10 // TODO Auto-generated method stub 11 //實現創建對象方法, 12 PrudactInterface prudactPojo = new PrudactPojo(); 13 return prudactPojo; 14 } 15 16 }
1 public class FactoryPojo_2 implements FactoryInterface { 2 3 @Override 4 public PrudactInterface found() { 5 // TODO Auto-generated method stub 6 PrudactInterface prudactPojo_2 = new PrudactPojo_2(); 7 return prudactPojo_2; 8 } 9 10 }
這兩種的方法的實現都相對較簡單,簡單工廠模式就是一個大工廠,里面根據分支條件來判斷你需要的各種對象,這種並不靈活,所有的創建對象的方法都在一個工廠類里面,工廠方法就是將工廠細分,一個工廠只生產創建一個對象,后期對於代碼更好的維護,
三,抽象工廠模式
1.創建接口
1 /** 2 *公共接口 3 * 4 */ 5 public interface Shape { 6 void draw(); 7 }
2.實現接口
1 /** 2 *接口實體類 3 */ 4 public class Rectangle implements Shape { 5 6 @Override 7 public void draw() { 8 System.out.println("Inside Rectangle::draw() method."); 9 } 10 }
1 /** 2 *接口實體類 3 * 4 */ 5 public class Square implements Shape { 6 7 @Override 8 public void draw() { 9 System.out.println("Inside Square::draw() method."); 10 } 11 }
3.創建擴展接口
1 /** 2 *擴展接口,顏色 3 */ 4 public interface Color { 5 void fill(); 6 }
4.實現擴展接口
1 public class Red implements Color { 2 3 @Override 4 public void fill() { 5 System.out.println("Inside Red::fill() method."); 6 } 7 }
1 public class Green implements Color { 2 3 @Override 4 public void fill() { 5 System.out.println("Inside Green::fill() method."); 6 } 7 }
5.創建工廠抽象類
1 /** 2 *抽象工廠方法 3 */ 4 public abstract class AbstractFactory { 5 public abstract Color getColor(String color); 6 public abstract Shape getShape(String shape) ; 7 }
6.實現工廠抽象類
1 /** 2 *實現工廠類,制定特定功能的工廠 3 * 4 */ 5 public class ShapeFactory extends AbstractFactory { 6 7 @Override 8 public Shape getShape(String shapeType){ 9 if(shapeType == null){ 10 return null; 11 } 12 if(shapeType.equalsIgnoreCase("CIRCLE")){ 13 return new Circle(); 14 } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ 15 return new Rectangle(); 16 } else if(shapeType.equalsIgnoreCase("SQUARE")){ 17 return new Square(); 18 } 19 return null; 20 } 21 22 @Override 23 public Color getColor(String color) { 24 return null; 25 } 26 }
1 public class ColorFactory extends AbstractFactory { 2 3 @Override 4 public Shape getShape(String shapeType){ 5 return null; 6 } 7 8 @Override 9 public Color getColor(String color) { 10 if(color == null){ 11 return null; 12 } 13 if(color.equalsIgnoreCase("RED")){ 14 return new Red(); 15 } else if(color.equalsIgnoreCase("GREEN")){ 16 return new Green(); 17 } else if(color.equalsIgnoreCase("BLUE")){ 18 return new Blue(); 19 } 20 return null; 21 } 22 }
7.創建一個工廠創造器/生成器類
1 public class FactoryProducer { 2 public static AbstractFactory getFactory(String choice){ 3 if(choice.equalsIgnoreCase("SHAPE")){ 4 return new ShapeFactory(); 5 } else if(choice.equalsIgnoreCase("COLOR")){ 6 return new ColorFactory(); 7 } 8 return null; 9 } 10 }
最后通過FactoryProducer對象調用getFactory方法,參數被創建者的類型
總結:工廠模式的使用就是將創建對象與對象的使用解耦,在各個地方用對應的工廠類創造所需要的對象,最后如有修改對工廠統一修改
