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

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed
L
LangChain Blog
Jina AI
Jina AI
爱范儿
爱范儿
C
Check Point Blog
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
月光博客
月光博客
GbyAI
GbyAI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Stack Overflow Blog
Stack Overflow Blog
V
V2EX
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
The GitHub Blog
The GitHub Blog
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题

博客园 - 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