码迷,mamicode.com
首页 > 其他好文 > 详细

工厂方法模式

时间:2016-01-23 15:34:28      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

 

/// <summary>
/// FPC抽象类
/// </summary>
public abstract class FPC
{
    /// <summary>
    /// FPC生产抽象方法
    /// </summary>
    public abstract void Produce();
}
/// <summary>
/// 生产GTL类
/// </summary>
public class GTL : FPC
{
    public override void Produce()
    {
        Console.WriteLine("GTL");
    }
}
/// <summary>
/// 生产GBL类
/// </summary>
public class GBL : FPC
{
    public override void Produce()
    {
        Console.WriteLine("GBL");
    }
}
/// <summary>
/// 生产GTS类
/// </summary>
public class GTS : FPC
{
    public override void Produce()
    {
        Console.WriteLine("GTS");
    }
}
/// <summary>
/// FPC抽象工厂类
/// </summary>
public abstract class CreateFPC
{
    /// <summary>
    /// FPC工厂方法
    /// </summary>
    /// <returns></returns>
    public abstract FPC ProduceFPC();
}
/// <summary>
/// 生产GTL工厂类
/// </summary>
public class CreateGTL : CreateFPC
{
    //负责生产GTL
    public override FPC ProduceFPC()
    {
        return new GTL();
    }
}
/// <summary>
/// 生产GBL工厂类
/// </summary>
public class CreateGBL : CreateFPC
{
    /// <summary>
    /// 负责生产GBL
    /// </summary>
    /// <returns></returns>
    public override FPC ProduceFPC()
    {
        return new GBL();
    }
}
/// <summary>
/// 生产GTS工厂类
/// </summary>
public class CreateGTS : CreateFPC
{
    /// <summary>
    /// 负责生产GTS
    /// </summary>
    /// <returns></returns>
    public override FPC ProduceFPC()
    {
        return new GTS();
    }
}

调用:

CreateFPC a1a = new CreateGTL();
FPC fpca1a = a1a.ProduceFPC();
fpca1a.Produce();

 

工厂方法模式

标签:

原文地址:http://www.cnblogs.com/genesis/p/5153361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!