简单工厂模式

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

.啥是简单工厂?

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

结构大概如下图:

简单工厂模式_第1张图片

画出场景的类图

解释:

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();
                }
        }
}

 

.运行效果和总结

效果:

简单工厂模式_第2张图片

总结:

简单工厂模式的优缺点: 

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

简单工厂模式_第3张图片

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

 

你可能感兴趣的:(简单工厂模式)