YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

Practices of Good Component Design

Microsoft Research Asia Advanced Technology

Page 2: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

2

Introduction

Barn-Wan Li

Born in Toronto, Canada

B.A., University of California, Berkeley

Microsoft Silicon Valley Campus

Microsoft Research Asia, Beijing

Page 3: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

3

Software Components

Conceptualized in the 60s

Improve encapsulation and reuse

Page 4: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

4

Software Components

Conceptualized in the 60s

Improve encapsulation and reuse

Page 5: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

5

Software Components

Conceptualized in the 60s

Improve encapsulation and reuse

PowerPoint Word Excel

OfficeArt Drawing

Page 6: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

6

How is a Component different than an Object?

Object-oriented Programming History– created in the 60s

• real-world modeling and metaphors• microcosm of objects with messages

Page 7: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

7

How is a Component different than an Object?

Object-oriented Programming History– created in the 60s

• real-world modeling and metaphors• microcosm of objects with messages

– the promise of the 80s• combining the concept of components for

abstraction and reuse

Page 8: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

8

How is a Component different than an Object?

Object-oriented Programming History– created in the 60s

• real-world modeling and metaphors• microcosm of objects with messages

– the promise of the 80s• combining the concept of components for

abstraction and reuse

– fear in the 90s• fragile software reuse with objects

Page 9: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

9

Inheritance for Reuse

class Car

{

};

Page 10: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

10

Inheritance for Reuse

class Car

{

};

Page 11: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

11

Inheritance for Reuse

class Car

{

};

class Racecar : public Car

{

};

class Truck : public Car

{

};

class Taxi : public Car

{

};

Page 12: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

12

Inheritance for Reuseclass Truck

{

};

Page 13: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

13

Inheritance for Reuseclass Truck

{

};

class FireHydrant

{

};

class FireTruck :

public Truck,

public FireHydrant

{

};

Page 14: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

14

Inheritance for Reuseclass Truck

{

};

class FireHydrant

{

};

class FireTruck :

public Truck,

public FireHydrant

{

};

Page 15: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

15

Inheritance for Reuse

• polymorphism• incremental behavior changes• self-recursive down-calls

Page 16: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

16

Inheritance for Reuse

• polymorphism• incremental behavior changes• self-recursive down-calls

class Car {

void Stop() { SayMessage(); }

virtual void SayMessage()

{ cout << “Bye!” << endl; }

};

class Taxi : public Car {

virtual void SayMessage()

{ cout << “50 RMB please” <<

endl; }

};

Page 17: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

17

Inheritance for Reuse

Page 18: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

18

Inheritance for Reuse

Page 19: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

19

Inheritance for Reuse

breaks encapsulation

breaks data hiding

breaks implementation hiding

compile-time and run-time dependencies

syntactic and semantic fragility

Page 20: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

20

Reuse and Sharing - Templates

class LinkList

{

};

class MyObjList : public LinkList

{

};

Page 21: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

21

Reuse and Sharing - Templates

template <class T>

class LinkList

{

T *operator -> () { return &m_t; }

T m_t;

};

class MyObj

{

};

typedef LinkList<MyObj> MyObjList;

class LinkList

{

};

class MyObjList : public LinkList

{

};

Page 22: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

22

Reuse and Sharing - Templates

class LinkList

{

};

class MyObjList : public LinkList

{

};

class MyObjTree : public BinTree

{

};

template <class T>

class LinkList

{

T *operator -> () { return &m_t; }

T m_t;

};

class MyObj

{

};

typedef LinkList<MyObj> MyObjList;

typedef BinTree<MyObj> MyObjTree;

Page 23: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

23

Reuse and Sharing - Templates

class LinkList

{

};

class MyObjList : public LinkList

{

};

class MyObjTree : public BinTree

{

};

class MyObjList : public MyObj, public LinkList

{

};

template <class T>

class LinkList

{

T *operator -> () { return &m_t; }

T m_t;

};

class MyObj

{

};

typedef LinkList<MyObj> MyObjList;

typedef BinTree<MyObj> MyObjTree;

Page 24: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

24

class LinkList

{

};

class MyObjList : public LinkList

{

};

class MyObjTree : public BinTree

{

};

class MyObjList : public MyObj, public LinkList

{

};

template <class T>

class LinkList

{

T *operator -> () { return &m_t; }

T m_t;

};

class MyObj

{

};

typedef LinkList<MyObj> MyObjList;

typedef BinTree<MyObj> MyObjTree;

Reuse and Sharing - Templates

Page 25: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

25

Reuse and Sharing - Containment

class LinkList

{

void Set(Object *pobj) { m_pObject = pobj; }

Object *Get() { return m_pObject; }

Object *m_pObject;

};

class Object

{

Object() { m_linklist.Set(pobj); }

Object *Next() { return m_linklist.Next().Get(); }

LinkList m_listlist;

};

Page 26: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

26

What is a Component

different layers– within a single system– sharable library– crossing application boundaries– crossing system boundaries

encapsulation and abstraction

Page 27: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

27

How is a Component Design “Good”?

• the component’s interface upholds a clear “contract”.

• changes to the implementation of the component do not require changes in the code that uses it.

• the component has reuse value when the time required to understand and integrate for a new user is faster and easier than to rewrite it.

Page 28: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

28

Life-time of a Component

• conception of an abstract layer, a sharable service, or a piece of code that has reuse value

• understanding the requirements and limitations

• designing the interfaces• implementation• creating the user of the component that

wraps and tests the implementation

