zca: a component architecture for python

18

Click here to load reader

Upload: plone-foundation

Post on 08-May-2015

1.315 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: ZCA: A component architecture for Python

A Component Architecture for Python

Timo Stollenwerk

February 26th, 2009

Timo Stollenwerk A Component Architecture for Python

Page 2: ZCA: A component architecture for Python

Introduction

Developing a large software system is always very complicated

How to avoid starting from the scratch over and over again?

How to reuse, customize and extend existing functionality?

Timo Stollenwerk A Component Architecture for Python

Page 3: ZCA: A component architecture for Python

Object-orientation

Object-orientation provides two approaches

Subclassing

Delegation

Timo Stollenwerk A Component Architecture for Python

Page 4: ZCA: A component architecture for Python

Subclassing

A subclass can be derived from one or more superclasses

Inherits all of their functionality

Unless explicitly overridden

Weakness: A new class for every change in functionality

Common in Zope 2

Timo Stollenwerk A Component Architecture for Python

Page 5: ZCA: A component architecture for Python

Delegation

Instead of inheriting functionality from a superclass

Delegate the work among several separate objects calledcomponents

Each component takes responsibility in a complex action

Example: Model-View-Controller

When a component is no longer satisfactory, it can be replacedby a better implementation

Timo Stollenwerk A Component Architecture for Python

Page 6: ZCA: A component architecture for Python

A Python Framework for component based design

The Zope Component Architecture (ZCA):

A Python framework for supporting component based designand programming

Well suited to developing large Python software systems

Not speci�c to the Zope web application server

"Python Component Architecture"

Timo Stollenwerk A Component Architecture for Python

Page 7: ZCA: A component architecture for Python

A Python Framework for component based design

There are two core packages related to the ZCA:

zope.interface is used to de�ne the interface of a component

zope.component deals with registration and retrieval ofcomponents

Timo Stollenwerk A Component Architecture for Python

Page 8: ZCA: A component architecture for Python

Interfaces

Gang of Four: "Program to an interface, not an implementation"

Legal contract

Vendor has to ful�ll its promises

Customer uses the contract as a guarantee

Both know, from the contract, what is expected from thetransaction

Interfaces can server as API documentation

Python has no build-in support for interfaces

Timo Stollenwerk A Component Architecture for Python

Page 9: ZCA: A component architecture for Python

Installation

$ e a s y_ i n s t a l l zope . i n t e r f a c e$ e a s y_ i n s t a l l zope . component

Timo Stollenwerk A Component Architecture for Python

Page 10: ZCA: A component architecture for Python

Interface Example

>>> from zope . i n t e r f a c e impor t ∗>>> c l a s s IQuacksLikeADuck ( I n t e r f a c e ) :. . . d e f quack ( ) :. . . """ Retu rns a quack ing sound . """. . .>>> c l a s s P l a t ypu s ( o b j e c t ) :. . . imp lements ( IQuacksLikeADuck ). . .. . . d e f quack ( s e l f ) :. . . r e t u r n "Quack !". . .>>> ben = P la t ypu s ( )>>> IQuacksLikeADuck . p rov idedBy ( ben )True>>> p r i n t ben . quack ( )Quack !

Timo Stollenwerk A Component Architecture for Python

Page 11: ZCA: A component architecture for Python

The Standard Python Way

>>> i f h a s a t t r ( ben , ' quack ' ) :. . . ben . quack ( )

Zope 2 is littered with skeletons like if getattr(obj,'_isPrincipiaFolderish', 0)

Where does that get documented?

Who knows it's there?

Who knows what its values may be.

Is it callable?

Interfaces allow one to formalize duck typing, without reallyadding too much weight to their program.

Timo Stollenwerk A Component Architecture for Python

Page 12: ZCA: A component architecture for Python

Adaption

>>> c l a s s Hunter ( o b j e c t ) :. . . d e f __init__( s e l f , name ) :. . . s e l f . name = name. . .>>> tom = Hunter ( 'Tom' )>>> IQuacksLikeADuck . p rov idedBy ( tom)Fa l s e

Timo Stollenwerk A Component Architecture for Python

Page 13: ZCA: A component architecture for Python

A DuckCall Adapter

>>> from zope . component impor t adapt s>>> c l a s s DuckCa l l ( o b j e c t ) :. . . imp lements ( IQuacksLikeADuck ). . . adapt s ( Hunter ). . .. . . d e f __init__( s e l f , hun t e r ) :. . . # Adapte r s a r e pas sed i n t h e i r. . . # adaptee as f i r s t argument. . . s e l f . hun t e r = hunte r. . .. . . d e f quack ( s e l f ) :. . . r e t u r n s e l f . hun t e r . name + ' quacks w i th a duck c a l l '. . .>>> zope . component . p r o v i d eAdap t e r ( DuckCa l l )

Timo Stollenwerk A Component Architecture for Python

Page 14: ZCA: A component architecture for Python

A Tickler

>>> de f t i c k l e r (∗ a r g s ) :. . . """ Goes th rough the p r o v i d ed arguments and

t r i e s to make them quack """. . . f o r p o t e n t i a lQua c k e r i n a r g s :. . . quacke r = IQuacksLikeADuck ( po t en t i a lQuack e r , \. . . None ). . . i f quacke r i s None :. . . p r i n t "Could not quack : %r " % po t e n t i a lQua c k e r. . . e l s e :. . . p r i n t quacke r . quack ( )

Timo Stollenwerk A Component Architecture for Python

Page 15: ZCA: A component architecture for Python

A Tickler (2)

>>> t i c k l e r ( ben , tom , s qu e e k e r s )Quack !Tom quacks w i th a duck c a l lCould not quack : <__main__. I r i s hWo l f h ound o b j e c t a t 0x698bd0>

Timo Stollenwerk A Component Architecture for Python

Page 16: ZCA: A component architecture for Python

Summary

This was a very simple example with Interfaces and Adapters

But it's a powerful building block

Everything �ows through interfaces and adapter registries

There is more: content components, multiadapters, utilities, ...

Timo Stollenwerk A Component Architecture for Python

Page 17: ZCA: A component architecture for Python

Further Information

A Comprehensive Guide to Zope Component Architecture(Baiju M)

Web Component Development with Zope 3 (P. vonWeitershausen)

Zope 3 Wiki (http://wiki.zope.org/zope3)

Timo Stollenwerk A Component Architecture for Python

Page 18: ZCA: A component architecture for Python

The End

The End

Timo Stollenwerk A Component Architecture for Python