16现代软件设计技术 (2)

上传人:无*** 文档编号:244034126 上传时间:2024-10-02 格式:PPT 页数:36 大小:264.50KB
返回 下载 相关 举报
16现代软件设计技术 (2)_第1页
第1页 / 共36页
16现代软件设计技术 (2)_第2页
第2页 / 共36页
16现代软件设计技术 (2)_第3页
第3页 / 共36页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,现代软件设计技术,潘爱民,2003-12-26,http:/ Programming,构造框架(,framework),的技术,构造可重用类库的技术,Generic programming,思想:,“,通过参数化技术达到重用的目的(,reuse through parameterization)”,,使得组件更容易被定制。实现静态配置代码,从而获得很高的效率。,Generic Programming,可以消除类型和算法之间本来不必要的相依性,STL,是成功的,generic programming,典型,generic programming,限制代码产生的方式:用实体类型代替类型参数,使这些预先编制好的代码模型成为真正的代码,无法产生完全新的代码。,相关的,programming,Aspect-Oriented Programming(AOP),An aspect is a modular unit that cross-cuts the structure of other modular units,Aspects exist in both design and implementation.A design aspect is a modular unit of the design that cross-cuts the structure of other parts of the design.A program or code aspect is a modular unit of the program that cross-cuts other modular units of the program.,把问题分解为功能单元(组件)和,aspect,在,AOP,系统中,组件和,aspects(,交织)组合起来得到一个系统的具体实现。交织组合既可以在,compile-time,,也可以在,runtime,AOSD,参考:,http:/,/,generative programming,Generative Programming,建立起一族软件系统的模型,在特定的要求下,利用一些基本的、可重用的组件,通过配置,能够自动根据需要产生一个高度定制和优化的软件系统实例,一些原则,用参数化技术来概括出差异性,对于相依性和交互进行分析和建模,通过静态链接(,compile-time),消除不必要的开销,以及执行与特定领域相关的优化,将问题空间和方案空间分离,通过配置建立两者的映射关系,Separation of concerns:borrowed from AOP,特点,Generative Programming,涉及到软件开发过程的各个阶段,与,Domain Engineering,结合,A Model-Based,Approach:,http:/www.sei.cmu.edu/domain-engineering/domain_engineering.html,Active Libraries,C+Generic Programming,Template,技术,使,C+,成为,two-level language,metaprogram,从科学计算用途-一般性的抽象,即,generic programming,对于编译器,代码产生、优化,在编译时刻,实现静态绑定,partial evaluation,对于库开发人员,Active libraries,,提供一种抽象的功能,并且控制优化过程,许多技术,如,policy(traits,)、,编译时刻计算功能,对于应用开发人员,定制,template class,或者,template function,C+Generic Programming(,续),设计思想,编译时刻程序设计,类似于,logic-programming,即,在,compile-time,让编译器完成一些功能,例如,静态的计算功能,if/,else,loop,switch,对,type,进行一些基本的逻辑操作(编程),保证类型安全,宁可要,compile-time error,,也不要,runtime error,尽可能地泛化 抽象性,跟传统的设计方法结合起来,比如利用,patterns,Template,基础,class template,template,class Array,public:,T,&,operator(size_t,n),if(n=,MAX_ELEMS,)throw Out of bounds!;,return,m_rgn,;,protected:,T,m_rg,MAX_ELEMS,;,;,使用:,Array a1;,Array a2;,Template,基础 模板特化,template specialization,template,class Array,public:,char&,operator(size_t,n),if(n=256)throw Out of bounds!;,return,m_szn,;,bool,operator=(const Array&,rhs,),return,strcmp(m_sz,rhs.m_sz,)=0;,protected:,char m_sz256;,;,Template,基础 部分模板特化,partial template specialization,template,class Array,public:,T&,operator(size_t,n),if(n=MAX_ELEMS)throw Out of bounds!;,return,m_szn,;,bool,operator=(const Array&,rhs,),return,strcmp(m_sz,rhs.m_sz,)=0;,protected:,T,m_szMAX_ELEMS,;,;,*Visual C+6,不支持部分模板特化,Template,基础 函数模板,function template,template,void,Swap(T,&a,T&b,),T temp=a;,a=b;,b=temp;,template,T&,min(T,&a,T&b),return a b?a:b;,模板参数,,compile-time,起作用,函数参数,,runtime,起作用,Template,基础 函数对象泛化,generalized,functors(function,objects),functor,:,重载了,operator(),的,C+,对象,扩展了函数指针的概念,可以增加内部状态,可以被当作对象来传递,具有值语义(,value semantic)。,它可以是,template class,,也可以不是,template,class,Functor,public:,ResultType,operator()();,/other member function,private:,/implementation,;,用法:,Functor,MyFunctor(val1);,int,Result=,MyFunctor,();,Template,技术 代替,runtime,的,if/else,/,Class declarations,template,class,ConditionProcess,;,class,ConditionProcess,public:,static inline void f(),statement1;/true case,;,class,ConditionProcess,public:,static inline void f(),statement2;/false case,;,/Replacement for if/else statement:,ConditionProcess,:f();,if(condition),statement1;,else,statement2;,Compile-time,能够确定,condition,的值,Template,技术 代替,runtime,的,switch,/,Class declarations,template,class,SwitchProcess,public:,static inline void f()default-statement;,;,class,SwitchProcess,public:,static inline void f()statement1;,;,class,SwitchProcess,public:,static inline void f()statement2;,;,/Replacement for,switch(i,)statement,SwitchProcess,:f();,int,i;,switch(i,),case value1:,statement1;,break;,case value2:,statement2;,break;,default:,default-statement;,break;,Template,技术 代替,runtime,的,do,循环,template,class,DoProcess,private:,enum,go=(I-1)!=0;,public:,static inline void f(),statement;,DoProcess,:f();,;,/Specialization provides base case for recursion,class,DoProcess,public:,static inline void f(),;,/Equivalent loop code,DoProcess,:f();,int,i=N;,do,statement;,while(-i 0);,Template,技术,代替,runtime,的临时变量,template,class,countBits,enum,bit3=(N&0 x08)?1:0,bit2=(N&0 x04)?1:0,bit1=(N&0 x02)?1:0,bit0=(N,public:,enum,nbits,=bit0+bit1+bit2+bit3;,;,int,i=,countBits,:,nbits,;,int,countBits(int,N),int,bit3=(N&0 x08)?1:0,bit2=(N&0 x04)?1:0,bit1=(N&0 x02)?1:0,bit0=(N,return bit0+bit1+bit2+bit3;,int,i=countBits(13);,Template,技术 计算,Compile-time functions,一般原则:,局部变量用,enum,类型,循环转化为递归,结束条件为一个特化版本,也可以是多重循环,需要用到部分特化特性,条件分支用模板特化解决,效果:以类型为基础,实现各种操作,例如,sin x=x-x3/3!+x5/5!-x7/7!+,在编译时刻求,pow(x,y,),,即,x,的,y,次方,Template,技术 计算,pow(x,y,),template,struct,ctime_pow,enum,result=X*,ctime_pow,:result;,;,template,struct,ctime_pow,enum,result=1;,;,用法:,const,int,z=,ctime_pow,:result;,Trait,技术,定义一些“函数”,这些函数的参数和返回值都是类型(,type),,而不是数据(,data),例如:对于一个数组类,它的元素类型和平均数的类型不一定相同,可以用一个,trait class,来建立这种映射关系,对应关系,Average_type(T,)-T,Average_ty
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!