码迷,mamicode.com
首页 > 编程语言 > 详细

java设计模式-工厂模式

时间:2018-03-05 20:44:46      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:而不是   created   构造   weight   并且   wan   end   delegate   责任   

Real world example
> 铁匠制造武器。 精灵需要精灵武器和兽人需要兽人武器。 根据手头的顾客,召唤正确类型的铁匠。
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.

In plain words
> 它提供了一种将实例化逻辑委托给子类的方法。
> It provides a way to delegate the instantiation logic to child classes.

Wikipedia says
> 在基于类的编程中,工厂方法模式是一种创建模式,它使用工厂方法来处理创建对象的问题,而无需指定将创建的对象的确切类别。
这是通过调用一个工厂方法创建对象来完成的,这个方法要么在接口中指定,要么由子类实现,要么在基类中实现,并且可以被派生类重写,而不是通过调用构造函数。
> In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.



Use the Factory Method pattern when
使用工厂方法模式:
* a class can‘t anticipate the class of objects it must create
* 一个类无法预测它必须创建的对象的类
* a class wants its subclasses to specify the objects it creates
* 一个类想让它的子类指定它创建的对象
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
* 类将责任委派给几个助手子类之一,并且您想本地化哪些助手子类是委托的知识

java设计模式-工厂模式

标签:而不是   created   构造   weight   并且   wan   end   delegate   责任   

原文地址:https://www.cnblogs.com/wsh1230/p/8510799.html

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