Page 29: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

29

Example – Face DetectionBOOL DetectFace(HBITMAP image,

CArray<RECT> &faces, CArray<long> &angles);

Page 30: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

30

Example – Face DetectionBOOL DetectFace(HBITMAP image,

CArray<RECT> &faces, CArray<long> &angles);

BOOL DetectFace(HBITMAP image, int &facenum, RECT *faces[], long *angles[]);

Page 31: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

31

Example – Face DetectionBOOL DetectFace(HBITMAP image,

CArray<RECT> &faces, CArray<long> &angles);

BOOL DetectFace(HBITMAP image, int &facenum, RECT *faces[], long *angles[]);

ULONG DetectFaceNumber(HBITMAP image);

BOOL DetectFace(HBITMAP image, int index, RECT *face, long *angle);

Page 32: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

32

Example – Face DetectionBOOL DetectFace(HBITMAP image,

CArray<RECT> &faces, CArray<long> &angles);

BOOL DetectFace(HBITMAP image, int &facenum, RECT *faces[], long *angles[]);

ULONG DetectFaceNumber(HBITMAP image);

BOOL DetectFace(HBITMAP image, int index, RECT *face, long *angle);

BOOL DetectFace(HDC image, int width, int height, int index, RECT *face, long *angle);

BOOL DetectFace(void pvBits, int width, int height, int bitsperpixel, int index, RECT *face, long *angle);

Page 33: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

33

Introducing COM

• Component Object Model• interface == pure virtual class• may be more than one object• language independent

interface IUnknown

{

ULONG AddRef(); // reference counting

ULONG Release();

HRESULT QueryInterface(); // dynamic casting

}

Page 34: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

34

Face Detection Interfaceinterface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

Page 35: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

35

Face Detection Interfaceinterface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

interface IFDImage

{

HRESULT SetHBitmap(HBITMAP bitmap);

HRESULT SetHDC(HDC image, int width, int height);

HRESULT SetBits(void pvBits, int width, int height, int bitsperpixel);

};

Page 36: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

36

Face Detection Interfaceinterface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

interface IFDImage

{

HRESULT SetHBitmap(HBITMAP bitmap);

HRESULT SetHDC(HDC image, int width, int height);

HRESULT SetBits(void pvBits, int width, int height, int bitsperpixel);

};

interface IFDFaceEnum

{

HRESULT GetNum(ULONG *pNum);

HRESULT GetFace(int index, IFDFace **pFace);

};

Page 37: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

37

Face Detection Interfaceinterface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

interface IFDImage

{

HRESULT SetHBitmap(HBITMAP bitmap);

HRESULT SetHDC(HDC image, int width, int height);

HRESULT SetBits(void pvBits, int width, int height, int bitsperpixel);

};

interface IFDFaceEnum

{

HRESULT GetNum(ULONG *pNum);

HRESULT GetFace(int index, IFDFace **pFace);

};

interface IFDFace

{

HRESULT GetRect(RECT *pRect);

HRESULT GetAngle(long *pAngle);

};

Page 38: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

38

Face Detection Interfaceinterface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

interface IFDImage

{

HRESULT SetHBitmap(HBITMAP bitmap);

HRESULT SetHDC(HDC image, int width, int height);

HRESULT SetBits(void pvBits, int width, int height, int bitsperpixel);

};

interface IFDFaceEnum

{

HRESULT GetNum(ULONG *pNum);

HRESULT GetFace(int index, IFDFace **pFace);

};

interface IFDFace

{

HRESULT GetRect(RECT *pRect);

HRESULT GetAngle(long *pAngle);

HRESULT GetLeftEye(RECT *pRect);

HRESULT GetRightEye(RECT *pRect);

HRESULT GetAccuracy(long *pPercent);

};

Page 39: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

39

Face Detection Interface

Face Detection Component

Application

Page 40: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

40

Face Detection Interface

IFaceDetectorIFDImage

Face Detection Component

IFDFaceEnum IFDFace

Application

Page 41: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

41

Face Detection Implementation

interface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

Page 42: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

42

Face Detection Implementation

interface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

class CFaceDetector : public IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

Page 43: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

43

Face Detection Implementation

HRESULT CFaceDetector::Detect(IFDImage *pImage, IFDFaceEnum **ppFaces)

{

if (pImage == NULL ||

ppResult == NULL)

return E_INVALIDARG;

:

* ppFaces = new CFDFaceEnum;

return E_SUCCESS;

}

interface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

class CFaceDetector : public IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

Page 44: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

44

Face Detection Usage

void main()

{

CComPtr<IFaceDetector> pDetector;

pDetector.CreateInstance(

CLSID_FaceDetector);

:

CComPtr<IFDFaceEnum> pResult;

pDetector->Detect(pImage, pResult);

:

}

interface IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

class CFaceDetector : public IFaceDetector

{

HRESULT Detect(IFDImage *pImage, IFDFaceEnum **ppFaces);

};

Page 45: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

45

Face Detection Interface

IFaceDetector

CFaceDetector

IFDImage

Face Detection Component

IFDFaceEnum

CFDFaceEnum

IFDFace

CFDFace

Application

Page 46: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

46

Tips for Authoring a Component

• know your callers• check for parameter errors at external APIs , assert for

internal• avoid allocating memory that the caller must free• use existing types-- or create your own abstract

interfaces• aggregate types for reuse or to hide complexity

Page 47: Practices of Good Component Design Microsoft Research Asia Advanced Technology.

47


Related Documents