CSharp: Abstract Factory in donet core 3
/// <summary>
/// Abstract Factory抽像工厂
/// geovindu,Geovin Du eidt
/// </summary>
public interface IShape
{
void Draw();
}
/// <summary>
///
/// </summary>
public class Square : IShape
{
public void Draw() => Console.WriteLine("Basic square");
}
/// <summary>
///
/// </summary>
public class Rectangle : IShape
{
public void Draw() => Console.WriteLine("Basic rectangle");
}
/// <summary>
///
/// </summary>
public class RoundedSquare : IShape
{
public void Draw() => Console.WriteLine("Rounded square");
}
/// <summary>
///
/// </summary>
public class RoundedRectangle : IShape
{
public void Draw() => Console.WriteLine("Rounded rectangle");
}
/// <summary>
///
/// </summary>
public enum Shape
{
Square,
Rectangle
}
/// <summary>
///
/// </summary>
public abstract class ShapeFactory
{
public abstract IShape Create(Shape shape);
}
/// <summary>
///
/// </summary>
public class BasicShapeFactory : ShapeFactory
{
public override IShape Create(Shape shape)
{
switch (shape)
{
case Shape.Square:
return new Square();
case Shape.Rectangle:
return new Rectangle();
default:
throw new ArgumentOutOfRangeException(
nameof(shape), shape, null);
}
}
}
/// <summary>
///
/// </summary>
public class RoundedShapeFactory : ShapeFactory
{
public override IShape Create(Shape shape)
{
switch (shape)
{
case Shape.Square:
return new RoundedSquare();
case Shape.Rectangle:
return new RoundedRectangle();
default:
throw new ArgumentOutOfRangeException(nameof(shape), shape, null);
}
}
}
/// <summary>
///
/// </summary>
public class Demo
{
public static ShapeFactory GetFactory(bool rounded)
{
if (rounded)
return new RoundedShapeFactory();
else
return new BasicShapeFactory();
}
}
/// <summary>
/// Abstract Factory抽像工厂
/// geovindu,Geovin Du eidt
/// </summary>
public interface IAnimalFactory
{
IDog GetDog();
ITiger GetTiger();
}
/// <summary>
/// Abstract Product-1
/// </summary>
public interface ITiger
{
void AboutMe();
}
/// <summary>
/// Abstract Product-2
/// </summary>
public interface IDog
{
void AboutMe();
}
/// <summary>
/// Concrete product-A1(WildTiger)
/// </summary>
class WildTiger : ITiger
{
public void AboutMe()
{
Console.WriteLine("涂氏 tiger says: I prefer hunting in jungles.Halum.");
}
}
/// <summary>
/// Concrete product-B1(WildDog)
/// </summary>
class WildDog : IDog
{
public void AboutMe()
{
Console.WriteLine("涂氏 dog says: I prefer to roam freely in jungles.Bow-Wow.");
}
}
/// <summary>
/// Concrete product-A2(PetTiger)
/// </summary>
class PetTiger : ITiger
{
/// <summary>
///
/// </summary>
public void AboutMe()
{
Console.WriteLine("李氏 tiger says: Halum.I play in an animal circus.");
}
}
/// <summary>
/// Concrete product-B2(PetDog)
/// </summary>
class PetDog : IDog
{
public void AboutMe()
{
Console.WriteLine("李氏 dog says: Bow-Wow.I prefer to stay at home.");
}
}
/// <summary>
/// Concrete Factory 1-Wild Animal Factory
/// </summary>
public class WildAnimalFactory : IAnimalFactory
{
public ITiger GetTiger()
{
return new WildTiger();
}
public IDog GetDog()
{
return new WildDog();
}
}
/// <summary>
/// Concrete Factory 2-Pet Animal Factory
/// </summary>
public class PetAnimalFactory : IAnimalFactory
{
public IDog GetDog()
{
return new PetDog();
}
public ITiger GetTiger()
{
return new PetTiger();
}
}
/// <summary>
/// Factory provider
/// </summary>
class FactoryProvider
{
/// <summary>
///
/// </summary>
/// <param name="factoryType"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IAnimalFactory GetAnimalFactory(string factoryType)
{
if (factoryType.Contains("涂氏"))
{
// Returning a WildAnimalFactory
return new WildAnimalFactory();
}
else
if (factoryType.Contains("李氏"))
{
// Returning a PetAnimalFactory
return new PetAnimalFactory();
}
else
{
throw new ArgumentException("You need to pass either wild or pet as argument.");
}
}
}
调用:
//Abstract Factory抽像工厂
Console.WriteLine("***Abstract Factory Pattern Demo.***\n");
//Making a wild dog and wild tiger through WildAnimalFactory
IAnimalFactory animalFactory = FactoryProvider.GetAnimalFactory("涂氏");
IDog dog = animalFactory.GetDog();
ITiger tiger = animalFactory.GetTiger();
dog.AboutMe();
tiger.AboutMe();
Console.WriteLine("******************");
//Making a pet dog and pet tiger through PetAnimalFactory now.
animalFactory = FactoryProvider.GetAnimalFactory("李氏");
dog = animalFactory.GetDog();
tiger = animalFactory.GetTiger();
dog.AboutMe();
tiger.AboutMe();
//
var basic = Demo.GetFactory(false);
var basicRectangle = basic.Create(Shape.Rectangle);
basicRectangle.Draw();
var roundedSquare = Demo.GetFactory(true).Create(Shape.Square);
roundedSquare.Draw();
Console.ReadLine();
输出:
***Abstract Factory Pattern Demo.*** 涂氏 dog says: I prefer to roam freely in jungles.Bow-Wow. 涂氏 tiger says: I prefer hunting in jungles.Halum. ****************** 李氏 dog says: Bow-Wow.I prefer to stay at home. 李氏 tiger says: Halum.I play in an animal circus. Basic rectangle Rounded square ***Abstract Factory Pattern Demo.***

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号