Tow points of WebKit in design

Preview:

DESCRIPTION

Just describe two basic design concepts which are applied in WebKit.

Citation preview

陈浩 http://blog.csdn.net/horkychen

以小见大以小见大 WebKit 中两个小设计

AgendaAgenda

•Private Data Class Pattern

•RAII

Private Data Class Private Data Class PatternPattern

绝对的权力必然产生绝对的腐败

Country

KingKing

-Slaves-Properties-Lands

-Slaves-Properties-Lands

+ 内政+ 外交- 人事……

+ 内政+ 外交- 人事……

3. 缺乏弹性3. 缺乏弹性

1. 高耦合1. 高耦合

2. 权力泛滥带来的风险2. 权力泛滥带来的风险

friend class ResourceHandleInternal; OwnPtr<ResourceHandleInternal> d;

friend class ResourceHandleInternal; OwnPtr<ResourceHandleInternal> d;

内部数据内部数据

对外接口及业务逻辑

对外接口及业务逻辑

Opaque PointerOpaque Pointer

d-pointer d-pointer

为什么不直接使用私有成员变量?为什么不直接使用私有成员变量?

Three benefits of Opaque Pointer

1.To hide detail class implementation.2.To add new data member without compatibility

issue.3.To includes less header files and get faster

compilation time.

Three benefits of Opaque Pointer

1.To hide detail class implementation.2.To add new data member without compatibility

issue.3.To includes less header files and get faster

compilation time.

内部细节的实现逻辑

内部细节的实现逻辑

对外接口及业务逻辑

对外接口及业务逻辑

The benefits of Pimpl compared to OP

1.More pure encapsulation.2.Minimize the coupling.

……

The benefits of Pimpl compared to OP

1.More pure encapsulation.2.Minimize the coupling.

……

抽象与实现可以分开演进

抽象与实现可以分开演进

LibraryLibraryApplicationApplication

Fragile Binary Interface Problem

In C++, when anything in a class definition changes (even private

members) all users of that class must be recompiled.

Fragile Binary Interface Problem

In C++, when anything in a class definition changes (even private

members) all users of that class must be recompiled.

Class BClass B Class AClass A

添加新成员

添加新成员

RResourceesource A Acquisitioncquisition I Iss IInitializationnitialization

简单直接

{ NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel); //Do Something}

{ NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel); //Do Something}

{ ++m_scriptNestingLevel; //Do Something --m_scriptNestingLevel;}

{ ++m_scriptNestingLevel; //Do Something --m_scriptNestingLevel;}

{ ++m_scriptNestingLevel; try { //Do Something } finally { --m_scriptNestingLevel; } }

{ ++m_scriptNestingLevel; try { //Do Something } finally { --m_scriptNestingLevel; } }

Not

sup

porte

d in

C++

Not

sup

porte

d in

C++

The idea is that an object's destructor is responsible for freeing resources.

{ NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel); //Do Something}

{ NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel); //Do Something}

NestingLevelIncrementer::~NestingLevelIncrementer(); NestingLevelIncrementer::~NestingLevelIncrementer();

{ MutexLocker locker(m_eventsLock); //Do Something}

{ MutexLocker locker(m_eventsLock); //Do Something}

好奇好奇 模式 / 规则模式 / 规则 转述转述

……以小见大、不断积累……以小见大、不断积累

•MMore C++ idioms <ore C++ idioms <LINK>>

•UUsing a d-pointer <LINK>sing a d-pointer <LINK>

•PPrivate Data Class Pattern <LINK>rivate Data Class Pattern <LINK>

•OOpaque Pointer <LINK>paque Pointer <LINK>

•PPimpl idiom <LINK>impl idiom <LINK>

•FFast Pimpl idiom <LINK>ast Pimpl idiom <LINK>

•MMaking Pimpl Easy <Dr. Dobb’s>aking Pimpl Easy <Dr. Dobb’s>

•FFragile Binary Interface Problem <LINK>ragile Binary Interface Problem <LINK>