当前位置:网站首页>GoF23—工厂模式
GoF23—工厂模式
2022-06-26 06:03:00 【酷小亚】
什么是工厂模式:
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
工厂模式的核心本质:
- 实例化对象不适用new,用工厂方法代替
- 将选择实现类,创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。
三种模式:
简单工厂模式(Simple Factory)
- 用来生产统一等级结构中的
任意产品(对于增加新的产品,需要修改原有的代码,代码量虽少,维护性较差!) - 虽然某种程度上不符合设计原则,但实际上
使用最多!
工厂方法模式(Factory Method)
- 用来生产同一等级结构中的
固定产品(支持增加任意产品,代码量虽大,但维护性较好!) 横向扩展,不影响操作!
抽象工厂模式(Abstract Factory)
- 围绕一个
超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。 - 不可以增加产品,可以
增加产品族!
简单工厂模式 : (又叫静态工厂模式)

package factory.simple;
//接口
public interface Car {
void name();
}
package factory.simple;
public class DaZhong implements Car{
@Override
public void name() {
System.out.println("大众");
}
}
package factory.simple;
public class Tesla implements Car{
@Override
public void name() {
System.out.println("特斯拉");
}
}
package factory.simple;
public class WuLing implements Car{
@Override
public void name() {
System.out.println("五菱宏光!");
}
}
package factory.simple;
/**静态工厂模式(简单工厂模式) * 缺点:增加一个新的产品,如果你不修改代码,做不到! * 没有满足开闭原则 */
public class CarFactory {
//方法一
// public static Car getCar(String car){
// if (car.equals("五菱")){
// return new WuLing();
// }else if(car.equals("Tesla")){
// return new Tesla();
// }else{
// return null;
// }
// }
//方法二
public static Car getWuLing(){
return new WuLing();
}
public static Car getTesla(){
return new Tesla();
}
public static Car getDaZhong(){
return new DaZhong();
}
}
package factory.simple;
//接口的所有实现类!
public class Consumer {
public static void main(String[] args) {
//方法一
// Car car = CarFactory.getCar("五菱");
// car.name();
// CarFactory.getCar("Tesla").name();
//方法二
CarFactory.getTesla().name();
CarFactory.getWuLing().name();
CarFactory.getDaZhong().name();
}
}

工厂方法模式:

package factory.method;
public interface Car {
void name();
}
package factory.method;
//工厂方法模式
public interface CarFactory {
Car getCar();
}
package factory.method;
public class WuLing implements Car {
@Override
public void name() {
System.out.println("五菱宏光!");
}
}
package factory.method;
public class WuLingFactory implements CarFactory{
@Override
public Car getCar() {
return new WuLing();
}
}
package factory.method;
public class Tesla implements Car {
@Override
public void name() {
System.out.println("特斯拉");
}
}
package factory.method;
public class TeslaFactory implements CarFactory{
@Override
public Car getCar() {
return new Tesla();
}
}
package factory.method;
public class MoBai implements Car{
@Override
public void name() {
System.out.println("摩拜单车");
}
}
package factory.method;
public class MoBaiFactory implements CarFactory{
@Override
public Car getCar() {
return new MoBai();
}
}
package factory.method;
//接口的所有实现类!
public class Consumer {
public static void main(String[] args) {
new WuLingFactory().getCar().name();
new TeslaFactory().getCar().name();
new MoBaiFactory().getCar().name();
}
}

抽象工厂模式:
下篇文章(点击查看):GoF23—抽象工厂模式
边栏推荐
- Selective search for object recognition paper notes [image object segmentation]
- 消息队列-功能、性能、运维对比
- Multi thread synchronous downloading of network pictures
- The difference between overload method and override method
- 实时数仓方案如何选型和构建
- [spark] how to implement spark SQL field blood relationship
- Evolution history of qunar Bi platform construction
- Adapter mode
- The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
- Implementation of third-party wechat authorized login for applet
猜你喜欢

The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level

数据可视化实战:实验报告

How to design a good technical scheme

Logstash——Logstash向Email发送告警邮件

University Information Management System

冒泡排序(Bubble Sort)

家庭记账程序(第一版)

Introduction to canal deployment, principle and use

Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction

Getting started with Python
随机推荐
Kolla ansible deploy openstack Yoga version
Mongodb——使用Mongodb对字段中字符串内容进行截取,并进行分组统计
E-commerce seeks growth breakthrough with the help of small program technology
volatile应用场景
Prototype mode, Baa Baa
Record how to modify the control across threads
Yamaha robot splits visual strings
去哪儿网BI平台建设演进史
On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back
Spark source code analysis (I): RDD collection data - partition data allocation
How to design a good technical scheme
Household accounting procedures (First Edition)
Handwritten background management framework template (I)
卷妹带你学jdbc---2天冲刺Day2
Factory method pattern, abstract factory pattern
技术能力的思考和总结
ES6的搭配环境
Easy to understand from the IDE, and then talk about the applet IDE
MySQL-06
【Spark】Spark SQL 字段血缘如何实现