当前位置:网站首页>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 !
边栏推荐
- 源码编译pytorch坑
- AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
- 2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
- How to add a map to the legendary server
- Let you understand several common traffic exposure schemes in kubernetes cluster
- 深度学习刷SOTA的一堆trick
- ARFoundation从零开始3-创建ARFoundation项目
- < El table column> place multiple pictures
- Modification of annotation based three-tier project and the way of adding package scanning
- IDEA中使用注解Test
猜你喜欢

Google gtest事件机制

ARFoundation入门教程7-url动态加载图像跟踪库

IDEA中使用注解Test

AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块

Diagram of odoo development tutorial

Pytorch learning notes

Word如何查看文档修改痕迹?Word查看文档修改痕迹的方法

How does WPS take quick screenshots? WPS quick screenshot method

Activity workflow table structure learning

浅谈AspectJ框架
随机推荐
SM整合原来这么简单,步骤清晰(详细)
How does word view document modification traces? How word views document modification traces
Original code, inverse code, complement code
AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
Word如何查看文档修改痕迹?Word查看文档修改痕迹的方法
How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
[config] configure array parameters
Wechat picture identification
AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
【config】配置数组参数
Self join and joint query of MySQL
开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开
Pivot table of odoo development tutorial
Jackson解析JSON详细教程
Northeast University Data Science Foundation (matlab) - Notes
On AspectJ framework
SQL log
How to add traffic statistics codes to the legendary Development Zone website
Spark的算子操作列表
Open source Huizhi creates the future | the openeuler sub forum of 2022 open atom global open source summit was successfully held