Abstract Factory Patterns

xiaoxiao2021-03-06  19

Using system;

// "AbstractFactory"

Abstract Class ContinentFactory {// methods Abstract public herbivore createherbivore (); Abstract public carnivore createcarnivore ();

// "Concretefactory1"

Class AfricaFactory: ContinentFactory {// methods override public herbivore creteherbivore () {return new wildebeest ();} {return new lion ();}}

// "ConcreteFactory2"

Class AmericaFactory: ContinentFactory {//Methods override public herbivore creteherbivore () {Return new bison ();} Override public carnivorecarnivore () {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);}}

///

/// GameApp Test class ///

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 American; 4; Worlda; World.Runfoodchain ();

Console.read ();}}

转载请注明原文地址:https://www.9cbs.com/read-53605.html

New Post(0)