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.


  • aly1989
  • aly1989
  • 2016年11月06日 19:19
  • 634

设计模式之---单例模式(Singleton Design Pattern)

场景 对象只要利用自己的属性完成了自己的任务.那该对象就是承担了责任。 除了维持了自身的一致性,该对象无需承担其他任何责任。 如果该对象还承担着其他责任,而其他对象又依赖于该特定对象所承担的贵任...
  • u010878994
  • u010878994
  • 2016年04月26日 01:19
  • 557

Ant-design 快速上手

1、在开始之前,先下载 Node.js v4.x 或以上。 2、安装后在win+r输入npm install antd-init -g 安装antd 3、使用命令行进行初始化。 $ mkdir an...
  • sinat_38270658
  • sinat_38270658
  • 2017年04月11日 14:28
  • 3163


  • xupeng874395012
  • xupeng874395012
  • 2017年01月20日 16:26
  • 3016

《API Design for C++》读书笔记(一)

背景:  常见的模块化开发、代码复用、组件化、动态链接库(DLL)、 软件框架、分布式计算以及面向服务的架构(SOA),都隐含了对髙超的API设计技能的需求。    目标:  健壮而优雅、稳定而耐用、...
  • j_s_3721
  • j_s_3721
  • 2017年04月14日 18:40
  • 339

Android源码大放送之material design类型

本文转载自: 鉴于大家对源码的渴望,就算自己辛苦一点也要满足大家的需求,查看了几百个源码...
  • cike110120
  • cike110120
  • 2015年06月20日 09:33
  • 11816

Material Design学习之 EditText (功能强大,优于系统自带,感谢“扔物线”)

转载请注明出处:王亟亟的大牛之路继续之前的Material Design历程,今天是EditText,素材来源于
  • ddwhan0123
  • ddwhan0123
  • 2016年01月29日 10:39
  • 4920

Axure 搭建小项目原型图

Axure RP是一个专业的快速原型设计工具。链接不提供。 Ant Design Library v2.0.1 蚂蚁金服体验技术部出品,
  • wangyingtong
  • wangyingtong
  • 2016年10月26日 16:49
  • 3741

打造极致Material Design动画风格Button

曾经有两篇关于Material Design风格的按钮实现。在第一章中只是简单的实现了动画的波纹效果,而在第二篇中实现可以自动移动到中心位置的动画;虽然两者都可用,但是在我的使用中却发现了一定的问题,...
  • qiujuer
  • qiujuer
  • 2015年01月07日 09:15
  • 8923


问题描述:我的项目中主页有个右下脚有个悬浮按钮FloatingActionButton,悬浮按钮有个自定义的behavior,效果是下拉隐藏,上拉显示。本来项目用的design包版本是compile ...
  • IWantToHitRen
  • IWantToHitRen
  • 2017年03月22日 20:23
  • 1362
您举报文章:Reading Notes: Chapter 1. Policy-Based Class Design(Modern C++ Design By Andrei Alexandrescu)