Abstract Factory Patterns

原创 2005年02月25日 17:07:00

using System;

// "AbstractFactory"

abstract class ContinentFactory
{
    // Methods
    abstract public Herbivore CreateHerbivore();
    abstract public Carnivore CreateCarnivore();
}

// "ConcreteFactory1"

class AfricaFactory : ContinentFactory
{
    // Methods
    override public Herbivore CreateHerbivore()
    {
        return new Wildebeest();
    }
    override public Carnivore CreateCarnivore()
    {
        return new Lion();
    }
}

// "ConcreteFactory2"

class AmericaFactory : ContinentFactory
{
    // Methods
    override public Herbivore CreateHerbivore()
    {
        return new Bison();
    }
    override public Carnivore CreateCarnivore()
    {
        return new Wolf();
    }
}

// "AbstractProductA"

abstract class Herbivore
{
    abstract public void Eat();
}

// "AbstractProductB"

abstract class Carnivore
{
    // Methods
    abstract public void Eat( Herbivore h );
}

// "ProductA1"

class Wildebeest : Herbivore
{
    override public void Eat()
    {
        Console.WriteLine(this + "eats Africa's Glass");
    }
}

// "ProductB1"

class Lion : Carnivore
{
    // Methods
    override public void Eat( Herbivore h )
    {
        // eat wildebeest
        Console.WriteLine( this + " eats " + h );
    }
}

// "ProductA2"

class Bison : Herbivore
{
    override public void Eat()
    {
        Console.WriteLine(this + "eats American's Glass");
    }
}

// "ProductB2"

class Wolf : Carnivore
{
    // Methods
    override public void Eat( Herbivore h )
    {
        // Eat bison
        Console.WriteLine( this + " eats " + h );
    }
}

// "Client"

class AnimalWorld
{
    // Fields
    private Herbivore herbivore;
    private Carnivore carnivore;

    // Constructors
    public AnimalWorld( ContinentFactory factory )
    {
        carnivore = factory.CreateCarnivore();
        herbivore = factory.CreateHerbivore();
    }

    // Methods
    public void RunFoodChain()
    {
        herbivore.Eat();
        carnivore.Eat( herbivore );
    }
}

/// <summary>
///  GameApp test class
/// </summary>

class GameApp
{
    public static void Main( string[] args )
    {
        // Create and run the Africa animal world
        ContinentFactory africa = new AfricaFactory();
        AnimalWorld world = new AnimalWorld( africa );
        world.RunFoodChain();

        // Create and run the America animal world
        ContinentFactory america = new AmericaFactory();
        world = new AnimalWorld( america );
        world.RunFoodChain();

        Console.Read();
    }
}

相关文章推荐

Java Design Patterns Abstract Factory

Abstract Factory OverviewAn abstract factory has sets of methods to make families of various objects...

C++抽象工厂模式(abstract factory patterns)实现

#ifndef SKIN_H #define SKIN_H#include //Button class Button { public: virtual void disp...

Design Pattern - Creational Patterns - Abstract Factory Pattern

2007 Section 2, Chapter 2 Abstract Factory Pattern Concept Since...

Design Patterns - Abstract Factory - Example 1

#include #include using namespace std; // interface AbstractProductA class AbstractProductA { ...
  • wxqee
  • wxqee
  • 2012年06月07日 18:44
  • 424

Design Patterns VI : Simple Factory, Factory Method, Abstract Factory

Design Patterns VI : Simple Factory, Factory Method, Abstract Factory

设计模式(Design Patterns)-可复用面向对象软件的基础 01:简介(Abstract)

一、概述 设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。 使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问...

Abstract Factory

  • 2008年12月25日 18:23
  • 84KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Abstract Factory Patterns
举报原因:
原因补充:

(最多只允许输入30个字)