工厂方法模式的真实世界示例

工厂方法模式的真实世界示例

本文介绍了工厂方法模式的真实世界示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚读工厂方法。我明白,它提供了一种将实例化委托给子类的方法。但是我无法理解现实世界中的可能用途。



任何人都可以给出一个典型的例子,说明如何使用Factory方法模式,以便我可以关联



对于哪种工厂方法模式是最佳解决方案的问题陈述将足以使其清楚。



解决方案

实现工厂设计模式的类作为多个类之间的桥梁。考虑使用多个数据库服务器(如SQL Server和Oracle)的示例。如果您正在开发使用SQL Server数据库作为后端的应用程序,但在将来需要将后端数据库更改为oracle时,如果您没有按照工厂设计模式编写代码,则需要修改所有代码。



在工厂设计模式中,您需要做很少的工作才能实现这一点。一个实施工厂设计模式的课程照顾你,减轻你的负担。从数据库服务器切换不会打扰你。您只需要在配置文件中进行一些小的更改。


I just read Factory Method. I understand that it provides a way to delegate the instantiation to sub-classes. But I couldn't understand the possible uses in a real-world scenario.

Can anyone give one typical example showing how Factory method pattern can be used so that I can relate to what I have read.

A problem statement for which factory method pattern is the best solution would be sufficient to make it clear.

alt text http://www.dofactory.com/Patterns/Diagrams/factory.gif

解决方案

A class implementing factory design pattern works as bridge between multiple classes. Consider an example of using multiple database servers like SQL Server and Oracle. If you are developing an application using SQL Server database as backend, but in future need to change backend database to oracle, you will need to modify all your code, if you haven’t written your code following factory design pattern.

In factory design pattern you need to do very little work to achieve this. A class implementing factory design pattern takes care for you and lessen your burden. Switching from database server won’t bother you at all. You just need to make some small changes in your configuration file.

这篇关于工厂方法模式的真实世界示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:38