当前位置:网站首页>C how to realize simple factory mode
C how to realize simple factory mode
2022-07-29 05:17:00 【Yisu cloud】
C# How to implement simple factory mode
This article mainly introduces “C# How to implement simple factory mode ”, In daily operation , I believe a lot of people are C# There are doubts about how to realize the simple factory mode , Xiao Bian consulted all kinds of materials , Sort out simple and easy-to-use operation methods , I hope to answer ”C# How to implement simple factory mode ” Your doubts help ! Next , Please follow Xiaobian to learn !
scene : There is a monster ,HP yes 100, Now warriors can defeat them with weapons , There are three weapons , Every time the wooden sword hits 20 blood , Iron sword every time 50 blood , Vajra sword every time 100 blood , If you want to use the simple factory method , How to design ?
One . What is a simple factory ?
Responsible for creating instances of other classes by specifically defining one class , Instances created usually have a common parent class .
The structure is roughly shown in the figure below :

Draw the class diagram of the scene

explain :
1.Sword Is a base class , Save the monster's blood volume through one of the fields , Another virtual method is to fight monsters
2. There are three specific categories of weapons , Corresponding to wooden sword 、 Iron sword 、 Jin Gangjian , It realizes various logic of attacking monsters
3.CreateSwordFactory class , Is a class that instantiates weapons , Through the call of the client , You can pass in the weapon you want to create .
4.Program Is the client
Two . Specific code
1.Sword.cs class
namespace SimpleFactory{ public class Sword { protected int monsterLife = 100; public virtual void beat() { } }}2.WoodSword.cs
namespace SimpleFactory{ public class WoodSword : Sword { public override void beat() { while (monsterLife > 0) { base.monsterLife -= 20; Console.WriteLine("The Monster is already alive!"); } Console.WriteLine("Excellent!The Monster is dead!"); } }}3.IronSword.cs
namespace SimpleFactory{ public class IronSword:Sword { public override void beat() { while (monsterLife > 0) { base.monsterLife -= 50; Console.WriteLine("The Monster is already alive!"); } Console.WriteLine("Excellent!The Monster is dead!"); } }}4.DiamondSword.cs
namespace SimpleFactory{ public class DiamondSword:Sword { public override void beat() { while (monsterLife > 0) { base.monsterLife -= 100; Console.WriteLine("The Monster is already alive!"); } Console.WriteLine("Excellent!The Monster is dead!"); } }}5.CreateSwordFactory.cs
namespace SimpleFactory{ public class CreateSwordFactory { public static Sword CreateSword(string sword) { Sword s = null; switch (sword) { case "WoodSword": s = new WoodSword(); break; case "IronSword": s = new IronSword(); break; case "DiamondSword": s = new DiamondSword(); break; default: break; } return s; } }}6.Program.cs
namespace SimpleFactory{ class Program { static void Main(string[] args) { Sword s = CreateSwordFactory.CreateSword("WoodSword"); s.beat(); Console.WriteLine("----------------------"); s=CreateSwordFactory.CreateSword("IronSword"); s.beat(); Console.WriteLine("----------------------"); s = CreateSwordFactory.CreateSword("DiamondSword"); s.beat(); } }}3、 ... and . Operation effect and summary
effect :

summary :
Advantages and disadvantages of the simple factory model :
advantage : As shown in the figure below , At this time, we add another sword , Then I don't need to modify things in my red area , Just modify CreateSwordFactory.cs This class will do , Then instantiate this class according to the specific sword given by the client . There is no need to know how each sword is created .

shortcoming : Think too much reliance on factory classes , Simple factory mode is violated “ Open and closed principle ”, It's a violation “ The system is open to expansion , Turn off for changes ” Principles , Because when I add a new sword, I have to modify the factory class , The corresponding factory class needs to be recompiled .
Here we are , About “C# How to implement simple factory mode ” That's the end of my study , I hope we can solve your doubts . The combination of theory and practice can better help you learn , Let's try ! If you want to continue to learn more related knowledge , Please continue to pay attention to Yisu cloud website , Xiaobian will continue to strive to bring you more practical articles !
边栏推荐
- [2022 freshmen learning] key points of the third week
- SQL log
- Original code, inverse code, complement code
- Mapper agent development
- ODOO开发教程之透视表
- Let you understand several common traffic exposure schemes in kubernetes cluster
- ARFoundation从零开始8-Geospatial API(地理空间)开发
- Do you remember the process analysis and function implementation of post notification?
- MySQL sorts the queried result set according to the specified sequence
- 传奇服务端如何添加地图
猜你喜欢

Unity Metaverse(三)、Protobuf & Socket 实现多人在线

7.2-function-overloading

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

虚拟偶像的歌声原来是这样生成的!

那个准时上下班,从不愿意加班加点的人,在我前面升职了...

Mysql多对多关系,分组拼接把多个数据查询到一条数据上

The song of the virtual idol was originally generated in this way!

TCP三次握手四次挥手

Arfoundation starts from scratch 8-geospatial API (geospatial) development

IDEA中使用注解Test
随机推荐
传奇开区网站如何添加流量统计代码
后置通知的流程分析与功能实现有哪些内容你还记得吗?
SM整合原来这么简单,步骤清晰(详细)
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
Learn the first program of database
2021-10-23
Arfoundation starts from scratch 3- create an arfoundation project
Youxuan database failed to start and reported network error
Self join and joint query of MySQL
Wechat picture identification
QT学习:使用JSON/XML等非ts文件实现多语言国际化
电脑无法打开excel表格怎么办?excel打不开的解决方法
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
P5714 [deep foundation 3. Case 7] obesity
JS daily question (11)
2021-10-11
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Young freshmen yearn for more open source | here comes the escape guide from open source to employment!
MySQL sorts the queried result set according to the specified sequence
Functions in MySQL statements