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

抽象工厂

时间:2014-12-29 21:15:05      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

用户(程序员)创建的抽象工厂和抽象产品,抽象工厂是实体工厂的抽象类,实体工厂生产与抽象产品对应的实体产品!

代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimalWorld
{
    // 抽象大陆工厂
    abstract class ContinentFactory
    {
        abstract public Herbivore CreateHerbivore();   //抽象创建食草动物的类,返回食草类动物
        abstract public Carnivore CreateCarnivore();   //抽象创建食肉动物的类,返回食肉类动物
    }
    //非洲大陆,有角马,狮子
    class AfricaFactory : ContinentFactory    //非洲动物工厂类
    {
        override public Herbivore CreateHerbivore()    //实现上面的抽象方法,注意返回类型
        {
            return new Wildebeest();    //实例化一个角马,角马是食草动物
        }
        override public Carnivore CreateCarnivore()   //实现上面的抽象方法,注意返回类型
        {
            return new Lion(); //实例化一个狮子,狮子是食肉动物
        }
    }
    // 美洲大陆,有狼,野牛
    class AmericaFactory : ContinentFactory     //同上,美洲动物工厂类
    {
        override public Herbivore CreateHerbivore()
        {
            return new Bison(); //野牛
        }
        override public Carnivore CreateCarnivore()
        {
            return new Wolf();   //狼
        }
    }
    //食草动物"
    abstract class Herbivore    //抽象食草类
    {
    }
    //肉食动物"
    abstract class Carnivore    //抽象食肉类
    {
        //猎食食草动物的方法
        abstract public void Eat(Herbivore h);     //抽象吃方法,传入食草动物为参数
    }
    //角马
    class Wildebeest : Herbivore    //继承食草
    {
    }
    //狮子"
    class Lion : Carnivore
    {
        //重载猎食食草动物的方法
        override public void Eat(Herbivore h)   //重写吃方法
        {
            Console.WriteLine(this + " eats " + h);    //this代表本身
        }
    }
    //野牛
    class Bison : Herbivore
    {
    }
    //狼
   class Wolf : Carnivore
    {
        //重载猎食食草动物的方法
        override public void Eat(Herbivore h)
        {
            Console.WriteLine(this + " eats " + h);
        }
    }
    //动物世界类
    class AnimalWorld
    {
        private Herbivore herbivore;   //变量,返回食草抽象类
        private Carnivore carnivore;
        // 创建两种动物分类
        public AnimalWorld(ContinentFactory factory)
        {
            carnivore = factory.CreateCarnivore();
            herbivore = factory.CreateHerbivore();
        }
        //运行食物链
        public void RunFoodChain()
        {
            //肉食动物猎食食草动物
            carnivore.Eat(herbivore);
        }
    }
    /// <summary>
    /// 抽象工厂模式客户应用测试
    /// </summary>
    class GameApp
    {
        static void Main(string[] args)
        {
            //创造并运行非洲动物世界
            ContinentFactory africa = new AfricaFactory();
            AnimalWorld world = new AnimalWorld(africa);
            world.RunFoodChain();
            //创造并运行美洲动物世界
            ContinentFactory america = new AmericaFactory();
            world = new AnimalWorld(america);
            world.RunFoodChain();
            Console.Read();
        }

    }
}

 

实例二:(增加亚洲食肉虎,亚洲食草兔)代码红色为增加上去了!其它不变

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AnimalWorld
{
    // 抽象大陆工厂
    abstract class ContinentFactory
    {
        abstract public Herbivore CreateHerbivore();   //抽象创建食草动物的类,返回食草类动物
        abstract public Carnivore CreateCarnivore();   //抽象创建食肉动物的类,返回食肉类动物
    }
    //非洲大陆,有角马,狮子
    class AfricaFactory : ContinentFactory    //非洲动物工厂类
    {
        override public Herbivore CreateHerbivore()    //实现上面的抽象方法,注意返回类型
        {
            return new Wildebeest();    //实例化一个角马,角马是食草动物
        }
        override public Carnivore CreateCarnivore()   //实现上面的抽象方法,注意返回类型
        {
            return new Lion(); //实例化一个狮子,狮子是食肉动物
        }
    }
    // 美洲大陆,有狼,野牛
    class AmericaFactory : ContinentFactory     //同上,美洲动物工厂类
    {
        override public Herbivore CreateHerbivore()
        {
            return new Bison(); //野牛
        }
        override public Carnivore CreateCarnivore()
        {
            return new Wolf();   //狼
        }
    }
    //亚洲大陆,有虎,兔子
    class AsiaFactory : ContinentFactory
    {
        public override Herbivore CreateHerbivore()
        {
            return new rabbit();
        }
        public override Carnivore CreateCarnivore()
        {
            return new tiger();
        }
    }
    //食草动物"
    abstract class Herbivore    //抽象食草类
    {
    }
    //肉食动物"
    abstract class Carnivore    //抽象食肉类
    {
        //猎食食草动物的方法
        abstract public void Eat(Herbivore h);     //抽象吃方法,传入食草动物为参数
    }
    //角马
    class Wildebeest : Herbivore    //继承食草
    {
    }
    //狮子"
    class Lion : Carnivore
    {
        //重载猎食食草动物的方法
        override public void Eat(Herbivore h)   //重写吃方法
        {
            Console.WriteLine(this + " eats " + h);    //this代表本身
        }
    }
    //野牛
    class Bison : Herbivore
    {
    }
    //狼
    class Wolf : Carnivore
    {
        //重载猎食食草动物的方法
        override public void Eat(Herbivore h)
        {
            Console.WriteLine(this + " eats " + h);
        }
    }
    //兔子类
    class rabbit:Herbivore
    {
    }
    //老虎
    class tiger : Carnivore
    {
        public override void Eat(Herbivore h)
        {
            Console.WriteLine(this + "eats" + h);
        }
    }
    //动物世界类
    class AnimalWorld
    {
        private Herbivore herbivore;   //变量,返回食草抽象类
        private Carnivore carnivore;
        // 创建两种动物分类
        public AnimalWorld(ContinentFactory factory)
        {
            carnivore = factory.CreateCarnivore();
            herbivore = factory.CreateHerbivore();
        }
        //运行食物链
        public void RunFoodChain()
        {
            //肉食动物猎食食草动物
            carnivore.Eat(herbivore);
        }
    }
    /// <summary>
    /// 抽象工厂模式客户应用测试
    /// </summary>
    class GameApp
    {
        static void Main(string[] args)
        {
            //创造并运行非洲动物世界
            ContinentFactory africa = new AfricaFactory();
            AnimalWorld world = new AnimalWorld(africa);
            world.RunFoodChain();
            //创造并运行美洲动物世界
            ContinentFactory america = new AmericaFactory();
            world = new AnimalWorld(america);
            world.RunFoodChain();

            //创建并运行亚洲动物世界
            ContinentFactory asia = new AsiaFactory();
            world = new AnimalWorld(asia);
            world.RunFoodChain();
            Console.Read();
        }

    }
}

抽象工厂

标签:

原文地址:http://www.cnblogs.com/lxclqy/p/4192375.html

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