Reading Notes: Chapter 1. Policy-Based Class Design(Modern C++ Design By Andrei Alexandrescu)

原创 2011年01月10日 14:32:00

1.13 Summary

Design is choice. Most often, the struggle is not that there is no way to solve a design problem, but that there are too many ways that apparently solve the problem. You must know which collection of solutions solves the problem in a satisfactory manner. The need to choose propagates from the largest architectural levels down to the smallest unit of code. Furthermore, choices can be combined, which confers on design an evil multiplicity.
To fight the multiplicity of design with a reasonably small amount of code, a writer of a design-oriented library needs to develop and use special techniques. These techniques are purposely conceived to support flexible code generation by combining a small number of primitive devices. The library itself provides a number of such devices. Furthermore, the library exposes the specifications from which these devices are built, so the client can build her own. This essentially makes a policy-based design open-ended. These devices are called policies, and the implementations thereof are called policy classes.

The mechanics of policies consist of a combination of templates with multiple inheritance. A class that uses policies—a host class—is a template with many template parameters (often, template template parameters), each parameter being a policy. The host class "indirects" parts of its functionality through its policies and acts as a receptacle that combines several policies in a coherent aggregate.

Classes designed around policies support enriched behavior and graceful degradation of functionality. A policy can provide supplemental functionality that propagates through the host class due to public inheritance. Furthermore, the host class can implement enriched functionality that uses the optional functionality of a policy. If the optional functionality is not present, the host class still compiles successfully, provided the enriched functionality is not used.

The power of policies comes from their ability to mix and match. A policy-based class can accommodate very many behaviors by combining the simpler behaviors that its policies implement. This effectively makes policies a good weapon for fighting against the evil multiplicity of design.

Using policy classes, you can customize not only behavior but also structure. This important feature takes policy-based design beyond the simple type genericity that’s specific to container classes.

Policy-based classes support flexibility when it comes to conversions. If you use policy-by-policy copying, each policy can control which other policies it accepts, or converts to, by providing the appropriate conversion constructors, conversion operators, or both.

In breaking a class into policies, you should follow two important guidelines. One is to localize, name, and isolate design decisions in your class—things that are subject to a trade-off or could be sensibly implemented in other ways. The other guideline is to look for orthogonal policies, that is, policies that
don’t need to interact with each other and that can be changed independently.



第一章:Policy-Based Class Design 读书笔记

Policy Class (Template) 下面的这些Creater叫做“policy class (template)”。它们类似于设计模式里的strategy。它们是语法导向(synta...

学习 Policy based design - 读C++设计新思维-泛型编程与设计模式的应用

现在将今年读过的一些书, 感觉比较有心得的地方做一些分享.  久闻Andrei, Alexandrescu的Modern C++ Design - Generic Programing and De...

用C++ 模板做代码设计的三类方法及代码——基于策略Policy-based design

列出了3个方法,还有很多衍生版本,总结如下: 通过继承,与static 有同样的使用接口 继承:对代码改动最小:using ..., 适用于对代码已经写好,但用于追加功能,修改功能的时候 static...

Policy-based design

One problem which often arises during programming is how to build a base set of functionality which ...

如何用C#实践Policy based design

简介 这篇是我在CodeProject的发文,在这篇文章中我用一个简单的范例示范如何在C#实...

【十问十答】深度对话C++天才Andrei Alexandrescu

导读:国外技术网站ServerSideMagazine对Andrei Alexandrescu进行专访,CSDN软件研发频道对此文进行编译。Andrei Alexandrescu是罗马尼亚人,现担任F...

【十问十答】深度对话C++天才Andrei Alexandrescu

导读:国外技术网站ServerSideMagazine对Andrei Alexandrescu进行专访,CSDN软件研发频道对此文进行编译。Andrei Alexandrescu是罗马尼亚人,现担任F...

深度对话C++天才Andrei Alexandrescu

导读:国外技术网站ServerSideMagazine对Andrei Alexandrescu进行专访,CSDN软件研发频道对此文进行编译。Andrei Alexandrescu是罗马尼亚人,现担任F...