/*--===------------------------------------------===---
简单工厂模式 (构建式模式)
--===------------------------------------------===---*/
using System;
namespace xumh
{
public abstract class Fruit
{
public abstract string GetName();
}
public class Apple:Fruit
{
public override string GetName()
{
return "Apple";
}
}
public class Pear:Fruit
{
public override string GetName()
{
return "pear";
}
}
public class FruitFactory
{
public static Apple CreateApple()
{
return new Apple();
}
public static Pear CreatePear()
{
return new Pear();
}
}
public class runMyApp
{
static void Main()
{
Fruit[] fruits = new Fruit[2];
fruits[0] = FruitFactory.CreateApple();
fruits[1] = FruitFactory.CreatePear();
foreach(Fruit fruit in fruits)
Console.WriteLine( fruit.GetName() );
}
}
}
简单工厂模式 (构建式模式)
--===------------------------------------------===---*/
using System;
namespace xumh
{
public abstract class Fruit
{
public abstract string GetName();
}
public class Apple:Fruit
{
public override string GetName()
{
return "Apple";
}
}
public class Pear:Fruit
{
public override string GetName()
{
return "pear";
}
}
public class FruitFactory
{
public static Apple CreateApple()
{
return new Apple();
}
public static Pear CreatePear()
{
return new Pear();
}
}
public class runMyApp
{
static void Main()
{
Fruit[] fruits = new Fruit[2];
fruits[0] = FruitFactory.CreateApple();
fruits[1] = FruitFactory.CreatePear();
foreach(Fruit fruit in fruits)
Console.WriteLine( fruit.GetName() );
}
}
}
抽象工厂模式:
/*
★new的问题:
实现依赖,不能应对"具体实例化类型"的变化.
★解决思路:
封装变化的--哪里变化,封装哪里,没有变化,不必封装.
★工厂模式的缘起
1).变化点在"对象创建",因此就封装"对象创建"
2).面向接口编程--以来接口,而非依赖实现.
class RoadFactory
{
public static Road CreateRoad()
{
return new Road();
}
}
//创建一个Road对象
Road road = roadFactory.CreateRoad();
★要点:
1).如果没有应对"多系列对象构建"的需求变化,则没有必要使用Abstract Factory Pattern,使用简单的静态工厂模式搞定.
2)."系列对象"指的是这些对象之间有相互依赖关系.
3).抽象工厂模式应付"新系列"的需求变化,缺点是难以应付"新对象"的需求变动.
4).抽象工厂模式经常和Factory Method模式共同应付"对象创建"的需求变化.
--===------------------------------------------===---
★经验:
所有的对象类采用抽象类; 工厂类的方法采用抽象方法以应付对象各个系列具体类的创建;
在客户部分,将具体的不同风格的工厂类作为参数,既可以产生对应的具体风格的对象.
* */
using System;
namespace AbstractFactory
{
//道路
public abstract class Road
{
}
//房屋
public abstract class Building
{
}
//地道
public abstract class Tunnel
{
}
//丛林
public abstract class Jungle
{
}
/// <summary>
/// 工厂类,抽象方法可以创建多种对象
/// </summary>
public abstract class BuildFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//--===------------------------------------------===---
//现代风格
//道路
public class ModernRoad:Road
{
}
//房屋
public class ModernBuilding:Building
{
}
//地道
public class ModernTunnel:Tunnel
{
}
//丛林
public class ModernJungle:Jungle
{
}
//--===------------------------------------------===---
class ModernBuildFactory:BuildFactory
{
public override Road CreateRoad(){
return new ModernRoad();
}
public override Building CreateBuilding(){
return new ModernBuilding();
}
public override Tunnel CreateTunnel(){
return new ModernTunnel();
}
public override Jungle CreateJungle(){
return new ModernJungle();
}
}
/// <summary>
/// 客户端程序,都不依赖于具体的类,且不必改变
/// </summary>
class GameManager
{
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
BuildFactory buildFactory;
public GameManager(BuildFactory buildFactory)
{
this.buildFactory = buildFactory;
}
public void BuildGameFacilities()
{
road = buildFactory.CreateRoad();
building = buildFactory.CreateBuilding();
tunnel = buildFactory.CreateTunnel();
jungle = buildFactory.CreateJungle();
}
public void Run()
{
//road.Aaa();
//building.Bbb(road);
//tunnel.CCC();
//jungle.Ddd(tunnel);
}
}
class Program
{
static void Main(string[] args)
{
GameManager gameManager =
new GameManager(new ModernBuildFactory());
gameManager.BuildGameFacilities();
gameManager.Run();
}
}
}
★new的问题:
实现依赖,不能应对"具体实例化类型"的变化.
★解决思路:
封装变化的--哪里变化,封装哪里,没有变化,不必封装.
★工厂模式的缘起
1).变化点在"对象创建",因此就封装"对象创建"
2).面向接口编程--以来接口,而非依赖实现.
class RoadFactory
{
public static Road CreateRoad()
{
return new Road();
}
}
//创建一个Road对象
Road road = roadFactory.CreateRoad();
★要点:
1).如果没有应对"多系列对象构建"的需求变化,则没有必要使用Abstract Factory Pattern,使用简单的静态工厂模式搞定.
2)."系列对象"指的是这些对象之间有相互依赖关系.
3).抽象工厂模式应付"新系列"的需求变化,缺点是难以应付"新对象"的需求变动.
4).抽象工厂模式经常和Factory Method模式共同应付"对象创建"的需求变化.
--===------------------------------------------===---
★经验:
所有的对象类采用抽象类; 工厂类的方法采用抽象方法以应付对象各个系列具体类的创建;
在客户部分,将具体的不同风格的工厂类作为参数,既可以产生对应的具体风格的对象.
* */
using System;
namespace AbstractFactory
{
//道路
public abstract class Road
{
}
//房屋
public abstract class Building
{
}
//地道
public abstract class Tunnel
{
}
//丛林
public abstract class Jungle
{
}
/// <summary>
/// 工厂类,抽象方法可以创建多种对象
/// </summary>
public abstract class BuildFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//--===------------------------------------------===---
//现代风格
//道路
public class ModernRoad:Road
{
}
//房屋
public class ModernBuilding:Building
{
}
//地道
public class ModernTunnel:Tunnel
{
}
//丛林
public class ModernJungle:Jungle
{
}
//--===------------------------------------------===---
class ModernBuildFactory:BuildFactory
{
public override Road CreateRoad(){
return new ModernRoad();
}
public override Building CreateBuilding(){
return new ModernBuilding();
}
public override Tunnel CreateTunnel(){
return new ModernTunnel();
}
public override Jungle CreateJungle(){
return new ModernJungle();
}
}
/// <summary>
/// 客户端程序,都不依赖于具体的类,且不必改变
/// </summary>
class GameManager
{
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
BuildFactory buildFactory;
public GameManager(BuildFactory buildFactory)
{
this.buildFactory = buildFactory;
}
public void BuildGameFacilities()
{
road = buildFactory.CreateRoad();
building = buildFactory.CreateBuilding();
tunnel = buildFactory.CreateTunnel();
jungle = buildFactory.CreateJungle();
}
public void Run()
{
//road.Aaa();
//building.Bbb(road);
//tunnel.CCC();
//jungle.Ddd(tunnel);
}
}
class Program
{
static void Main(string[] args)
{
GameManager gameManager =
new GameManager(new ModernBuildFactory());
gameManager.BuildGameFacilities();
gameManager.Run();
}
}
}