简单工厂模式

Posted 奋斗的大橙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单工厂模式相关的知识,希望对你有一定的参考价值。

情景:有一个怪兽,HP100,现在勇士有可以使用武器将其打败,有三种武器,木剑每次打击20血,铁剑每次50血,金刚剑每次100血,如果想要使用简单工厂方式,怎么设计?

.啥是简单工厂?

通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

结构大概如下图:

技术分享

画出场景的类图

技术分享

解释:

1.Sword是一个基类,通过其中有一个字段保存怪物的血量,还有一个虚方法是打击怪物的方法

2.有三个具体的武器的类,分别对应木剑、铁剑、金刚剑,实现了各种对怪物打击的逻辑

3.CreateSwordFactory类,是具体实例化武器的类,通过客户端的调用,可以传入想要创建的武器。

4.Program就是客户端

 

.具体的代码

1.Sword.cs类
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();
                }
        }
}

 

.运行效果和总结

效果:

技术分享

总结:

简单工厂模式的优缺点: 

       点:如下图所示,这时候我们添加一个其他的剑,那么我不需要去修改我红色区域的东西,仅仅修改CreateSwordFactory.cs这个类就行,然后这个类根据客户端给出的具体产生什么剑再去实例化就可以了。不需要了解具体每一个剑是怎么被创建的。

技术分享

缺点:以为过多的依赖于工厂类,简单工厂模式背了“开放封则”,就是背了“系统对扩展开放,修改关闭”的原,因当我新增加一个候必修改工厂,相的工厂就需要重新编译一遍。

 

以上是关于简单工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式学习——简单工厂模式工厂模式抽象工厂模式

PHP面向对象之选择工厂和更新工厂

设计模式-简单工厂工厂方法模式抽象工厂模式详解

设计模式之简单工厂模式

C++工厂模式(简单工厂工厂方法抽象工厂)

C++工厂模式(简单工厂工厂方法抽象工厂)