下面我们通过简单的例子来学习工厂模式;
首先我们要谈一谈工厂模式 的作用;
工厂模式(Factory Method):定义一个用于创建对象的接口,让子类决定将哪一个类实例化。从而使一个类的实例化延迟到其子类。
抽象工厂模式(Abstract Factory):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
其次我想说的是 我们的工厂模式 有三种常用的,1:简单工厂模;2:工厂方法模式;3:抽象工厂模式;
1.简单工厂模式 :单个工厂生产,在工厂中进行判断要生产的那一种类型;
2.工厂方法模式:单个工厂生产,但是在这个工厂中有多个要生产产品的方法;
3.抽象工厂模式:多个工厂生产,需要哪种产品就要求哪个工厂进行生产;
下面我们一 生产面包为例;butterbread (奶油面包),chocolatbread(巧克力面包);
(1)简单工厂模式:
先写一个面包接口;public interface Bread {
void prepareflour();//准备面粉
void makebread();//制作面包
void toast();//烤面包
}
奶油面包类:
public interface Bread {
void prepareflour();//准备面粉
void makebread();//制作面包
void toast();//烤面包
}
public class Butter implements Bread {
public void preparebutter(){
System.out.println("准备奶油");
}
public void prepareflour() {
// TODO Auto-generated method stub
System.out.println("准备面粉");
}
public void makebread() {
// TODO Auto-generated method stub
System.out.println("制作奶油面包");
}
public void toast() {
// TODO Auto-generated method stub
System.out.println("烤制奶油面包");
}
}
巧克力面包类:
public class Butter implements Bread {
public void preparebutter(){
System.out.println("准备奶油");
}
public void prepareflour() {
// TODO Auto-generated method stub
System.out.println("准备面粉");
}
public void makebread() {
// TODO Auto-generated method stub
System.out.println("制作奶油面包");
}
public void toast() {
// TODO Auto-generated method stub
System.out.println("烤制奶油面包");
}
}
public class Chocolat implements Bread {
public void prepareChcolat(){
System.out.println("准备巧克力");
}
public void prepareflour() {
// TODO Auto-generated method stub
System.out.println("准备面粉");
}
public void makebread() {
// TODO Auto-generated method stub
System.out.println("制作巧克力面包");
}
public void toast() {
// TODO Auto-generated method stub
System.out.println("烤制面包");
}
}
创建一个简单的工厂类:
public class Chocolat implements Bread {
public void prepareChcolat(){
System.out.println("准备巧克力");
}
public void prepareflour() {
// TODO Auto-generated method stub
System.out.println("准备面粉");
}
public void makebread() {
// TODO Auto-generated method stub
System.out.println("制作巧克力面包");
}
public void toast() {
// TODO Auto-generated method stub
System.out.println("烤制面包");
}
}
public class Factory {
public Bread produce(String type) {
if ("巧克力面包".equals(type)) {
return new Chocolat();
}else if("奶油面包".equals(type)){
return new Butter();
}else {
System.out.println("对不起我们已经卖完了");
return null;
}
}
}
在main方法中调用:
public class Factory {
public Bread produce(String type) {
if ("巧克力面包".equals(type)) {
return new Chocolat();
}else if("奶油面包".equals(type)){
return new Butter();
}else {
System.out.println("对不起我们已经卖完了");
return null;
}
}
}
Factory factory = new Factory();
Bread bread = factory.produce("奶油面包");
bread.makebread();
Factory factory = new Factory();
Bread bread = factory.produce("奶油面包");
bread.makebread();
(2)工厂方法模式:
创建一个工厂:public class Factory2 {
public Bread produceButter(){
return new Butter();
}
public Bread produceChocolat(){
return new Chocolat();
}
}
public class Factory2 {
public Bread produceButter(){
return new Butter();
}
public Bread produceChocolat(){
return new Chocolat();
}
}
在main 方法中调用;
Factory2 factory2 = new Factory2();
Bread chocolat = factory2.produceChocolat();
chocolat.makebread();
Factory2 factory2 = new Factory2();
Bread chocolat = factory2.produceChocolat();
chocolat.makebread();
抽象工厂模式:
public class ButterFactory {
public Bread producebutter() {
return new Butter();
}
}
public class ChocolatFactory {
public Bread produceCholocat() {
return new Chocolat();
}
}
在main方法中调用:
public class ButterFactory {
public Bread producebutter() {
return new Butter();
}
}
public class ChocolatFactory {
public Bread produceCholocat() {
return new Chocolat();
}
}
ChocolatFactory factory3 = new ChocolatFactory();
Bread produceCholocat = factory3.produceCholocat();
produceCholocat.makebread();
好了终于完工了,希望对同行们有帮助:
ChocolatFactory factory3 = new ChocolatFactory();
Bread produceCholocat = factory3.produceCholocat();
produceCholocat.makebread();