亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? sect14.htm

?? Pythn design pattern
?? HTM
?? 第 1 頁 / 共 5 頁
字號:
<B>sumValue(&#160;)</B> also takes an <B>Iterator </B>to perform operations on
every object in the <B>ArrayList</B>.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_343">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It looks silly to upcast the types of
<B>Trash</B> into a container holding base type references, and then turn around
and downcast. Why not just put the trash into the appropriate receptacle in the
first place? (Indeed, this is the whole enigma of recycling). In this program it
would be easy to repair, but sometimes a system&#146;s structure and
flexibility can benefit greatly from downcasting.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_344">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The program satisfies the design
requirements: it works. This might be fine as long as it&#146;s a one-shot
solution. However, a useful program tends to evolve over time, so you must ask,
&#147;What if the situation changes?&#148; For example, cardboard is now a
valuable recyclable commodity, so how will that be integrated into the system
(especially if the program is large and complicated). Since the above
<A NAME="Index39"></A>type-check coding in the <B>switch</B> statement could be
scattered throughout the program, you must go find all that code every time a
new type is added, and if you miss one the compiler won&#146;t give you any
help by pointing out an error.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_345">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The key to the
<A NAME="Index40"></A>misuse of RTTI here is that <I>every type is tested</I>.
If you&#146;re looking for only a subset of types because that subset needs
special treatment, that&#146;s probably fine. But if you&#146;re hunting for
every type inside a switch statement, then you&#146;re probably missing an
important point, and definitely making your code less maintainable. In the next
section we&#146;ll look at how this program evolved over several stages to
become much more flexible. This should prove a valuable example in program
design.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_346">Add Comment</A></FONT><A NAME="_Toc375545414"></A><A NAME="_Toc476705920"></A><A NAME="_Toc534420133"></A><BR></P></DIV>
<A NAME="Heading84"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
Improving the design</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The solutions in <I>Design Patterns</I>
are organized around the question &#147;What will change as this program
evolves?&#148; This is usually the most important question that you can ask
about any design. If you can build your system around the answer, the results
will be two-pronged: not only will your system allow easy (and inexpensive)
maintenance, but you might also produce components that are reusable, so that
other systems can be built more cheaply. This is the promise of object-oriented
programming, but it doesn&#146;t happen automatically; it requires thought and
insight on your part. In this section we&#146;ll see how this process can
happen during the refinement of a system.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_347">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The answer to the question &#147;What
will change?&#148; for the recycling system is a common one: more types will be
added to the system. The goal of the design, then, is to make this addition of
types as painless as possible. In the recycling program, we&#146;d like to
encapsulate all places where specific type information is mentioned, so (if for
no other reason) any changes can be localized to those encapsulations. It turns
out that this process also cleans up the rest of the code considerably.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_348">Add Comment</A></FONT><A NAME="_Toc375545415"></A><A NAME="_Toc476705921"></A><A NAME="_Toc534420134"></A><BR></P></DIV>
<A NAME="Heading85"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H3 ALIGN="LEFT">
&#147;Make more objects&#148;</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This brings up a general object-oriented
design principle that I first heard spoken by <A NAME="Index41"></A>Grady Booch:
&#147;If the design is too complicated, make more objects.&#148; This is
simultaneously counterintuitive and ludicrously simple, and yet it&#146;s the
most useful guideline I&#146;ve found. (You might observe that &#147;making
more objects&#148; is often equivalent to &#147;add another level of
indirection.&#148;) In general, if you find a place with messy code, consider
what sort of class would clean that up. Often the side effect of cleaning up the
code will be a system that has better structure and is more flexible.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_349">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Consider first the place where
<B>Trash</B> objects are created, which is a <B>switch</B> statement inside
<B>main(&#160;)</B>:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_350">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>    <font color=#0000ff>for</font>(int i = 0 i &lt; 30 i++)
      switch((int)(Math.random() * 3)):
        case 0 :
          bin.add(new
            Aluminum(Math.random() * 100))
          <font color=#0000ff>break</font>
        case 1 :
          bin.add(new
            Paper(Math.random() * 100))
          <font color=#0000ff>break</font>
        case 2 :
          bin.add(new
            Glass(Math.random() * 100))
      </PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This is definitely messy, and
also a place where you must change code whenever a new type is added. If new
types are commonly added, a better solution is a single method that takes all of
the necessary information and produces a reference to an object of the correct
type, already upcast to a trash object. In <I>Design Patterns</I> this is
broadly referred to as a
<A NAME="Index42"></A><A NAME="Index43"></A><I>creational pattern</I> (of which
there are several). The specific pattern that will be applied here is a variant
of the <A NAME="Index44"></A><A NAME="Index45"></A><I>Factory Method</I>. Here,
the factory method is a <B>static</B> member of <B>Trash</B>, but more commonly
it is a method that is overridden in the derived class.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_351">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The idea of the factory method is that
you pass it the essential information it needs to know to create your object,
then stand back and wait for the reference (already upcast to the base type) to
pop out as the return value. From then on, you treat the object polymorphically.
Thus, you never even need to know the exact type of object that&#146;s created.
In fact, the factory method hides it from you to prevent accidental misuse. If
you want to use the object without polymorphism, you must explicitly use RTTI
and casting.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_352">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But there&#146;s a little problem,
especially when you use the more complicated approach (not shown here) of making
the factory method in the base class and overriding it in the derived classes.
What if the information required in the derived class requires more or different
arguments? &#147;Creating more objects&#148; solves this problem. To implement
the factory method, the <B>Trash</B> class gets a new method called
<B>factory</B>. To hide the creational data, there&#146;s a new class called
<B>Messenger</B> that carries all of the necessary information for the
<B>factory</B> method to create the appropriate <B>Trash</B> object (we&#146;ve
started referring to <I>Messenger</I> as a design pattern, but it&#146;s simple
enough that you may not choose to elevate it to that status). Here&#146;s a
simple implementation of <B>Messenger</B>:
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_353">Add Comment</A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>class</font> Messenger:
  int type
  # Must change this to add another type:
  static final int MAX_NUM = 4
  double data
  <font color=#0000ff>def</font> __init__(self, int typeNum, double val):
    type = typeNum % MAX_NUM
    data = val
</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A <B>Messenger</B> object&#146;s
only job is to hold information for the <B>factory(&#160;)</B> method. Now, if
there&#146;s a situation in which <B>factory(&#160;)</B> needs more or
different information to create a new type of <B>Trash</B> object, the
<B>factory(&#160;)</B> interface doesn&#146;t need to be changed. The
<B>Messenger</B> class can be changed by adding new data and new constructors,
or in the more typical object-oriented fashion of subclassing.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_354">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>factory(&#160;)</B> method for
this simple example looks like this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>  static Trash factory(Messenger i):
    switch(i.type):
      default: # To quiet the compiler
      case 0:
        <font color=#0000ff>return</font> Aluminum(i.data)
      case 1:
        <font color=#0000ff>return</font> Paper(i.data)
      case 2:
        <font color=#0000ff>return</font> Glass(i.data)
      # Two lines here:
      case 3: 
        <font color=#0000ff>return</font> Cardboard(i.data)
</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here, the determination of the
exact type of object is simple, but you can imagine a more complicated system in
which <B>factory(&#160;)</B> uses an elaborate algorithm. The point is that
it&#146;s now hidden away in one place, and you know to come to this place when
you add new types.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_355">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The creation of new objects is now much
simpler in <B>main(&#160;)</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE>    <font color=#0000ff>for</font>(int i = 0 i &lt; 30 i++)
      bin.add(
        Trash.factory(
          Messenger(
            (int)(Math.random() * Messenger.MAX_NUM),
            Math.random() * 100)))</PRE></FONT></BLOCKQUOTE><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A
<B>Messenger</B> object is created to pass the data into <B>factory(&#160;)</B>,
which in turn produces some kind of <B>Trash</B> object on the heap and returns
the reference that&#146;s added to the <B>ArrayList</B> <B>bin</B>. Of course,
if you change the quantity and type of argument, this statement will still need
to be modified, but that can be eliminated if the creation of the
<B>Messenger</B> object is automated. For example, an <B>ArrayList</B> of
arguments can be passed into the constructor of a <B>Messenger</B> object (or
directly into a <B>factory(&#160;)</B> call, for that matter). This requires
that the arguments be parsed and checked at run time, but it does provide the
greatest flexibility.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_356">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can see from this code what
&#147;<A NAME="Index46"></A><A NAME="Index47"></A><A NAME="Index48"></A>vector
of change&#148; problem the factory is responsible for solving: if you add new
types to the system (the change), the only code that must be modified is within
the factory, so the factory isolates the effect of that change.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_357">Add Comment</A></FONT><A NAME="_Toc375545418"></A><A NAME="_Toc476705922"></A><A NAME="_Toc534420135"></A><BR></P></DIV>
<A NAME="Heading86"></A><FONT FACE = "Verdana, Tahoma, Arial, Helvetica, Sans"><H2 ALIGN="LEFT">
A pattern for prototyping creation</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A problem with the design above is that
it still requires a central location where all the types of the objects must be
known: inside the <B>factory(&#160;)</B> method. If new types are regularly
being added to the system, the <B>factory(&#160;)</B> method must be changed for
each new type. When you discover something like this, it is useful to try to go
one step further and move <I>all</I> of the information about the
type&#151;including its creation&#151;into the class representing that type.
This way, the only thing you need to do to add a new type to the system is to
inherit a single class.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_358">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To move the information concerning type
creation into each specific type of <B>Trash</B>,<B> </B>the
&#147;<A NAME="Index49"></A><A NAME="Index50"></A>prototype&#148; pattern
(from the <I>Design Patterns </I>book) will be used. The general idea is that
you have a master sequence of objects, one of each type you&#146;re interested
in making. The objects in this sequence are used <I>only</I> for making new
objects, using an operation that&#146;s not unlike the
<A NAME="Index51"></A><B>clone(&#160;)</B> scheme built into Java&#146;s root
class <B>Object</B>. In this case, we&#146;ll name the cloning method
<B>tClone(&#160;)</B>.<B> </B>When you&#146;re ready to make a new object,
presumably you have some sort of information that establishes the type of object
you want to create, then you move through the master sequence comparing your
information with whatever appropriate information is in the prototype objects in
the master sequence. When you find one that matches your needs, you clone it.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_359">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In this scheme there is no hard-coded
information for creation. Each object knows how to expose appropriate
information and how to clone itself. Thus, the <B>factory(&#160;)</B> method
doesn&#146;t need to be changed when a new type is added to the system.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_360">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One approach to the problem of
prototyping is to add a number of methods to support the creation of new
objects. However, in Java 1.1 there&#146;s already support for creating new
objects if you have a reference to the <B>Class</B> object. With
<A NAME="Index52"></A><A NAME="Index53"></A>Java 1.1 <I>reflection</I>
(introduced in Chapter 12 of <I>Thinking in Java, 2<SUP>nd</SUP> edition</I>)
you can call a constructor even if you have only a reference to the <B>Class</B>
object. This is the perfect solution for the prototyping problem.
<A HREF="http://www.mindview.net/Books/TIPython/BackTalk/FindPage/A_361">Add Comment</A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The list of prototypes will be
represented indirectly by a list of references to all the <B>Class</B> objects

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产91精品一区二区麻豆网站 | 免费在线观看日韩欧美| 亚洲国产三级在线| 日本欧美大码aⅴ在线播放| 亚洲午夜久久久久久久久电影网 | 亚洲bt欧美bt精品| 日韩精品一二区| 极品尤物av久久免费看| 国模娜娜一区二区三区| 高清国产一区二区三区| 99久久99久久久精品齐齐| 色噜噜狠狠成人中文综合| 亚洲最大的成人av| 丝袜美腿成人在线| 国产一区二区0| 成人丝袜18视频在线观看| 色国产综合视频| 一区二区激情小说| 国产尤物一区二区| 欧美色中文字幕| 国产精品人成在线观看免费| 亚洲无线码一区二区三区| 麻豆精品久久精品色综合| 91小视频免费观看| 久久婷婷国产综合精品青草| 亚洲第一综合色| 成人av在线影院| 日韩一区二区电影在线| 中文字幕一区二区在线观看| 狠狠色狠狠色综合| 91精品黄色片免费大全| 一区二区三区在线不卡| 首页国产欧美久久| 91免费版在线| 欧美国产视频在线| 激情综合五月婷婷| 91精品国产综合久久久蜜臀图片| 国产精品免费免费| 国产成人综合在线观看| 日韩精品一区二区在线观看| 亚洲欧美日本在线| 9i在线看片成人免费| 国产精品污污网站在线观看 | 日本一区中文字幕| 欧美午夜片在线观看| 亚洲日本在线天堂| 91视视频在线直接观看在线看网页在线看 | 日韩二区三区在线观看| 欧美中文字幕一二三区视频| 亚洲青青青在线视频| 一本色道**综合亚洲精品蜜桃冫| 国产精品欧美一区二区三区| 国产激情一区二区三区桃花岛亚洲 | 欧美日韩国产系列| 久久精品国内一区二区三区| 日韩欧美成人一区二区| 美女视频一区在线观看| 日韩一区二区免费在线观看| 精品一区二区三区在线观看| 久久新电视剧免费观看| 成人18精品视频| 亚洲一区二区av在线| 欧美军同video69gay| 久久国产剧场电影| 日本一区免费视频| 91久久精品一区二区二区| 一区精品在线播放| 69堂国产成人免费视频| 国内不卡的二区三区中文字幕| 精品美女在线播放| 色94色欧美sute亚洲13| 九九精品视频在线看| 一区二区三区中文在线| 精品动漫一区二区三区在线观看| 99久久99久久免费精品蜜臀| 亚洲午夜精品久久久久久久久| 欧美一区二区大片| 97精品视频在线观看自产线路二| 丝袜亚洲另类丝袜在线| 国产精品福利一区| 日韩午夜小视频| 91老师片黄在线观看| 高清不卡在线观看av| 一区二区三区免费在线观看| 国产午夜亚洲精品理论片色戒| 欧美麻豆精品久久久久久| 成人美女视频在线看| 国产一区二区免费看| 亚洲国产aⅴ成人精品无吗| 亚洲柠檬福利资源导航| 亚洲一区二区三区中文字幕 | 精品一区二区三区日韩| 一区二区三区在线免费播放| 亚洲天堂福利av| 一区精品在线播放| 日韩一卡二卡三卡四卡| 欧美色男人天堂| 日本道色综合久久| 99re这里都是精品| 色偷偷久久一区二区三区| 成人午夜视频免费看| 激情欧美一区二区| 丁香桃色午夜亚洲一区二区三区| 视频在线观看91| 亚洲乱码精品一二三四区日韩在线| 精品成人一区二区三区四区| 欧美一级夜夜爽| 欧美日韩一二三区| 91精品国产综合久久久蜜臀图片| 欧美日韩一二三| 精品欧美黑人一区二区三区| 国产调教视频一区| 成人免费在线播放视频| 亚洲女同ⅹxx女同tv| 亚洲一区二区三区四区在线免费观看| 亚洲精品日韩综合观看成人91| 一区二区国产视频| 久久国产精品无码网站| 色综合天天视频在线观看| 国产亚洲午夜高清国产拍精品| 一区二区三区在线视频播放| 精品在线观看视频| 在线观看日韩电影| 国产精品美女久久久久aⅴ| 丝袜亚洲精品中文字幕一区| 成人av电影在线观看| 日韩欧美高清在线| 日韩精品欧美成人高清一区二区| 99精品热视频| 国产精品入口麻豆九色| 精品夜夜嗨av一区二区三区| 欧美三级中文字幕| 亚洲三级在线观看| youjizz国产精品| 日本一区二区成人在线| 国产综合色在线| 欧美成人欧美edvon| 免费观看在线综合色| 欧美性猛片xxxx免费看久爱| 一区二区三区自拍| 91在线播放网址| 亚洲美女电影在线| 成人福利视频在线| 中文字幕亚洲成人| 色婷婷综合久色| 亚洲图片欧美视频| 欧美日韩dvd在线观看| 天堂精品中文字幕在线| 91精品久久久久久蜜臀| 天堂久久久久va久久久久| 欧美高清视频www夜色资源网| 亚洲国产wwwccc36天堂| 91麻豆精品国产91久久久久久 | 国内外成人在线| 欧美国产欧美亚州国产日韩mv天天看完整| 国产一区二区三区黄视频 | 日本高清不卡视频| 日韩精品1区2区3区| 久久影院视频免费| 91视视频在线直接观看在线看网页在线看 | 久久久久久久久岛国免费| 成人午夜在线播放| 亚洲国产毛片aaaaa无费看| 欧美成人欧美edvon| 91网站在线观看视频| 青青草原综合久久大伊人精品优势| 精品三级av在线| 日本道色综合久久| 国产一区二区三区视频在线播放| 亚洲色图在线播放| 26uuu国产日韩综合| 欧洲国产伦久久久久久久| 韩国精品在线观看| 亚洲国产日韩a在线播放| 国产亚洲欧美激情| 欧美精选午夜久久久乱码6080| 经典三级视频一区| 亚洲高清三级视频| 欧美激情综合在线| 欧美少妇bbb| 色哟哟一区二区三区| 国产高清不卡二三区| 美日韩一区二区| 亚洲综合在线观看视频| 国产精品免费视频一区| 精品国产百合女同互慰| 欧美一级黄色大片| 国产精品色在线观看| 日韩一区二区中文字幕| 欧美三区在线观看| 日本高清不卡视频| 91色porny| 色老汉一区二区三区| 99精品在线免费| 成人av资源在线观看| 成人蜜臀av电影| 91蜜桃婷婷狠狠久久综合9色| 国产ts人妖一区二区| 国产美女娇喘av呻吟久久| 激情六月婷婷综合|