`
leonzhx
  • 浏览: 797345 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 21: Use function objects to represent strategies

阅读更多

1.  A primary use of function pointers is to implement the Strategy pattern.

 

2.  Some languages support function pointers, delegates, lambda expressions, or similar facilities that allow programs to store and transmit the ability to invoke a particular function.

 

3.  It is possible to define an object whose methods perform operations on other objects, passed explicitly to the methods. An instance of a class that exports exactly one such method is effectively a pointer to that method. Such instances are known as function objects. (i.e. instance of Comparator)

 

4.  A typical concrete strategy class is stateless: it has no fields, hence all instances of the class are functionally equivalent. Thus it should be a singleton to save on unnecessary object creation costs.

 

5.  Because the strategy interface serves as a type for all of its concrete strategy instances, a concrete strategy class needn’t be made public to export a concrete strategy. Instead, a “host class” can export a public static field (or static factory method) whose type is the strategy interface, and the concrete strategy class can be a private nested class of the host. The String class uses this pattern to export a case-independent string comparator via its CASE_INSENSITIVE_ORDER field.

 

分享到:
评论

相关推荐

    Introduction to Deep Learning: From Logical Calculus to Artificial Intelligence

    This textbook presents a concise, accessible and engaging first introduction to deep learning, offering a wide range of connectionist models which represent the current state-of-the-art. The text ...

    C++ Primer 源程序

    // first check that item1 and item2 represent the same book if (item1.same_isbn(item2)) { std::cout << item1 + item2 << std::endl; return 0; // indicate success } else { std::cerr ...

    Python库 | Represent-1.2.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:Represent-1.2.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    NELL-995 知识图谱 数据集

    entity2vec.bern/relation2vec.bern: transE embeddings to represent out RL states, can be trained using TransX implementations by thunlp tasks/: each task is a particular reasoning relation tasks/${...

    C & C++ Win32 API Programming

    - **Handles**: Represent pointers to objects such as windows or device contexts. - **POINT and RECT**: Structures used for positioning and sizing windows and controls. - **LRESULT and WPARAM**: Used ...

    C Programming

    - **More Than One Dimension**: Explanation of multidimensional arrays and how they can be used to represent tables or matrices. #### Switching This part discusses the `switch` statement, which is ...

    思科网络技术培训之9:IP路由

    The path determination function enables a router to evaluate the available paths to a destination and to establish the best path. Routing services use network topology information when evaluating ...

    3D Object Detection with Latent Support Surfaces

    use latent support surfaces to better represent the 3D appearance of large objects, and provide contextual cues to improve the detection of small objects. We evaluate our model with 19 object ...

    Oracle Database 10g PL-SQL Programming

    - **Function Creation**: Writing and using functions to return values. - **Package Creation**: Creating packages to encapsulate related procedures and functions. - **Overloading**: Allowing multiple ...

    represent:Open North Represent API Node.js 模块

    represent . boundarySets ( "federal-electoral-districts" , function ( error , data ) { } ) ; 边界 响应示例: : represent . boundaries ( "toronto-wards" , function ( error , data ) { } ) ; 从纬度和...

    A MATLAB class to represent the tree data structure..zip

    这个"A MATLAB class to represent the tree data structure"的项目旨在为MATLAB环境提供一个类来实现树的数据结构。下面将详细介绍树数据结构的基本概念、MATLAB中的类实现以及如何使用此类进行操作。 **树数据...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    They are commonly used to represent a group of options or states. ### 7. Debugging Applications with Visual Studio 2008 - **Stepping Through Code**: Debugging involves running your application step-...

    3D Pose Estimation and 3D Model Retrieval for Objects in the Wild

    accurately represent the geometry of objects in RGB im- ages. For this purpose, we render depth images from 3D models under our predicted pose and match learned im- age descriptors of RGB images ...

    Introduction to iUMLite 2.20

    3. **Architecture Domains**: Represent globally applied design and coding strategies (e.g., Software Architecture). 4. **Implementation Domains**: Represent pre-existing software components (e.g., C, ...

    Manning.Git.in.Practice.2014.9.pdf

    - **Exporting to SVN**: Use `git svn dcommit` to export commits from Git to SVN. This command translates Git commits into SVN format and sends them to the SVN repository. - **Importing from SVN**: Use...

    A 3D Modeller-Erick Dransch.zip

    Firstly, they must have a data structure to represent the object that's being designed: this is the computer's understanding of the 3-dimensional world that the user is building. Secondly, the CAD ...

    Clojure.for.Finance.1785289284

    Then, the concept of side-effecting and its various approaches are introduced, along with the strategy of how to use data as the interface to other systems. Finally, you will discover how to build ...

Global site tag (gtag.js) - Google Analytics