19
陈陈 http://blog.csdn.net/horkychen 陈陈陈陈 陈陈陈陈 WebKit 陈陈陈陈陈陈

Tow points of WebKit in design

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Tow points of WebKit in design

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

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

Page 2: Tow points of WebKit in design

AgendaAgenda

•Private Data Class Pattern

•RAII

Page 3: Tow points of WebKit in design

Private Data Class Private Data Class PatternPattern

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

Page 4: Tow points of WebKit in design

Country

KingKing

-Slaves-Properties-Lands

-Slaves-Properties-Lands

+ 内政+ 外交- 人事……

+ 内政+ 外交- 人事……

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

1. 高耦合1. 高耦合

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

Page 5: Tow points of WebKit in design

friend class ResourceHandleInternal; OwnPtr<ResourceHandleInternal> d;

friend class ResourceHandleInternal; OwnPtr<ResourceHandleInternal> d;

内部数据内部数据

对外接口及业务逻辑

对外接口及业务逻辑

Opaque PointerOpaque Pointer

d-pointer d-pointer

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

Page 6: Tow points of WebKit in design

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.

Page 7: Tow points of WebKit in design

内部细节的实现逻辑

内部细节的实现逻辑

对外接口及业务逻辑

对外接口及业务逻辑

Page 8: Tow points of WebKit in design

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.

……

Page 9: Tow points of WebKit in design

抽象与实现可以分开演进

抽象与实现可以分开演进

Page 10: Tow points of WebKit in design

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

添加新成员

添加新成员

Page 11: Tow points of WebKit in design

RResourceesource A Acquisitioncquisition I Iss IInitializationnitialization

简单直接

Page 12: Tow points of WebKit in design

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

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

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

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

Page 13: Tow points of WebKit in design

{ ++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++

Page 14: Tow points of WebKit in design

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

Page 15: Tow points of WebKit in design

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

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

Page 16: Tow points of WebKit in design
Page 17: Tow points of WebKit in design

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

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

Page 18: Tow points of WebKit in design

•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>

Page 19: Tow points of WebKit in design