当前位置:网站首页>抽象工厂模式与工厂方法模式代码结构的区别
抽象工厂模式与工厂方法模式代码结构的区别
2022-08-05 19:13:00 【辰三】
两种模式有点分不清,在相同的场景下分别使用两种模式编写一下代码来让大家伙具体看看,到底是差在哪里。
首先 先说结论,举个例子:
工厂方法模式相当于是生产线的小作坊,比如有四个小作坊,一个产A手机、一个产A电脑,一个产B手机、一个产B电脑;四个小作坊各干各的,你需要电脑就去找A电脑生产线或者B电脑生产线。
抽象工厂模式相当于来了2个厂长,把个小作坊给收购了,A厂子里面有手机和电脑两条生产线,B厂子里也有两条对应的生产线,那我需要电脑就只需要找这个厂长给我生产就可以了。
下面这个场景两种模式分别写一下:
首先写一下实体接口类:
两个厂长(工厂方法模式不具备)以及四个产品线
#region 产品接口
public interface IComputer
{
void Open();
void Close();
}
public interface IPhone
{
void Call();
}
//工厂方法模式不具备这个抽象类
public abstract class Product
{
protected IComputer Computer {
get; set; }
protected IPhone Phone {
get; set; }
public abstract IComputer CreateComputer();
public abstract IPhone CreatePhone();
}
#endregion
#region 具体实现类
//工厂方法模式不具备这个A厂子类
public class AProduct : Product
{
public override IComputer CreateComputer()
{
if (Computer==null)
{
Computer = new AComputer();
}
return Computer;
}
public override IPhone CreatePhone()
{
if (Phone==null)
{
Phone = new APhone();
}
return Phone;
}
}
//工厂方法模式不具备这个B厂子类
public class BProduct : Product
{
public override IComputer CreateComputer()
{
throw new NotImplementedException();
}
public override IPhone CreatePhone()
{
throw new NotImplementedException();
}
}
//A电脑生产线
public class AComputer : IComputer
{
public void Open()
{
Console.WriteLine("");
}
public void Close()
{
throw new NotImplementedException();
}
}
//A手机生产线
public class APhone : IPhone
{
public void Call()
{
throw new NotImplementedException();
}
}
//B电脑生产线
public class BComputer : IComputer
{
public void Open()
{
throw new NotImplementedException();
}
public void Close()
{
throw new NotImplementedException();
}
}
//B手机生产线
public class BPhone : IPhone
{
public void Call()
{
throw new NotImplementedException();
}
}
在这个基础上在写一下抽象工厂模式的factory:
两个厂子,各自有两条生产线,需要手机的话就直接去找厂长要就可以了
public interface IFactory
{
//create两条产品线
IComputer CreateComputerProduct();
IPhone CreatePhoneProduct();
}
//A厂子,里面有两条生产线
public class AMiFactory : IFactory
{
public IComputer CreateComputerProduct()
{
return new AProduct().CreateComputer();
}
public IPhone CreatePhoneProduct()
{
return new AProduct().CreatePhone();
}
}
//B厂子,里面有两条生产线
public class BFactory : IFactory
{
public IComputer CreateComputerProduct()
{
return new BProduct().CreateComputer();
}
public IPhone CreatePhoneProduct()
{
return new BProduct().CreatePhone();
}
}
然后再写一下工厂方法模式的factory:
手机电脑factory要分别写,下面对应了4个生产线factory,想要手机就需要去找具体的生产线
public interface IComputerFactory
{
IComputer CreateComputer();
}
public interface IPhoneFactory
{
IPhone CreatePhone();
}
public class AComputerFactory : IComputerFactory
{
public IComputer CreateComputer()
{
return new AComputer();
}
}
public class APhoneFactory : IPhoneFactory
{
public IPhone CreatePhone()
{
return new APhone();
}
}
public class BComputerFacotry : IComputerFactory
{
public IComputer CreateComputer()
{
return new BComputer();
}
}
public class BPhoneFactory : IPhoneFactory
{
public IPhone CreatePhone()
{
return new BPhone();
}
}
以上。
边栏推荐
- tiup cluster deploy
- 01 thinkphp6的前期开发准备《ThinkPHP6 入门到电商实战》
- 03 数据库查询、模型查询、多库查询《ThinkPHP6 入门到电商实战》
- Paginator 简单分页
- RHCE 作业四
- RHCE 作业八(Ansible的三个命令模块和部分文件操模块)
- 04 tp6 的查数据 find、select、findOrEmpty、findOrFail、toArray、selectOrFail、value、column《ThinkPHP6 入门到电商实战》
- 怎么跨网络远程控制电脑?
- jd试用h5st参数探索
- 05 tp6 的数据添加 助手函数、 save、insert、strict、replace、insertGetId、insertAll《ThinkPHP6 入门到电商实战》
猜你喜欢

软测人面试 ,HR 会问到哪些问题?学会涨薪3000+

如何推进质量内建?如何设置质量门禁?

rhcsa 第五次作业

2242902-55-0_Desthiobiotin-phenol_脱硫生物素价格

Cookies and Sessions

8 RESTful案例

04 tp6 的查数据 find、select、findOrEmpty、findOrFail、toArray、selectOrFail、value、column《ThinkPHP6 入门到电商实战》

MVC设计思想

CAS:139554-72-6 _Biotin-Mal生物素修饰的怀槐凝集素价格

kettle庖丁解牛第34篇之常用转换组件之Add XML
随机推荐
Win11开机提示音要怎么改?
jd试用h5st参数探索
零基础学黑客,该怎么学?
JDBC data persistence
给echart.js折线图设置滚动条
Qt 中重载信号槽的连接
tiup cluster clean
CAS:1604673-42-8 (Biotin-PEG4-Mal, 生物素PEG-MAL)
Jetpack Compose Effect 的作用
[ACTF2020 Freshman Competition]Upload 1
MatrixDB v4.6.0 发布,查询性能和图形化操作界面全面升级!
#yyds干货盘点#【愚公系列】2022年08月 Go教学课程 002-Go语言环境安装
数据泄露溯源
浅述量子计算
TensorFlow learning record (4): stochastic gradient descent & Keras high-level interface
Develop SQL editors with Monaco Editor
申请证券账户在手机开通安全吗?
【 Jmeter聚合报告 】
The latest interface is about Douyin, get Douyin share password url API
Splashtop:更好的 Zoom、RingCentral 等远程IT支持协作工具替代方案