using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Design_Patterns
{
/// <summary>
/// 抽象工厂模式
/// </summary>
abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA();
public abstract AbstractProductB CreateProductB();
}
class ConcreateFactoryA : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA1();
}
public override AbstractProductB CreateProductB()
{
return new ProductB1();
}
}
class ConcreateFactoryB : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA2();
}
public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}
abstract class AbstractProductA
{
}
abstract class AbstractProductB
{
public abstract void Interact(AbstractProductA a);
}
class ProductA1 : AbstractProductA
{
}
class ProductB1 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name+" interacts with "+a.GetType().Name);
}
}
class ProductA2 : AbstractProductA
{
}
class ProductB2 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
}
}
class Client
{
AbstractProductA AbstractProductA;
AbstractProductB AbstractProductB;
public Client(AbstractFactory AbstractFactory)
{
AbstractProductA = AbstractFactory.CreateProductA();
AbstractProductB = AbstractFactory.CreateProductB();
}
public void Run()
{
AbstractProductB.Interact(AbstractProductA);
}
}
//一系列相互依赖的对象
public interface Floor
{
}
public interface Wall
{
}
public interface Roof
{
}
public class ChinaFloor : Floor
{
}
public class ChinaWall : Wall
{
}
public class ChinaRoof : Roof
{
}
public class USAFloor : Floor
{
}
public class USAWall : Wall
{
}
public class USARoof : Roof
{
}
public class FranceFloor : Floor
{
}
public class FranceWall : Wall
{
}
public class FranceRoof : Roof
{
}
//抽象工厂
public abstract class HouseFactory
{
public abstract Floor CreateFloor();
public abstract Wall CreateWall();
public abstract Roof CreateRoof();
}
public class ChinaHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new ChinaFloor();
}
public override Wall CreateWall()
{
return new ChinaWall();
}
public override Roof CreateRoof()
{
return new ChinaRoof();
}
}
public class USAHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new USAFloor();
}
public override Wall CreateWall()
{
return new USAWall();
}
public override Roof CreateRoof()
{
return new USARoof();
}
}
public class FranceHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new FranceFloor();
}
public override Wall CreateWall()
{
return new FranceWall();
}
public override Roof CreateRoof()
{
return new FranceRoof();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Design_Patterns
{
/// <summary>
/// 抽象工厂模式
/// </summary>
abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA();
public abstract AbstractProductB CreateProductB();
}
class ConcreateFactoryA : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA1();
}
public override AbstractProductB CreateProductB()
{
return new ProductB1();
}
}
class ConcreateFactoryB : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA2();
}
public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}
abstract class AbstractProductA
{
}
abstract class AbstractProductB
{
public abstract void Interact(AbstractProductA a);
}
class ProductA1 : AbstractProductA
{
}
class ProductB1 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name+" interacts with "+a.GetType().Name);
}
}
class ProductA2 : AbstractProductA
{
}
class ProductB2 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
}
}
class Client
{
AbstractProductA AbstractProductA;
AbstractProductB AbstractProductB;
public Client(AbstractFactory AbstractFactory)
{
AbstractProductA = AbstractFactory.CreateProductA();
AbstractProductB = AbstractFactory.CreateProductB();
}
public void Run()
{
AbstractProductB.Interact(AbstractProductA);
}
}
//一系列相互依赖的对象
public interface Floor
{
}
public interface Wall
{
}
public interface Roof
{
}
public class ChinaFloor : Floor
{
}
public class ChinaWall : Wall
{
}
public class ChinaRoof : Roof
{
}
public class USAFloor : Floor
{
}
public class USAWall : Wall
{
}
public class USARoof : Roof
{
}
public class FranceFloor : Floor
{
}
public class FranceWall : Wall
{
}
public class FranceRoof : Roof
{
}
//抽象工厂
public abstract class HouseFactory
{
public abstract Floor CreateFloor();
public abstract Wall CreateWall();
public abstract Roof CreateRoof();
}
public class ChinaHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new ChinaFloor();
}
public override Wall CreateWall()
{
return new ChinaWall();
}
public override Roof CreateRoof()
{
return new ChinaRoof();
}
}
public class USAHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new USAFloor();
}
public override Wall CreateWall()
{
return new USAWall();
}
public override Roof CreateRoof()
{
return new USARoof();
}
}
public class FranceHouseFactory : HouseFactory
{
public override Floor CreateFloor()
{
return new FranceFloor();
}
public override Wall CreateWall()
{
return new FranceWall();
}
public override Roof CreateRoof()
{
return new FranceRoof();
}
}
}