Java设计模式学习笔记,二:工厂模式

时间:2023-03-09 16:41:45
Java设计模式学习笔记,二:工厂模式

  工厂模式,主要实现了创建者和调用者的分离。

  分类:1、简单工厂模式;2、工厂方法模式;3、抽象工厂模式。

  核心:实例化对象时,用工厂方法代替new操作。

  一、简单工厂模式

    也叫静态工厂模式,工厂类中实现静态方法,根据入参,生产不同的产品,工程项目中常用。

    工厂类作为类使用,产品类作为接口使用,具体产品实现接口,用来生产同一等级结构中的任意产品,当新增产品时,需要修改已有的代码。

 package com.corey.factory.simpleFactory;

 /**
* 产品类接口
* @author Corey
*
*/
public interface Computer {
void calcData();
}
 package com.corey.factory.simpleFactory;

 /**
* 具体产品实现接口
* @author Corey
*
*/
public class HighEndComputer implements Computer{ @Override
public void calcData() {
System.out.println("高端电脑计算速度快!");
} }
 package com.corey.factory.simpleFactory;

 /**
* 具体产品实现接口
* @author Corey
*
*/
public class LowEndComputer implements Computer{ @Override
public void calcData() {
System.out.println("低端电脑计算速度慢!");
} }
 package com.corey.factory.simpleFactory;

 /**
* 产品工厂类,生产同一等级结构中的任意产品
* @author Corey
*
*/
public class ComputerFactory { public static Computer CreateComputer(String type){
Computer computer = null; switch(type){
case "HighEnd":
computer = new HighEndComputer();
break;
case "LowEnd":
computer = new LowEndComputer();
break;
default:
break;
}
return computer;
}
}
 package com.corey.factory.simpleFactory;

 /**
* 测试简单工厂模式
* @author Corey
*
*/
public class Test { public static void main(String[] args) {
Computer c1 = ComputerFactory.CreateComputer("HighEnd");
c1.calcData(); Computer c2 = ComputerFactory.CreateComputer("LowEnd");
c2.calcData(); } }

运行结果:
高端电脑计算速度快! 
低端电脑计算速度慢!

  二、工厂方法模式

    工厂类作为接口使用,固定产品工厂类实现接口,用来生产统一等级结构中的固定产品,可扩展任意产品。

 package com.corey.factory.factoryMethed;

 /**
* 产品工厂类作为接口使用
* @author Corey
*
*/
public interface ComputerFactory { Computer CreateComputer();
}
 package com.corey.factory.factoryMethed;

 /**
* 具体产品工厂类实现接口
* @author Corey
*
*/
public class HighEndComputerFactory implements ComputerFactory{ @Override
public Computer CreateComputer() {
return new HighEndComputer();
} }
 package com.corey.factory.factoryMethed;

 /**
* 具体产品工厂类实现接口
* @author Corey
*
*/
public class LowEndComputerFactory implements ComputerFactory{ @Override
public Computer CreateComputer() {
return new LowEndComputer();
} }
 package com.corey.factory.factoryMethed;

 /**
* 测试工厂方法模式
* @author Corey
*
*/
public class Test { public static void main(String[] args) {
Computer c1 = new HighEndComputerFactory().CreateComputer();
c1.calcData(); Computer c2 = new LowEndComputerFactory().CreateComputer();
c2.calcData(); } }

  三、抽象工厂模式

    工厂类作为接口使用,内含生产配件方法接口,产品配件类作为接口使用,具体产品配件类实现接口,具体产品工厂类实现工厂类,用来生产不同产品族的全部产品,无法增加新产品,可扩展产品族。

 package com.corey.factory.abstractFactory;

 /**
* 工厂类作为接口使用,内含组装配件方法接口
* @author Corey
*
*/
public interface ComputerFactory {
MainBoard createMainBoard(); //生产主板配件
HardDisk createHardDisk(); //生产硬盘配件
Memory createMemory(); //生产内存配件 }
 package com.corey.factory.abstractFactory;

 /**
* 产品配件类作为接口使用
*
* @author Corey
*
*/
public interface HardDisk {
void speed(); //硬盘实现方法
}
 package com.corey.factory.abstractFactory;

 /**
* 具体产品配件类实现接口
* @author Corey
*
*/
public class HighEndHardDisk implements HardDisk{ @Override
public void speed() {
System.out.println("高端硬盘速度快!");
} }
 package com.corey.factory.abstractFactory;

 /**
* 具体产品工厂类实现工厂类,用来生产不同产品族的全部产品
* @author Corey
*
*/
public class HighEndComputerFactory implements ComputerFactory { @Override
public MainBoard createMainBoard() {
return new HighEndMainBoard();
} @Override
public HardDisk createHardDisk() {
return new HighEndHardDisk();
} @Override
public Memory createMemory() {
return new HighEndMemory();
} }
 package com.corey.factory.abstractFactory;

 /**
* 测试抽象工厂模式
* @author Corey
*
*/
public class Test { public static void main(String[] args) {
ComputerFactory h = new HighEndComputerFactory();
HardDisk hHardDisk = h.createHardDisk();
MainBoard hMainBoard = h.createMainBoard();
Memory hMemory = h.createMemory();
hHardDisk.speed();
hMainBoard.speed();
hMemory.speed(); ComputerFactory l = new LowEndComputerFactory();
HardDisk lHardDisk = l.createHardDisk();
MainBoard lMainBoard = l.createMainBoard();
Memory lMemory = l.createMemory();
lHardDisk.speed();
lMainBoard.speed();
lMemory.speed();
}
}

运行结果:

高端硬盘速度快!
高端主板速度快!
高端内存速度快!
低端硬盘速度慢!
低端主板速度慢!
低端内存速度慢!

工厂模式基本就这些内容。

另,简单工厂还有另一种实现方式,就是编写不同的静态方法创建不同的产品,而无需传参,但是调用者需要知道更多的方法名。