zoukankan      html  css  js  c++  java
  • java设计模式-工厂模式

    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
    * 类将责任委派给几个助手子类之一,并且您想本地化哪些助手子类是委托的知识


  • 相关阅读:
    前端开发中同步和异步的区别
    SQL STUFF函数 拼接字符串
    Javascript的精华
    .net网站报错:对象的当前状态使该操作无效
    免费 WebOffice使用
    DotNetBar教程
    C#获取周的第一天、最后一天、月第一天和最后一天
    C#判断文字是否为汉字
    C# 将字符串转为ऩ这种的 html实体编码
    SqlServer将没有log文件的数据库文件附加到服务器中
  • 原文地址:https://www.cnblogs.com/wsh1230/p/8510799.html
Copyright © 2011-2022 走看看