码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2016-03-10 14:40:41      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

情景:有一个怪兽,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这个类就行,然后这个类根据客户端给出的具体产生什么剑再去实例化就可以了。不需要了解具体每一个剑是怎么被创建的。

技术分享

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

 

简单工厂模式

标签:

原文地址:http://www.cnblogs.com/dcz2015/p/5261609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!