csharp;gutter:true;
///
/// Abstract Factory抽像工厂
/// geovindu,Geovin Du eidt
///
public interface IShape
{
void Draw();
}
///
///
///
public class Square : IShape
{
public void Draw() => Console.WriteLine("Basic square");
}
///
///
///
public class Rectangle : IShape
{
public void Draw() => Console.WriteLine("Basic rectangle");
}
///
///
///
public class RoundedSquare : IShape
{
public void Draw() => Console.WriteLine("Rounded square");
}
///
///
///
public class RoundedRectangle : IShape
{
public void Draw() => Console.WriteLine("Rounded rectangle");
}
///
///
///
public enum Shape
{
Square,
Rectangle
}
///
///
///
public abstract class ShapeFactory
{
public abstract IShape Create(Shape shape);
}
///
///
///
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);
}
}
}
///
///
///
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);
}
}
}
///
///
///
public class Demo
{
public static ShapeFactory GetFactory(bool rounded)
{
if (rounded)
return new RoundedShapeFactory();
else
return new BasicShapeFactory();
}</p>
<pre><code>}
</code></pre>
<pre><code>
;gutter:true;
///
/// Abstract Factory抽像工厂
/// geovindu,Geovin Du eidt
///
public interface IAnimalFactory
{
IDog GetDog();
ITiger GetTiger();
}
///
/// Abstract Product-1
///
public interface ITiger
{
void AboutMe();
}
///
/// Abstract Product-2
///
public interface IDog
{
void AboutMe();
}
///
/// Concrete product-A1(WildTiger)
///
class WildTiger : ITiger
{
public void AboutMe()
{
Console.WriteLine("涂氏 tiger says: I prefer hunting in jungles.Halum.");
}
}
///
/// Concrete product-B1(WildDog)
///
class WildDog : IDog
{
public void AboutMe()
{
Console.WriteLine("涂氏 dog says: I prefer to roam freely in jungles.Bow-Wow.");
}
}
///
/// Concrete product-A2(PetTiger)
///
class PetTiger : ITiger
{
///
///
///
public void AboutMe()
{
Console.WriteLine("李氏 tiger says: Halum.I play in an animal circus.");
}
}
///
/// Concrete product-B2(PetDog)
///
class PetDog : IDog
{
public void AboutMe()
{
Console.WriteLine("李氏 dog says: Bow-Wow.I prefer to stay at home.");
}
}
///
/// Concrete Factory 1-Wild Animal Factory
///
public class WildAnimalFactory : IAnimalFactory
{
public ITiger GetTiger()
{
return new WildTiger();
}
public IDog GetDog()
{
return new WildDog();
}
}
///
/// Concrete Factory 2-Pet Animal Factory
///
public class PetAnimalFactory : IAnimalFactory
{
public IDog GetDog()
{
return new PetDog();
}
public ITiger GetTiger()
{
return new PetTiger();
}
}
///
/// Factory provider
///
class FactoryProvider
{
///
///
///
///
///
///
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.");
}
}
}
调用:
csharp;gutter:true;
//Abstract Factory抽像工厂
Console.WriteLine("<strong><em>Abstract Factory Pattern Demo.</em></strong>\n");</p>
<pre><code> //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();
</code></pre>
<pre><code>
输出:
;gutter:true;
***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.***
Original: https://www.cnblogs.com/geovindu/p/16754088.html
Author: ®Geovin Du Dream Park™
Title: CSharp: Abstract Factory in donet core 3
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/619997/
转载文章受原作者版权保护。转载请注明原作者出处!