practices of good component design n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Practices of Good Component Design PowerPoint Presentation
Download Presentation
Practices of Good Component Design

Loading in 2 Seconds...

play fullscreen
1 / 47

Practices of Good Component Design - PowerPoint PPT Presentation


  • 99 Views
  • Uploaded on

Practices of Good Component Design. Microsoft Research Asia Advanced Technology. Introduction. Barn-Wan Li Born in Toronto, Canada B.A., University of California, Berkeley Microsoft Silicon Valley Campus Microsoft Research Asia, Beijing. Software Components. Conceptualized in the 60s

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Practices of Good Component Design


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
practices of good component design

Practices of Good Component Design

Microsoft Research Asia Advanced Technology

introduction
Introduction
  • Barn-Wan Li
  • Born in Toronto, Canada
  • B.A., University of California, Berkeley
  • Microsoft Silicon Valley Campus
  • Microsoft Research Asia, Beijing
software components
Software Components
  • Conceptualized in the 60s
  • Improve encapsulation and reuse
software components1
Software Components
  • Conceptualized in the 60s
  • Improve encapsulation and reuse
software components2
Software Components
  • Conceptualized in the 60s
  • Improve encapsulation and reuse

PowerPoint

Word

Excel

OfficeArt Drawing

how is a component different than an object
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
how is a component different than an object1
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
how is a component different than an object2
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
inheritance for reuse2
Inheritance for Reuse
  • class Car
  • {
  • };
  • class Racecar : public Car
  • {
  • };
  • class Truck : public Car
  • {
  • };
  • class Taxi : public Car
  • {
  • };
inheritance for reuse3
Inheritance for Reuse
  • class Truck
  • {
  • };
inheritance for reuse4
Inheritance for Reuse
  • class Truck
  • {
  • };
  • class FireHydrant
  • {
  • };
  • class FireTruck :
  • public Truck,
  • public FireHydrant
  • {
  • };
inheritance for reuse5
Inheritance for Reuse

X

  • class Truck
  • {
  • };
  • class FireHydrant
  • {
  • };
  • class FireTruck :
  • public Truck,
  • public FireHydrant
  • {
  • };
inheritance for reuse6
Inheritance for Reuse
  • polymorphism
  • incremental behavior changes
  • self-recursive down-calls
inheritance for reuse7
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; }

};

inheritance for reuse10
Inheritance for Reuse
  • breaks encapsulation
  • breaks data hiding
  • breaks implementation hiding
  • compile-time and run-time dependencies
  • syntactic and semantic fragility
reuse and sharing templates
class LinkList

{

};

class MyObjList : public LinkList

{

};

Reuse and Sharing - Templates
reuse and sharing templates1
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

{

};

Reuse and Sharing - Templates
reuse and sharing templates2
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;

Reuse and Sharing - Templates
reuse and sharing templates3
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
reuse and sharing templates4
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

X

reuse and sharing containment
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;
  • };
what is a component
What is a Component
  • different layers
    • within a single system
    • sharable library
    • crossing application boundaries
    • crossing system boundaries
  • encapsulation and abstraction
how is a component design good
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.
life time of a component
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
example face detection
Example – Face Detection
  • BOOL DetectFace(HBITMAP image, CArray<RECT> &faces, CArray<long> &angles);
example face detection1
Example – Face Detection
  • BOOL DetectFace(HBITMAP image, CArray<RECT> &faces, CArray<long> &angles);
  • BOOL DetectFace(HBITMAP image, int &facenum, RECT *faces[], long *angles[]);
example face detection2
Example – Face Detection
  • BOOL 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);
example face detection3
Example – Face Detection
  • BOOL 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);
introducing com
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

}

face detection interface
interface IFaceDetector

{

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

};

Face Detection Interface
face detection interface1
interface 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);

};

Face Detection Interface
face detection interface2
interface 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);

};

Face Detection Interface
face detection interface3
interface 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);

};

Face Detection Interface
face detection interface4
interface 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);

};

Face Detection Interface
face detection interface5
Face Detection Interface

Application

Face Detection Component

face detection interface6
Face Detection Interface

Application

IFDImage

IFaceDetector

IFDFaceEnum

IFDFace

Face Detection Component

face detection implementation
interface IFaceDetector

{

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

};

Face Detection Implementation
face detection implementation1
interface IFaceDetector

{

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

};

class CFaceDetector : public IFaceDetector

{

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

};

Face Detection Implementation
face detection implementation2
interface IFaceDetector

{

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

};

class CFaceDetector : public IFaceDetector

{

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

};

Face Detection Implementation

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

{

if (pImage == NULL ||

ppResult == NULL)

return E_INVALIDARG;

:

* ppFaces = new CFDFaceEnum;

return E_SUCCESS;

}

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);

};

Face Detection Usage
face detection interface7
Face Detection Interface

Application

IFDImage

IFaceDetector

IFDFaceEnum

IFDFace

CFaceDetector

CFDFaceEnum

CFDFace

Face Detection Component

tips for authoring a component
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