Java设计模式 - 工厂模式
工厂模式使用场景:
工厂模式分为简单工厂 工厂 抽象工厂模式. 当用户需要一个类的子类实例,且不希望与该类的子类行成耦合或者不知道该类有哪些子类可用时,可采用工厂模式; 当用户需要系统提供多个对象,且希望和创建对象的类解耦时,可采用抽象工厂模式.
源码下载地址:
https://github.com/godlikecheng/Factory_model/
工厂模式的实现方式:
1.简单工厂
定义小汽车接口
package factory_model; /** * 简单工厂 - 定义小汽车接口:Icar.java * @author 张宜成 */ public interface ICar { // 由于工厂模式仅关系对象的创建,为说明方便,无需定义方法 }
定义高中低档具体的小汽车
package factory_model; /** * 简单工厂 - 定义高中低档的小汽车 * 高档小汽车:UpCar.java * @author 张宜成 */ public class UpCar implements ICar { }
package factory_model; /** * 简单工厂 - 定义高中低档的小汽车 * 中档小汽车:MidCar.java * @author 张宜成 */ public class MidCar implements ICar { }
package factory_model; /** * 简单工厂 - 定义高中低档的小汽车 * 低档小汽车:UpCar.java * @author 张宜成 */ public class DnCar implements ICar { }
简单工厂
package factory_model; /** * 简单工厂 * @author 张宜成 */ public class CarSimpleFactory { public static final String UPTYPE = "uptype"; public static final String MIDTYPE = "midtype"; public static final String DNTYPE = "dntype"; public static ICar create(String mark) { ICar obj = null; if(mark.equals(UPTYPE)) { // 如果是高档类型 obj = new UpCar(); // 则创建高档车对象 }else if (mark.equals(MIDTYPE)) { obj = new MidCar(); }else if (mark.equals(DNTYPE)) { obj = new DnCar(); } return obj; // 返回选择的对象 } }
测试程序:
package factory_model;
/**
* 测试类
* @author 张宜成
*/
public class CarTest {
public static void main(String[] args) {
// 从工厂中创建对象
ICar obj = CarSimpleFactory.create("UPTYPE");
}
}
2.工厂
定义小汽车接口
package factory_model_02; /** * 工厂 - 定义小汽车接口:Icar.java * @author 张宜成 */ public interface ICar { // 由于工厂模式仅关系对象的创建,为说明方便,无需定义方法 }
定义高中低档具体的小汽车
package factory_model_02; /** * 工厂 - 定义高中低档的小汽车 * 高档小汽车:UpCar.java * @author 张宜成 */ public class UpCar implements ICar { }
package factory_model_02; /** * 工厂 - 定义高中低档的小汽车 * 中档小汽车:MidCar.java * @author 张宜成 */ public class MidCar implements ICar { }
package factory_model_02; /** * 工厂 - 定义高中低档的小汽车 * 低档小汽车:UpCar.java * @author 张宜成 */ public class DnCar implements ICar { }
定义抽象工厂
package factory_model_02; /** * 定义抽象工厂 * */ public abstract class AbstractFactory { // 定义抽象方法 public abstract ICar create(); }
定义抽象工厂实现类
package factory_model_02; /** * 定义高档小汽车工厂 * @author 张宜成 */ public class UpFactory extends AbstractFactory{ public ICar create() { return new UpCar(); // 高档工厂生成高档小汽车对象 } }
package factory_model_02; /** * 定义中档小气车工厂 * @author 张宜成 */ public class MidFactory extends AbstractFactory { public ICar create() { return new MidCar(); // 中档工厂生成中档小汽车对象 } }
package factory_model_02; /** * 定义抵挡小汽车工厂 * @author 张宜成 */ public class DnFactory extends AbstractFactory { public ICar create() { return new DnCar(); // 低档工厂生成低档小汽车对象 } }
测试类:
package factory_model_02;
/**
* 测试类
* @author 张宜成
*/
public class CarTest {
public static void main(String[] args) {
AbstractFactory obj = new UpFactory(); // 多态创建高档工厂
ICar car = obj.create(); // 获得高档工厂中的小汽车对象
System.out.println(car);
}
}
3.抽象工厂
定义小汽车接口
package factory_model_03; /** * 抽象工厂 - 定义小汽车接口:Icar.java * @author 张宜成 */ public interface ICar { // 由于工厂模式仅关系对象的创建,为说明方便,无需定义方法 }
定义高中低档具体的小汽车
package factory_model_03; /** * 抽象工厂 - 定义高中低档的小汽车 * 高档小汽车:UpCar.java * @author 张宜成 */ public class UpCar implements ICar { }
package factory_model_03; /** * 抽象工厂 - 定义高中低档的小汽车 * 中档小汽车:MidCar.java * @author 张宜成 */ public class MidCar implements ICar { }
package factory_model_03; /** * 抽象工厂 - 定义高中低档的小汽车 * 低档小汽车:UpCar.java * @author 张宜成 */ public class DnCar implements ICar { }
定义高中低档具体的公共汽车
package factory_model_03; /** * 抽象工厂 - 高档公共汽车类 * @author 张宜成 */ public class UpBus implements IBus{ }
package factory_model_03; /** * 抽象工厂 - 中档公共汽车类 * @author 张宜成 */ public class MidBus implements IBus { }
package factory_model_03; /** * 抽象工厂 - 低档公共汽车类 * @author 张宜成 */ public class DnBus implements IBus { }
定义抽象工厂
package factory_model_03; /** * 定义抽象类工厂 * @author 张宜成 */ public abstract class AbstractFactory { public abstract ICar create(); // 产生小汽车对象 public abstract IBus create2(); // 产生公共汽车类 }
定义抽象工厂实现类
package factory_model_03; /** * 定义高档工厂类 * @author 张宜成 * */ public class UpFactory extends AbstractFactory { public ICar create() { return new UpCar(); // 高档工厂生产高档小汽车对象 } public IBus create2() { return new UpBus(); // 高档工厂生成高档公共汽车对象 } }
package factory_model_03; /** * 定义中档工厂 * @author 张宜成 */ public class MidFactory extends AbstractFactory { public ICar create() { return new MidCar(); } public IBus create2() { return new MidBus(); } }
package factory_model_03; /** * 定义低档工厂 * @author 张宜成 */ public class DnFactory extends AbstractFactory { public ICar create() { return new DnCar(); } public IBus create2() { return new DnBus(); } }
上一篇: java设计模式之 单例模式
下一篇: Java设计模式之 原型模式