惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
D
DataBreaches.Net
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
M
MIT News - Artificial intelligence
美团技术团队
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The Cloudflare Blog
有赞技术团队
有赞技术团队
L
LangChain Blog
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
S
SegmentFault 最新的问题
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
InfoQ

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(9) Factory Method 工厂方法模式
Tristan(GuoZhijian) · 2008-03-14 · via 博客园 - Tristan(GuoZhijian)

VS 2008

工厂方法模式提供创建一系列属于同一产品体系的类的创建方法。并且,有别于一般的简单工厂,工厂方法模式创建产品由子类来完成。这样增加新的产品不需要修改原来的代码,只需要相应增加一个新的工厂子类。

1. 模式UML图

2. 应用

    程序中需要使用到不同的交通工具,将交通工具的行为抽象为一个接口,那么这些交通工具就形成了一个有共同行为的产品体系。根据工厂方法模式的思想,为产品的创建相应的建立一套工厂体系。

ITransportation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {

    
public interface ITransportation {

        
void Run();
    }

}

Car.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {

    
public class Car : ITransportation {
        
ITransportation Members
    }

}

Plane.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {

    
public class Plane : ITransportation {
        
ITransportation Members
    }

}

ITransportationFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {
    
public interface ITransportationFactory {

        ITransportation Create();
    }

}

CarFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {

    
public class CarFactory : ITransportationFactory {
        
ITransportationFactory Members
    }

}

PlaneFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.FactoryMethod.BLL {

    
public class PlaneFactory : ITransportationFactory {
        
ITransportationFactory Members
    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.FactoryMethod.BLL;

namespace DesignPattern.FactoryMethod {
    
class Program {
        
static void Main(string[] args) {

            ITransportationFactory carFactory 
= new CarFactory();
            ITransportation car 
= carFactory.Create();
            car.Run();

            ITransportationFactory planeFactory 
= new PlaneFactory();
            ITransportation plane 
= planeFactory.Create();
            plane.Run();
        }

    }

}

Output