Title: http://www.icst.pku.edu.cn/CompCourse
1????????
- ???
- http//www.icst.pku.edu.cn/CompCourse
2??
- ??
- Generic Programming
- ????patterns
- ????(framework)???
- ??????????
3??creational patters
- Factory Method
- ?????virtual method??????
- Abstract Factory
- ??product??factory method?????factory??
- Prototype
- ??product?????product,Cloneprototype manager
- Builder
- ?????????builder?????????????
- Singleton
- ?????,????????????????????
- Finder
- ??????????????
4??Structural patterns
- Adapter ?bridge?facade
- adapter??????????????
- bridge???????????????????
- facade??????????????????????
- composite?decorator?proxy
- composite??????????
- decorator???????????
- proxy????????????
- flyweight
- ????????????????
5??Behavioral Patterns
- Command
- ???????,????????????????
- Iterator
- ??????????????
- Observer
- ???????????,??????????????
- Strategy
- ?????????????????????????
- Visitor
- ????????????????????
6??Behavioral Patterns(??)
- Chain of Responsibility
- ???????,?????,decouple???????
- Interpreter
- ???????,??????interpret??
- Mediator
- ???mediator?decouple?????
- Memento
- ??????????????
- State
- ????????????,????????????
- Template Method
- ???????????,???????????
7??Behavioral Patterns(??)
- Strategy?Iterator?Mediator?State?command
- ????????????,????????????????
- Mediator?Observer
- Observer???subject?observer????????
- mediator????????? -gt????
- command?Chain of Responsibility?interpreter
- command????????????
- Chain of Responsibility??????????
- interpreter??????????????
8Generic programming
- ??
- ??????????????(reuse through parameterization),?
??????????????????,?????????? Generic
Programming???????????????????? - STL????generic programming??
- generic programming????????????????????,?????????
???????????,???????????
9???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(??)????????????????????????comp
ile-time,????runtime - AOSD, ??http//aosd.net/
10generative programming
- Generative Programming
- ????????????,???????,??????????????,????,?????????
????????????????? - ????
- ?????????????
- ???????????????
- ??????(compile-time)????????,??????????????
- ????????????,?????????????
- Separation of concerns borrowed from AOP
- ??
- Generative Programming??????????????
- ?Domain Engineering??
- A Model-Based Approachhttp//www.sei.cmu.edu/doma
in-engineering/domain_engineering.html - Active Libraries
11C Generic Programming
- Template??
- ?C??two-level language
- metaprogram
- ???????-gt??????,?generic programming
- ?????
- ???????
- ?????,?????? partial evaluation
- ???????
- Active libraries,?????????,????????
- ????,?policy(traits)?????????
- ????????
- ??template class??template function
12C Generic Programming(?)
- ????
- ????????,???logic-programming?,?compile-time?????
?????,?? - ???????
- if/else,loop,switch
- ?type???????????(??)
- ??????
- ???compile-time error,???runtime error
- ?????? ???
- ????????????,????patterns
13Template?? class template
- templatelttypename T, size_t MAX_ELEMS 8 gt
- class Array
- public
- T operator(size_t n)
- if (n lt 0 ngtMAX_ELEMS) throw "Out of bounds!
" - return m_rgn
-
- protected
- T m_rgMAX_ELEMS
?? Arrayltlong, 8gt a1 Arrayltchar, 200gt a2
14Template?? ????template specialization
- templateltgt
- class Arrayltchar, 256gt
- public
- char operator(size_t n)
- if (n lt 0 ngt256) throw "Out of bounds!"
- return m_szn
-
- bool operator (const Arrayltchar, 256gt rhs)
- return strcmp(m_sz, rhs.m_sz) 0
-
- protected
- char m_sz256
15Template?? ?????? partial template
specialization
- templateltsize_t MAX_ELEMSgt
- class Arrayltchar , MAX_ELEMSgt
- public
- T operator(size_t n)
- if (n lt 0 ngtMAX_ELEMS) throw "Out of
bounds!" - return m_szn
-
- bool operator (const Arrayltchar, MAX_ELEMSgt
rhs) - return strcmp(m_sz, rhs.m_sz) 0
-
- protected
- T m_szMAX_ELEMS
-
- Visual C 6?????????
16Template?? ????function template
????,compile-time???
- template lt class T gt
- void Swap(T a, Tb)
-
- T temp a
- a b
- b temp
-
- template ltclass Tgt
- T min(T a, T b)
-
- return a lt b ? a b
????,runtime???
17Template?? ??????generalized
functors(function objects)
- functor???operator()?C??,??????????,????????,??
????????,?????(value semantic)?????template
class,????? - template lt typename ResultType gt
- class Functor
-
- public
- ResultType operator()()
- // other member function
- private
- // implementation
-
- ??
- Functorltintgt MyFunctor(val1)
- int Result MyFunctor()
18Template?? ??runtime?if/else
- // Class declarations
- templateltbool Cgt
- class ConditionProcess
- class ConditionProcess lttruegt
- public
- static inline void f()
- statement1 // true case
-
- class ConditionProcess ltfalsegt
- public
- static inline void f()
- statement2 // false case
-
- // Replacement for 'if/else' statement
- ConditionProcess ltconditiongtf()
if (condition) statement1 else
statement2
Compile-time????condition??
19Template?? ??runtime?switch
- // Class declarations
- templateltint Igt
- class SwitchProcess
- public
- static inline void f() default-statement
-
- class SwitchProcess ltvalue1gt
- public
- static inline void f() statement1
-
- class SwitchProcess ltvalue2gt
- public
- static inline void f() statement2
-
- // Replacement for switch(i) statement
- SwitchProcess ltIgtf()
int i switch(i) case value1
statement1 break case value2
statement2 break default
default-statement break
20Template?? ??runtime?do??
- templateltint Igt
- class DoProcess
- private
- enum go (I-1) ! 0
- public
- static inline void f()
- statement
- DoProcess ltgo ? (I-1) 0gtf()
-
-
- // Specialization provides base case for
recursion - class DoProcess lt0gt
- public
- static inline void f()
-
- // Equivalent loop code
- DoProcess ltNgtf()
int i N do statement while (--i gt 0)
21Template?? ??runtime?????
- templateltint Ngt
- class countBits
- enum
- bit3 (N 0x08) ? 1 0,
- bit2 (N 0x04) ? 1 0,
- bit1 (N 0x02) ? 1 0,
- bit0 (N 0x01) ? 1 0
- public
- enum nbits bit0bit1bit2bit3
-
- int i countBitslt13gtnbits
int countBits(int N) int bit3 (N 0x08)
? 1 0, bit2 (N 0x04) ? 1 0,
bit1 (N 0x02) ? 1 0, bit0 (N
0x01) ? 1 0 return bit0bit1bit2bit3
int i countBits(13)
22Template?? ?? Compile-time functions
- ????
- ?????enum??
- ???????,???????????
- ????????,??????????
- ???????????
- ????????,??????
- ??
- sin x x - x3/3! x5/5! - x7/7!
- ??????pow(x,y),?x?y??
23Template?? ??pow(x,y)
- templateltint X, int Ygt
- struct ctime_pow
- enum result Xctime_powltX, Y-1gtresult
-
- templateltint Xgt
- struct ctime_powltX, 0gt
-
- enum result 1
-
- ??
- const int z ctime_powlt5,3gtresult
24Trait??
- ??????,???????????????(type),?????(data)
- ?????????,??????????????????,?????trait
class????????? - ????
- Average_type(T) -gt T
- Average_type(int) -gt float
- Trait???Average???
25Partial evaluation
- ?????????????
- ???????????
- ???????????
- ??,????????
26Template?? ???????
- ?????????????
- templateltsize_t MAX_LENgt
- class String public Arrayltchar, MAX_LEN1gt
- public
- // additional functionality
- bool operator(const StringltMAX_LENgt rhs)
-
- return strcmp(m_rg, rhs.m_rg) 0
-
27Template?? ?????????
- ??????????????????
- ??????,??????
- templatelttypename Base, typename Policy1gt
- class Deriving public BaseltPolicy1gt
-
28C as a two-level language
- ?type??first-class value???
- ??
- ???? ?????
- typedef T T_average
- ???
- typedef T_average T
- ????????
29Template?? typelistfrom Modern C Design
- ??????????
- template ltclass T, class Ugt
- struct Typelist
-
- typedef T Head
- typedef U Tail
-
- ??Length??
- template ltclass TListgt struct Length
- template ltgt struct LengthltNullTypegt
-
- enum value 0
-
-
- template ltclass T, class Ugt
- struct Lengthlt TypelistltT, Ugt gt
-
- enum value 1 LengthltUgtvalue
-
- typelist????
- Length
- TypeAt
- IndexOf
- Append
- Erase
- Replace
- MostDerived
- ...
30Template?? typelist(??)
- typelist??
- define TYPELIST_1(T1) TypelistltT1, NullTypegt
- define TYPELIST_2(T1, T2) TypelistltT1,
TYPELIST_1(T2) gt - define TYPELIST_3(T1, T2, T3) TypelistltT1,
TYPELIST_2(T2, T3) gt - template ltclass T1, class T2, template ltclassgt
class Unitgt - class GenScatterHierarchyltTypelistltT1, T2gt,
Unitgt - public GenScatterHierarchyltT1, Unitgt
- , public GenScatterHierarchyltT2, Unitgt
-
-
- template ltclass AtomicType, template ltclassgt
class Unitgt - class GenScatterHierarchy public
UnitltAtomicTypegt -
-
- template lttemplate ltclassgt class Unitgt
- class GenScatterHierarchyltNullType, Unitgt
-
31Template?? typelist(??)
- GenScatterHierarchy??
- template ltclass Tgt
- class Holder
-
- T m_value
typedef GenScatterHierarchy lt TYPELIST_3(int,
string, CustomClass), Holder gt MyTypeTree
32Template?????? ????????
templatelttypename T, typename Derivinggt class
Array public ... bool operatorlt(const
ArrayltTgt rhs) return static_castltDeriving
gt(this)-gt Compare(rhs) lt 0 bool
operatorgt(const ArrayltTgt rhs) return
static_castltDeriving gt(this)-gt Compare(rhs) gt
0 bool operator(const ArrayltTgt rhs)
return static_castltDeriving gt(this)-gt Compare(
rhs) 0
- templatelttypename Tgt
- class Array
- public
-
- virtual int Compare(const ArrayltTgtrhs)0
- bool operatorlt(const ArrayltTgt rhs)
- return this-gtCompare(rhs) lt 0
- bool operatorgt(const ArrayltTgt rhs)
- return this-gtCompare(rhs)gt 0
- bool operator(const ArrayltTgt rhs)
- return this-gtCompare(rhs) 0
33policy
- Aliases strategy, behavior class, trait, aspect
- ???????????????,???????????,??????????,???????????
- ?????????????
34Policy(??)
- ???policy??????????
- ???????????????????,???policy class???,??
- ????
- ????
- policy????????
- ?????,???????????,????????(RefCountPolicy)?????(St
oragePolicy) - policy??host??????runtime????,????compile-time????
35Policy(??)
- ???????policy class
- ????compile-time?host class????
- ??
- templatelt typename ThreadingModelSingleThreaded
gt - class Widget
-
- templatelttemplateltclass Createdgt class
CreationPolygt - class WidgetManager
36Policy(??)
- Policy??,?mn?????nm???,??
- template
- lt
- class T,
- template ltclassgt class CheckingPolicy,
- templateltclassgt ThreadingModel
- gt
- class SmartPtr
-
- typedef SmartPtrltWidget, EnsureNotNull,
SingleThreadedgt SafeWidgetPtr - templateltclass Tgtstruct EnsureNotNull
-
- static void Check(T ptr)
- if (!ptr) ptr GetDefaultValue()
37Policy(??)
- ?????Policy???
- ?compile-time??host class,????????????,??generic
- ??????????,??policy????????,?class-level???
- ??????????????,???????policy,???policy???
- ????hook???,????????policy class,???????????
- ??
- ???????policy??????,??????????????????
38Visitor??(GoF)
39Visitor????
????
Visitor??
EleVisitor
ElementAVisitor
ElementBVisitor
ConcreteElementA Accept(EleVisitor) OperationA
ConcreteElementB Accept(EleVisitor) OperationB
MyConcreteVisitor
40Lazy??
- Lazy Initialization,??????????
- Singleton SingletonInstance()
-
- if (!pInstance)
-
- pInstance new Singleton
-
- return pInstance
-
- COW(Copy on write),??????????
- Lazy ProtocolDCOM??
41Double-Checked Locking Pattern
- ??mutex???Mutex??,?????pInstance???????,?????
- Singleton SingletonInstance()
-
- Lock guard(mutex)
- if (!pInstance)
-
- pInstance new Singleton
-
- return pInstance
42Double-Checked Locking Pattern(??)
- ??????????,????new??????,?????????????,??
- Singleton SingletonInstance()
-
- if (!pInstance)
-
- Lock guard(mutex)
- pInstance new Singleton
-
- return pInstance
43Double-Checked Locking Pattern(??)
- Double-Checked Locking Pattern
- Singleton SingletonInstance()
-
- if (!pInstance)
-
- Lock guard(mutex)
- if (!pInstance)
-
- pInstance new Singleton
-
-
- return pInstance
44Table-driven pattern
- ??runtime switch
- ???????????CShape??????,?????
- ReadWord(stream, typeid)
- switch (typeid)
- case Line_ID
- pObj (CShape )new CLine
- pObj-gtLoad(stream)
- break
- case Poly_ID
-
-
- pDoc.Add(pObj)
45Table-driven pattern(?)
- ?????(typeid, fnCreator)
- ??typeid???????,??????,??????
- ReadWord(stream, typeid)
- FnCreator fn TableInstance().Lookup(typeid)
- pObj fn()
- pObj-gtLoad(stream)
- pDoc.Add(pObj)
- ?MFC/ATL????table-driven???
- ????????double-dispatch??multi-dispatch??
46??????union
- Union??????
- Discriminated union tag union
- ??VARIANT
- ???????????union,????????union???????
- ??Java
- ????????
- ?????
- ???????
- ????
- ????????????(OO)
- ??Effective Java
47????????union
48??enum??
- Enum?????
- ??????????????
- ????,??enum??????????
- ??????????
- ?????(?????)?????
- typesafe enum pattern, from Effective Java
- ????????????????,????????????????,???????final?,?
?????????????????
49framework
- ????
- ???? gt????
- ????????
- ???
- ??????,??????????????????
- ??????framework
- ????????framework,??MMC
- ??????framework,??MFC
50??????framework
- ??
- ??????????????
- ???????
- ??????
- ??
- ??????
- ????,??????????
- ????
- ???????????
- ???????????
- ????????
- ??????????
51??????framework
- ??
- ??????,??????????,????????
- ??????????
- ????
- ???????????
- ???????????????
52????????????????
- ??
- ???????????????????,??????????????????
- ???generic programming????,?????????????????(?????
) - ????
- ?????????? runtime???
- ??????????,???????????????,?????????????????
- ????????framework??
53Framework??
IFrameSite
SnapIn DLL
SnapIn DLL
SnapIn DLL
ISnapInfo
SnapIn??
SnapIn??
SnapIn??
SnapIn??
FrameSite
FrameSite
FrameSite
Snap-In???
Security???
Database???
UI???
54????????(?)
- ?????????,???????????????,???
- ?????????
- ????????
- ??????
- ????????????????,???????????
- ?????
- ?????????????????????????
- ???????
- ????
55????????(?)
- ?????
- ???????,?????????
- ?????C/C
- ??????????
- ??????????
- facade??
- ????
- ?????????????
- ?????
- out??????????????size?
- ?????????????,???????????
56????????(?)
- ??????
- ?????,?????
- policy??????????
- ????????????????????,??????????
- ?????????????????????
- ??????????????????
- ????????
- ????????compile-time????
- ????????
- ????????
57????????(?)
- ???????
- ???????????
- ???? ????????
- ??functor??????
- ??(??)?????????
- ???????
- ????
- ?????
- ??????????,????????
- ??????policy????,?????????
- ???policy???,????????????
58????????(?)
- ?????
- ??????,??assert??
- ?????
- ????????????
- ?????
- ????????
- ??
- ????
59????????(?)
- ??MFC/ATL
- MFC
- ?????????????
- ??????C??,?Win32?????
- ??????????,????
- ?????patterns,??Windows????
- ???????????
- ?Wizard????????
- ATL
- ???generic programming???????
- ????
- ??????
60??
- C??
- COM??
- COM??
- COM
- Design Patterns
- ??????