c#设计模式==工厂方法模式
2017-04-25 23:07
295 查看
工厂方法模式,定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法模式是一个类的实例化延迟到了其子类。
工厂方法模式在简单工厂模式的基础上进行了改进,将简单工厂方法中工厂的判断进行了改进,工厂方法模式在增加一个分支的时候不用再对原有的类进行修改了,而是可以直接增加一个子类,对客户端来说只需要知道一个工厂抽象类和一个抽象的志愿者类,于是便可以实例化不同的志愿者对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 工厂方法模式
{
class Program
{
static void Main(string[] args)
{
Factory fact = new LeiFactory();
Volunteer lei = fact.getvol();
lei.cook();
lei.sweep();
lei.wash();
fact = new UniFactory();
lei = fact.getvol();
lei.cook();
lei.sweep();
lei.wash();
}
}
}
abstract class Volunteer
{
public abstract void sweep();
public abstract void cook();
public abstract void wash();
}
class Leifeng : Volunteer
{
public override void sweep()
{
Console.WriteLine("雷锋扫地");
}
public override void cook()
{
Console.WriteLine("雷锋煮饭");
}
public override void wash()
{
Console.WriteLine("雷锋洗衣");
}
}
class Universtity : Volunteer
{
public override void sweep()
{
Console.WriteLine("大学生志愿者扫地");
}
public override void cook()
{
Console.WriteLine("大学生志愿者煮饭");
}
public override void wash()
{
Console.WriteLine("大学生志愿者洗衣");
}
}
abstract class Factory
{
public abstract Volunteer getvol();
}
class LeiFactory : Factory
{
public override Volunteer getvol()
{
return (new Leifeng());
}
}
class UniFactory : Factory
{
public override Volunteer getvol()
{
return (new Universtity());
}
}
工厂方法模式在简单工厂模式的基础上进行了改进,将简单工厂方法中工厂的判断进行了改进,工厂方法模式在增加一个分支的时候不用再对原有的类进行修改了,而是可以直接增加一个子类,对客户端来说只需要知道一个工厂抽象类和一个抽象的志愿者类,于是便可以实例化不同的志愿者对象。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 工厂方法模式
{
class Program
{
static void Main(string[] args)
{
Factory fact = new LeiFactory();
Volunteer lei = fact.getvol();
lei.cook();
lei.sweep();
lei.wash();
fact = new UniFactory();
lei = fact.getvol();
lei.cook();
lei.sweep();
lei.wash();
}
}
}
abstract class Volunteer
{
public abstract void sweep();
public abstract void cook();
public abstract void wash();
}
class Leifeng : Volunteer
{
public override void sweep()
{
Console.WriteLine("雷锋扫地");
}
public override void cook()
{
Console.WriteLine("雷锋煮饭");
}
public override void wash()
{
Console.WriteLine("雷锋洗衣");
}
}
class Universtity : Volunteer
{
public override void sweep()
{
Console.WriteLine("大学生志愿者扫地");
}
public override void cook()
{
Console.WriteLine("大学生志愿者煮饭");
}
public override void wash()
{
Console.WriteLine("大学生志愿者洗衣");
}
}
abstract class Factory
{
public abstract Volunteer getvol();
}
class LeiFactory : Factory
{
public override Volunteer getvol()
{
return (new Leifeng());
}
}
class UniFactory : Factory
{
public override Volunteer getvol()
{
return (new Universtity());
}
}
相关文章推荐
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
- C#设计模式之工厂模式(Factory)--02 工厂方法模式(Factory Method)
- C#设计模式——工厂方法模式
- C#设计模式——工厂方法模式(Factory Method Pattern)
- 【C# 设计模式-工厂方法模式】
- C#设计模式之工厂方法模式(游泳成绩排名例子)
- C#设计模式(3)——工厂方法模式
- C#设计模式01-工厂方法模式(附源码)
- C#设计模式(3)——工厂方法模式
- C#设计模式之工厂方法模式(Factory Method Pattern)
- [导入]C#面向对象设计模式纵横谈(5):Factory Method 工厂方法模式(创建型模式).zip(10.09 MB)
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
- C#设计模式——工厂方法模式(Factory Method Pattern)
- 设计模式C#描述——工厂方法模式 mutou23 [原作]
- 乐在其中设计模式(C#) - 工厂方法模式(Factory Method Pattern)
- 《模式——工程化实现及扩展》(设计模式C# 版)《工厂方法模式 Factory Method》——“自我检验" 参考答案
- c# 设计模式:工厂方法模式
- C#设计模式(3)——工厂方法模式 推荐
- C# 设计模式系列教程-工厂方法模式
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解