当前位置:网站首页>简单工厂模式
简单工厂模式
2022-08-09 06:22:00 【考拉盖饭】
简单工厂模式
角色由简单工厂(创造者),抽象产品(所有具体产品的父类,可以是接口或者抽象类),具体产品(继承或者实现具体产品)三个组成
具体实现
public class Test {
// 相当于客户端
public static void main(String[] args) {
// 首先创建工厂,再通过工厂来创建相应的类
Factory factory = new Factory();
factory.getCar("wulin").name();
factory.getCar("dazong").name();
}
// 抽象产品
interface Car {
void name();
}
// 具体产品
static class Dazong implements Car {
@Override
public void name() {
System.out.println("this is dazong");
}
}
// 具体产品
static class Wulin implements Car {
@Override
public void name() {
System.out.println("this is wulin");
}
}
// 简单工厂
static class Factory {
// 通过调用工厂里面的获取实体方法创建具体产品,返回具体产品的抽象产品
public Car getCar(String car){
switch (car){
case "wulin":
return new Wulin();
case "dazong":
return new Dazong();
}
return null;
}
}
}
边栏推荐
- MYSQLg advanced ------ batch insert millions of data
- 【R语言】对文件进行归一化整理到各文件类型文件夹
- DevNet: Deviation Aware Networkfor Lane Detection
- [HNOI2002]营业额统计
- 语句加锁分析
- 【Wwise】ArgumentException: The specified path is not of a legal form (empty). About the path reading error in WwiseGlobal
- 聚酰胺-胺(PAMAM)树形聚合物-硫化铋复合纳米粒子|硫化铋修饰Gd‑DTPA‑OA配体|科研实验用
- Invalid argument(s) appears when redis runs lua script
- Bismuth sulfide nanorods with CT imaging function | Bismuth sulfide-zinc protoporphyrin composites (PAMAM/Bi2S3 composite nanoparticles)
- [R language] interaction test data
猜你喜欢
契约测试(上):什么是契约测试
MYSQLg高级------批量插入百万级数据量
普罗米修斯原理及节点发布
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
JMeter test - JMeter 】 【 upload multiple images/batch CSV file upload pictures interface parametric method
【Word】Add subscripts to the text of Word at the same time
输入框最前面添加放大镜&&background-image和background-color冲突问题
excel表格如何不需鼠标往下拖动而自动往下填
Magnetic Core-Shell Fe3O4 Particles Supported Gold Nanostars | Magnetic Fe3O4-POSS-COOH | Superparamagnetic Fe3O4-Polydopamine Core-Shell Nanoparticles
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
随机推荐
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
思维方法 解决问题的能力
【Word】Add subscripts to the text of Word at the same time
harbor企业级镜像仓库搭建
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
Go lang1.18入门精炼教程——第一章:环境搭建
sqlserver导入数据类型问题
zip压缩包密码解密
The 24th day of the special assault version of the sword offer
[GO]、数组与切片
阿里巴巴官方技术号
Qt learning (3) - Qt module
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
golang xml 处理动态属性
Unity backgammon game design and simple AI implementation (1)
How to automatically fill down an excel table without dragging the mouse down
qt send mail program
普罗米修斯原理及节点发布
Used to import the data type