?? sect05.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIPython.rtf
Application Directory:c:\tools\rtf2html\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:12/31/2001
Translation Time:08:24:12
Translation Platform:Win32
Number of Output files:18
This File:Sect05.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>4:Fronting for an implementation</TITLE>
<script language="JavaScript">
</script>
</head>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans" size = "-1">
<!-- [ <a href="README.txt">Viewing Hints</a> ]
[ <a href="RevisionHistory.htm">Revision History</a> ] -->
[ <a href="http://www.mindview.net/Books/TIPython/">Book Home Page</a> ]
[ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
[ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
[ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
[ <a href="http://www.mindview.net/Services">Consulting</a> ]
</FONT>
<H2><FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans">
Thinking in Python<br>
<small>Revision 0.1.2 (12/31/01) -- Incomplete and Unfinished</small></FONT></H2>
<H3><FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans">
by Bruce Eckel ©2002 MindView, Inc.</FONT></H3>
<FONT FACE="Verdana, Tahoma, Arial, Helvetica, Sans" size = "-1">
[ <a href="Sect04.htm">Previous Chapter</a> ]
[ <a href="javascript:window.location.href = 'Index.htm';">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Sect06.htm">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><A NAME="_Toc476705898"></A><A NAME="_Toc534420083"></A><A NAME="Heading34"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H1 ALIGN="LEFT">
4:Fronting for an implementation</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Both <I>Proxy</I> and <I>State</I>
provide a surrogate class that you use in your code; the real class that does
the work is hidden behind this surrogate class. When you call a method in the
surrogate, it simply turns around and calls the method in the implementing
class. These two patterns are so similar that the <I>Proxy</I> is simply a
special case of <I>State</I>. One is tempted to just lump the two together into
a pattern called <I>Surrogate</I>, but the term “proxy” has a
long-standing and specialized meaning, which probably explains the reason for
the two different patterns.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_126">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The basic idea is simple: from a base
class, the surrogate is derived along with the class or classes that provide the
actual implementation:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_127">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="CENTER"><FONT FACE="Georgia"><IMG SRC="TIPyth00.gif"></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When a surrogate object is created, it is
given an implementation to which to send all of the method calls.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_128">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Structurally, the difference between
<I>Proxy</I> and <I>State</I> is simple: a <I>Proxy</I> has only one
implementation, while <I>State</I> has more than one. The application of the
patterns is considered (in <I>Design Patterns</I>) to be distinct: <I>Proxy</I>
is used to control access to its implementation, while <I>State</I> allows you
to change the implementation dynamically. However, if you expand your notion of
“controlling access to implementation” then the two fit neatly
together.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_129">Add Comment</A></FONT><A NAME="_Toc476705899"></A><A NAME="_Toc534420084"></A><BR></P></DIV>
<A NAME="Heading35"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
Proxy</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If we implement <I>Proxy</I> by following
the above diagram, it looks like this:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_130">Add Comment</A></FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:ProxyDemo.py
# Simple demonstration of the Proxy pattern.
<font color=#0000ff>class</font> Implementation:
<font color=#0000ff>def</font> f(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.f()"</font>
<font color=#0000ff>def</font> g(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.g()"</font>
<font color=#0000ff>def</font> h(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.h()"</font>
<font color=#0000ff>class</font> Proxy:
<font color=#0000ff>def</font> __init__(self):
self.__implementation = Implementation()
# Pass method calls to the implementation:
<font color=#0000ff>def</font> f(self): self.__implementation.f()
<font color=#0000ff>def</font> g(self): self.__implementation.g()
<font color=#0000ff>def</font> h(self): self.__implementation.h()
p = Proxy()
p.f(); p.g(); p.h()
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It isn’t necessary that
<B>Implementation</B> have the same interface as <B>Proxy</B>; as long as
<B>Proxy</B> is somehow “speaking for” the class that it is
referring method calls to then the basic idea is satisfied (note that this
statement is at odds with the definition for Proxy in GoF). However, it is
convenient to have a common interface so that <B>Implementation</B> is forced to
fulfill all the methods that <B>Proxy</B> needs to call.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_131">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Of course, in Python we have a delegation
mechanism built in, so it makes the <B>Proxy</B> even simpler to implement:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_132">Add Comment</A></FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:ProxyDemo2.py
# Simple demonstration of the Proxy pattern.
<font color=#0000ff>class</font> Implementation2:
<font color=#0000ff>def</font> f(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.f()"</font>
<font color=#0000ff>def</font> g(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.g()"</font>
<font color=#0000ff>def</font> h(self):
<font color=#0000ff>print</font> <font color=#004488>"Implementation.h()"</font>
<font color=#0000ff>class</font> Proxy2:
<font color=#0000ff>def</font> __init__(self):
self.__implementation = Implementation2()
<font color=#0000ff>def</font> __getattr__(self, name):
<font color=#0000ff>return</font> getattr(self.__implementation, name)
p = Proxy2()
p.f(); p.g(); p.h();
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The beauty of using
<B>__getattr__( )</B> is that <B>Proxy2</B> is completely generic, and not
tied to any particular implementation (in Java, a rather complicated
“dynamic proxy” has been invented to accomplish this same thing).
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_133">Add Comment</A></FONT><A NAME="_Toc476705900"></A><A NAME="_Toc534420085"></A><BR></P></DIV>
<A NAME="Heading36"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
State</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <I>State</I> pattern adds more
implementations to <I>Proxy</I>, along with a way to switch from one
implementation to another during the lifetime of the surrogate:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_134">Add Comment</A></FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>#: c04:StateDemo.py
# Simple demonstration of the State pattern.
<font color=#0000ff>class</font> State_d:
<font color=#0000ff>def</font> __init__(self, imp):
self.__implementation = imp
<font color=#0000ff>def</font> changeImp(self, newImp):
self.__implementation = newImp
# Delegate calls to the implementation:
<font color=#0000ff>def</font> __getattr__(self, name):
<font color=#0000ff>return</font> getattr(self.__implementation, name)
<font color=#0000ff>class</font> Implementation1:
<font color=#0000ff>def</font> f(self):
<font color=#0000ff>print</font> <font color=#004488>"Fiddle de dum, Fiddle de dee,"</font>
<font color=#0000ff>def</font> g(self):
<font color=#0000ff>print</font> <font color=#004488>"Eric the half a bee."</font>
<font color=#0000ff>def</font> h(self):
<font color=#0000ff>print</font> <font color=#004488>"Ho ho ho, tee hee hee,"</font>
<font color=#0000ff>class</font> Implementation2:
<font color=#0000ff>def</font> f(self):
<font color=#0000ff>print</font> <font color=#004488>"We're Knights of the Round Table."</font>
<font color=#0000ff>def</font> g(self):
<font color=#0000ff>print</font> <font color=#004488>"We dance whene'er we're able."</font>
<font color=#0000ff>def</font> h(self):
<font color=#0000ff>print</font> <font color=#004488>"We do routines and chorus scenes"</font>
<font color=#0000ff>def</font> run(b):
b.f()
b.g()
b.h()
b.g()
b = State_d(Implementation1())
run(b)
b.changeImp(Implementation2())
run(b)
#:~</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see that the first
implementation is used for a bit, then the second implementation is swapped in
and that is used.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_135">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The difference between <I>Proxy</I> and
<I>State</I> is in the problems that are solved. The common uses for
<I>Proxy</I> as described in <I>Design Patterns</I> are:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_136">Add Comment</A></FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Remote
proxy</B>. This proxies for an object in a different address space. A remote
proxy is created for you automatically by the RMI compiler <B>rmic</B> as it
creates stubs and skeletons.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_137">Add Comment</A></FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Virtual
proxy</B>. This provides “lazy initialization” to create expensive
objects on demand.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_138">Add Comment</A></FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Protection
proxy</B>. Used when you don’t want the client programmer to have full
access to the proxied object.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_139">Add Comment</A></FONT><LI><FONT FACE="Verdana"><B> </B></FONT><FONT FACE="Georgia"><B>Smart
reference</B>. To add additional actions when the proxied object is accessed.
For example, or to keep track of the number of references that are held for a
particular object, in order to implement the <I>copy-on-write</I> idiom and
prevent object aliasing. A simpler example is keeping track of the number of
calls to a particular method.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_140">Add Comment</A></FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You
could look at a Python reference as a kind of protection proxy, since it
controls access to the actual object on the heap (and ensures, for example, that
you don’t use a <B>null </B>reference).
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_141">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[[ Rewrite this: In <I>Design
Patterns</I>, <I>Proxy</I> and <I>State</I> are not seen as related to each
other because the two are given (what I consider arbitrarily) different
structures. <I>State</I>, in particular, uses a separate implementation
hierarchy but this seems to me to be unnecessary unless you have decided that
the implementation is not under your control (certainly a possibility, but if
you own all the code there seems to be no reason not to benefit from the
elegance and helpfulness of the single base class). In addition, <I>Proxy</I>
need not use the same base class for its implementation, as long as the proxy
object is controlling access to the object it “fronting” for.
Regardless of the specifics, in both <I>Proxy </I>and <I>State</I> a surrogate
is passing method calls through to an implementation object.]]]
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_142">Add Comment</A></FONT><A NAME="_Toc476705901"></A><A NAME="_Toc534420086"></A><BR></P></DIV>
<A NAME="Heading37"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
StateMachine</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While <I>State</I> has a way to allow the
client programmer to change the implementation, <I>StateMachine</I> imposes a
structure to automatically change the implementation from one object to the
next. The current implementation represents the state that a system is in, and
the system behaves differently from one state to the next (because it uses
<I>State</I>). Basically, this is a “state machine” using objects.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_143">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The code that moves the system from one
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -