标签:style blog class code java ext
我们一般得到一个实例的方法是由用户new出来的,即用户自行产生的。现在,将某个类的实例交付给一个类的方法提供,那么这就是一种工厂模式。
这样做做有两点好处:(1)在用户得到实例之前我们可以设置权限,只有满足某种条件才能得到相应的实例。(2)我们也可以在工厂类里设置实例产生过程,即是用单例的形式产生还是多例,抑或普通的new出实例。
如下,以单例形式生产实例的工厂模式:
1 package program.factory.model; 2 public interface Moveable { 3 void run(); 4 } 5 6 7 public class Car implements Moveable{ 8 private static Car car=new Car(); 9 private Car(){}; 10 11 public static Car getInstance() { 12 return car; 13 } 14 @Override 15 public void run() { 16 System.out.println("冒着烟喷跑中的car。。。"); 17 } 18 } 19 20 21 public class Main { 22 public static void main(String[]args) { 23 Car car1=Car.getInstance(); 24 Car car2=Car.getInstance(); 25 System.out.println(car1==car2); 26 } 27 }
抽象工厂模式
想象一个场景:一个人拥有一系列产品,交通工具、武器、食品。现在假设他已经拥有了Car、AK47、Apple这样一系列产品,而可能以后,这一系列产品要换成另一系列产品。就像QQ空间换皮肤都是一整套进行更换的,这就要用到抽象工厂设计模式。
1 package program.factory.model; 2 3 public abstract class AbstractFactory { 4 public abstract Vehicle getVehicle(); 5 public abstract Food getFood(); 6 public abstract Weapon getWeapon(); 7 } 8 9 10 public abstract class Vehicle { 11 public abstract void run(); 12 } 13 14 public abstract class Weapon { 15 public abstract void shoot(); 16 } 17 18 public abstract class Food { 19 public abstract void printName(); 20 } 21 22 23 public class Car extends Vehicle{ 24 @Override 25 public void run() { 26 System.out.println("Car run 。。。"); 27 } 28 } 29 30 public class AK47 extends Weapon{ 31 @Override 32 public void shoot() { 33 System.out.println("AK47 fire 。。。"); 34 } 35 } 36 37 public class Apple extends Food{ 38 @Override 39 public void printName() { 40 System.out.println("apple"); 41 } 42 } 43 44 45 public class DefaultFactory extends AbstractFactory{ 46 /* 47 * 父类引用指向子类对象 48 */ 49 private static Vehicle vehicle=new Car(); 50 private static Food food=new Apple(); 51 private static Weapon weapon=new AK47(); 52 @Override 53 public Food getFood() { 54 return food; 55 } 56 57 @Override 58 public Vehicle getVehicle() { 59 return vehicle; 60 } 61 62 @Override 63 public Weapon getWeapon() { 64 return weapon; 65 } 66 }
现在将默认的Car、AK47、Apple换成魔法世界当中Broom、MagicStick、Mushroom。
1 public class Broom extends Vehicle{ 2 @Override 3 public void run() { 4 System.out.println("broom fly 。。。"); 5 } 6 } 7 8 public class MagicStick extends Weapon{ 9 @Override 10 public void shoot() { 11 System.out.println("magicStick shoot ..."); 12 } 13 } 14 15 public class Mushroom extends Food{ 16 @Override 17 public void printName() { 18 System.out.println("Mushroom"); 19 } 20 } 21 22 public class MagicFactory extends AbstractFactory{ 23 private static Vehicle vehicle=new Broom(); 24 private static Food food=new Mushroom(); 25 private static Weapon weapon=new MagicStick(); 26 @Override 27 public Food getFood() { 28 return food; 29 } 30 31 @Override 32 public Vehicle getVehicle() { 33 return vehicle; 34 } 35 36 @Override 37 public Weapon getWeapon() { 38 return weapon; 39 } 40 }
标签:style blog class code java ext
原文地址:http://www.cnblogs.com/feijishuo/p/3705457.html