Design Pattern Framework 4.0 C# 之一 Abstract
2010-12-05 22:18
393 查看
using System;
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Abstract.NETOptimized
{
/// <summary>
/// MainApp startup class for .NET optimized
/// Abstract Factory Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
// Create and run the African animal world
var africa = new AnimalWorld<Africa>();
africa.RunFoodChain();
// Create and run the American animal world
var america = new AnimalWorld<America>();
america.RunFoodChain();
// Wait for user input
Console.ReadKey();
}
}
/// <summary>
/// The 'AbstractFactory' interface.
/// </summary>
interface IContinentFactory
{
IHerbivore CreateHerbivore();
ICarnivore CreateCarnivore();
}
/// <summary>
/// The 'ConcreteFactory1' class.
/// </summary>
class Africa : IContinentFactory
{
public IHerbivore CreateHerbivore()
{
return new Wildebeest();
}
public ICarnivore CreateCarnivore()
{
return new Lion();
}
}
/// <summary>
/// The 'ConcreteFactory2' class.
/// </summary>
class America : IContinentFactory
{
public IHerbivore CreateHerbivore()
{
return new Bison();
}
public ICarnivore CreateCarnivore()
{
return new Wolf();
}
}
/// <summary>
/// The 'AbstractProductA' interface
/// </summary>
interface IHerbivore
{
}
/// <summary>
/// The 'AbstractProductB' interface
/// </summary>
interface ICarnivore
{
void Eat(IHerbivore h);
}
/// <summary>
/// The 'ProductA1' class
/// </summary>
class Wildebeest : IHerbivore
{
}
/// <summary>
/// The 'ProductB1' class
/// </summary>
class Lion : ICarnivore
{
public void Eat(IHerbivore h)
{
// Eat Wildebeest
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
/// <summary>
/// The 'ProductA2' class
/// </summary>
class Bison : IHerbivore
{
}
/// <summary>
/// The 'ProductB2' class
/// </summary>
class Wolf : ICarnivore
{
public void Eat(IHerbivore h)
{
// Eat Bison
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
/// <summary>
/// The 'Client' interface
/// </summary>
interface IAnimalWorld
{
void RunFoodChain();
}
/// <summary>
/// The 'Client' class
/// </summary>
class AnimalWorld<T> : IAnimalWorld where T : IContinentFactory, new()
{
private IHerbivore _herbivore;
private ICarnivore _carnivore;
private T _factory;
/// <summary>
/// Contructor of Animalworld
/// </summary>
public AnimalWorld()
{
// Create new continent factory
_factory = new T();
// Factory creates carnivores and herbivores
_carnivore = _factory.CreateCarnivore();
_herbivore = _factory.CreateHerbivore();
}
/// <summary>
/// Runs the foodchain, that is, carnivores are eating herbivores.
/// </summary>
public void RunFoodChain()
{
_carnivore.Eat(_herbivore);
}
}
}
using System.Collections.Generic;
namespace DoFactory.GangOfFour.Abstract.NETOptimized
{
/// <summary>
/// MainApp startup class for .NET optimized
/// Abstract Factory Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
public static void Main()
{
// Create and run the African animal world
var africa = new AnimalWorld<Africa>();
africa.RunFoodChain();
// Create and run the American animal world
var america = new AnimalWorld<America>();
america.RunFoodChain();
// Wait for user input
Console.ReadKey();
}
}
/// <summary>
/// The 'AbstractFactory' interface.
/// </summary>
interface IContinentFactory
{
IHerbivore CreateHerbivore();
ICarnivore CreateCarnivore();
}
/// <summary>
/// The 'ConcreteFactory1' class.
/// </summary>
class Africa : IContinentFactory
{
public IHerbivore CreateHerbivore()
{
return new Wildebeest();
}
public ICarnivore CreateCarnivore()
{
return new Lion();
}
}
/// <summary>
/// The 'ConcreteFactory2' class.
/// </summary>
class America : IContinentFactory
{
public IHerbivore CreateHerbivore()
{
return new Bison();
}
public ICarnivore CreateCarnivore()
{
return new Wolf();
}
}
/// <summary>
/// The 'AbstractProductA' interface
/// </summary>
interface IHerbivore
{
}
/// <summary>
/// The 'AbstractProductB' interface
/// </summary>
interface ICarnivore
{
void Eat(IHerbivore h);
}
/// <summary>
/// The 'ProductA1' class
/// </summary>
class Wildebeest : IHerbivore
{
}
/// <summary>
/// The 'ProductB1' class
/// </summary>
class Lion : ICarnivore
{
public void Eat(IHerbivore h)
{
// Eat Wildebeest
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
/// <summary>
/// The 'ProductA2' class
/// </summary>
class Bison : IHerbivore
{
}
/// <summary>
/// The 'ProductB2' class
/// </summary>
class Wolf : ICarnivore
{
public void Eat(IHerbivore h)
{
// Eat Bison
Console.WriteLine(this.GetType().Name +
" eats " + h.GetType().Name);
}
}
/// <summary>
/// The 'Client' interface
/// </summary>
interface IAnimalWorld
{
void RunFoodChain();
}
/// <summary>
/// The 'Client' class
/// </summary>
class AnimalWorld<T> : IAnimalWorld where T : IContinentFactory, new()
{
private IHerbivore _herbivore;
private ICarnivore _carnivore;
private T _factory;
/// <summary>
/// Contructor of Animalworld
/// </summary>
public AnimalWorld()
{
// Create new continent factory
_factory = new T();
// Factory creates carnivores and herbivores
_carnivore = _factory.CreateCarnivore();
_herbivore = _factory.CreateHerbivore();
}
/// <summary>
/// Runs the foodchain, that is, carnivores are eating herbivores.
/// </summary>
public void RunFoodChain()
{
_carnivore.Eat(_herbivore);
}
}
}
相关文章推荐
- Design Pattern Framework 4.0 C# 之二 Adapter
- Design Pattern Framework 4.0 C# 设计模式 ef4,sliverlight4 N层项目
- Design Pattern Framework 4.0 C# 之三 Bridge
- Design Pattern Framework 4.0 C# 之四 Builder
- "Design Pattern FrameworkTM 3.5" c#版的.
- "Design Pattern FrameworkTM 3.5" c#版的.
- 抽象工厂的初次尝试 ((15c++ Homework) polymorphic and OOP design pattern Abstract Factory)
- 《设计模式--基于C#的工程化实现及扩展》 Security Design Pattern 系列 4 角色模式(Role Pattern)
- Observer Design Pattern in C#!
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
- Design Pattern Abstract Factory
- C#设计模式(6)-Abstract Factory Pattern
- [创建型模式系列]Abstract factory of Design Pattern模式设计之抽象工厂模式
- 抽象工厂设计模式(Abstract Factory Design Pattern)简单随笔
- C#设计模式(6)-Abstract Factory Pattern
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
- C#设计模式(6)-Abstract Factory Pattern
- .NET Design Pattern Framework 4.5 +QQ:386184911
- C#设计模式(6)-Abstract Factory Pattern
- C#设计模式(6)-Abstract Factory Pattern