📢 专栏推荐:23种设计模式(Java版)系列专栏
2021 年 12 月 25日 百思不得小赵 🔍点击进入博客首页 —— 新时代的农民工 🙊 —— 换一种思维逻辑去看待这个世界 👀
工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式。著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见。因为工厂模式就相当于创建实例对象的new,我们经常要根据类Class生成实例对象,如A a=new A() 工厂模式也是用来创建实例对象的,所以以后new时就要多个心眼,是否可以考虑使用工厂模式,虽然这样做,可能多做一些工作,但会给你系统带来更大的可扩展性和尽量少的修改量---------来源于百度百科。
案例代码:
//定义一个公共接口
public interface Car {
void gotoWork();
}
public class Bus implements Car{
@Override
public void gotoWork() {
// TODO Auto-generated method stub
System.out.println("坐公交上班");
}
}
public class Bike implements Car{
@Override
public void gotoWork() {
// TODO Auto-generated method stub
System.out.println("骑自行车上班");
}
}
//简单工厂类
public class SimpleFactory {
public enum CarType{
Bike,Bus;
}
//通过不同的方式构建不同的实例
public static Car getCar(CarType car) {
Car simpleCar=null;
switch(car) {
case Bike:
simpleCar=new Bike();
break;
case Bus:
simpleCar= new Bus();
break;
default:
simpleCar=new Bike();
}
return simpleCar;
}
}
通过一个需求案例:
看一个披萨的项目:要便于披萨种类的扩展,要便于维护 1.披萨的种类很多(比如 GreekPizz、CheesePizz 等 2.披萨的制作有 prepare,bake, cut, box 3.完成披萨店订购功能 客户在点披萨时,可以点不同口味的披萨,比如 北京的奶酪pizza、 北京的胡椒pizza 或者是伦敦的奶酪pizza、伦敦的胡椒pizza。
案例代码:
public abstract class Pizza {
protected String name;
//准备材料,不同的披萨材料不一样,做成抽象方法
public abstract void prepare();
public void bake() {
System.out.println(name + " baking;");
}
public void cut() {
System.out.println(name + " cutting;");
}
public void box() {
System.out.println(name + " boxing;");
}
public void setName(String name) {
this.name = name;
}
}
public class BJCheesePizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的奶酪披萨");
System.out.println("北京的奶酪披萨准备原材料");
}
}
public class BJGreekPizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的希腊披萨");
System.out.println("北京的希腊披萨准备原材料");
}
}
public class LDCheesePizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦奶酪披萨");
System.out.println("伦敦奶酪披萨准备原材料");
}
}
public class LDGreekPizz extends Pizza
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦希腊披萨");
System.out.println("伦敦希腊披萨准配原材料");
}
}
public abstract class OrderPizza {
//定义一个抽象方法,让各个工厂子类自己实现
abstract Pizza creatPizza(String orderType);
//构造器
public OrderPizza() {
Pizza pizza = null;
// 订购披萨类型
String orderType;
do {
orderType = getType();
pizza=creatPizza(orderType);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} while (true);
}
// 动态的获取用户希望订购的披萨
private String getType() {
try {
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type:");
String str = strin.readLine();
return str;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
public class BJOrderPizza extends OrderPizza{
@Override
Pizza creatPizza(String orderType) {
Pizza pizza=null;
if(orderType.equals("cheese")) {
pizza=new BJCheesePizz();
}else if(orderType.equals("greek")) {
pizza=new BJGreekPizz();
}
return pizza;
}
}
public class LDOrderPizza extends OrderPizza{
@Override
Pizza creatPizza(String orderType) {
Pizza pizza=null;
if(orderType.equals("cheese")) {
pizza=new LDCheesePizz();
}else if(orderType.equals("greek")) {
pizza=new LDGreekPizz();
}
return pizza;
}
}
public class PizzaStore {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建北京口味的披萨
// new BJOrderPizza();
//创建伦敦口味的披萨
new LDOrderPizza();
}
}
⏰需求案例同工厂方法案例,抽象工厂对其进行进一步的优化。
案例代码:
public abstract class Pizza {
protected String name;
//准备材料,不同的披萨材料不一样,做成抽象方法
public abstract void prepare();
public void bake() {
System.out.println(name + " baking;");
}
public void cut() {
System.out.println(name + " cutting;");
}
public void box() {
System.out.println(name + " boxing;");
}
public void setName(String name) {
this.name = name;
}
}
public class BJCheesePizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的奶酪披萨");
System.out.println("北京的奶酪披萨准备原材料");
}
}
public class BJGreekPizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("北京的希腊披萨");
System.out.println("北京的希腊披萨准备原材料");
}
}
public class LDCheesePizz extends Pizza{
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦奶酪披萨");
System.out.println("伦敦奶酪披萨准备原材料");
}
}
public class LDGreekPizz extends Pizza
@Override
public void prepare() {
// TODO Auto-generated method stub
setName("伦敦希腊披萨");
System.out.println("伦敦希腊披萨准配原材料");
}
}
//抽象工厂模式的抽象层
public interface AbsFactory {
//下面工厂子类自己建造
Pizza creatPizza(String orderType);
}
public class BJFactory implements AbsFactory{
public Pizza creatPizza(String orderType) {
System.out.println("使用抽象工厂模式");
// TODO Auto-generated method stub
Pizza pizza=null;
if(orderType.equals("cheese")) {
pizza=new BJCheesePizz();
}else if(orderType.equals("greek")) {
pizza=new BJGreekPizz();
}
return pizza;
}
}
public class LDFactory implements AbsFactory{
public Pizza creatPizza(String orderType) {
System.out.println("使用抽象工厂模式");
// TODO Auto-generated method stub
Pizza pizza=null;
if(orderType.equals("cheese")) {
pizza=new LDCheesePizz();
}else if(orderType.equals("greek")) {
pizza=new LDGreekPizz();
}
return pizza;
}
public class OrderPizza {
AbsFactory absFactory;
public OrderPizza(AbsFactory absFactory) {
setAbsFactory(absFactory);
}
private void setAbsFactory(AbsFactory absFactory) {
Pizza pizza=null;
String orderType="";
this.absFactory = absFactory;
do {
orderType=getType();
//absFactory 可能为北京 或者伦敦
pizza=absFactory.creatPizza(orderType);
if(pizza!=null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else {
System.out.println("订购失败");
break;
}
}while(true);
}
private String getType() {
try {
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("input pizza type:");
String str = strin.readLine();
return str;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
public class PizzaStore {
public static void main(String[] args) {
// TODO Auto-generated method stub
new OrderPizza(new BJFactory());
}
}
JDK 中的Calendar类中,就使用了简单工厂模式
public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
public static Calendar getInstance(TimeZone zone,
Locale aLocale){
return createCalendar(zone, aLocale);
}
private static Calendar createCalendar(TimeZone zone,
Locale aLocale){
CalendarProvider provider =
LocaleProviderAdapter.getAdapter(CalendarProvider.class, aLocale)
.getCalendarProvider();
if (provider != null) {
try {
return provider.getInstance(zone, aLocale);
} catch (IllegalArgumentException iae) {
// fall back to the default instantiation
}
}
Calendar cal = null;
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
case "buddhist":
cal = new BuddhistCalendar(zone, aLocale);
break;
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
case "gregory":
cal = new GregorianCalendar(zone, aLocale);
break;
}
}
}
if (cal == null) {
// If no known calendar type is explicitly specified,
// perform the traditional way to create a Calendar:
// create a BuddhistCalendar for th_TH locale,
// a JapaneseImperialCalendar for ja_JP_JP locale, or
// a GregorianCalendar for any other locales.
// NOTE: The language, country and variant strings are interned.
if (aLocale.getLanguage() == "th" && aLocale.getCountry() == "TH") {
cal = new BuddhistCalendar(zone, aLocale);
} else if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
return cal;
}
}