📄 tij0174.html
字号:
<font color="#009900">// Singleton s3 = (Singleton)s2.clone();</font>
} <font color="#0000ff">catch</font>(Exception e) {}
}
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
key to creating a singleton is to prevent the client programmer from having any
way to create an object except the ways you provide. You must make all <A NAME="Index2925"></A><A NAME="Index2926"></A>constructors
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>private</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and you must
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">create
at least one constructor to prevent the compiler from <A NAME="Index2927"></A><A NAME="Index2928"></A>synthesizing
a default constructor for you (which it will create as “friendly”).
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">At
this point, you decide how you’re going to create your object. Here,
it’s created statically, but you can also wait until the client
programmer asks for one and create it on demand. In any case, the object should
be stored privately. You provide access through public methods. Here,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getHandle( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
produces the handle to the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Singleton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. The rest of the interface (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>getValue( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>setValue( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
is the regular class interface.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java
also allows the creation of objects through cloning. In this example, making
the class
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>final</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
prevents cloning. Since
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Singleton</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is inherited directly from
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method remains
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>protected</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so it cannot be used (doing so produces a compile-time error). However, if
you’re inheriting from a class hierarchy that has already overridden
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>public</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and implemented
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Cloneable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the way to prevent cloning is to override
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and throw a
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>CloneNotSupportedException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as described in Chapter 12. (You could also override
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>clone( )</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and simply return
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>this</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
but that would be deceiving since the client programmer would think they were
cloning the object, but would instead still be dealing with the original.)
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Note
that you aren’t restricted to creating only one object. This is also a
technique to create a limited pool of objects. In that situation, however, you
can be confronted with the problem of sharing objects in the pool. If this is
an issue, you can create a solution involving a check-out and check-in of the
shared objects.
</FONT><a name="_Toc408018796"></a><P></DIV>
<A NAME="Heading554"></A><H3 ALIGN=LEFT>
Classifying
patterns
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Design
Patterns
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
book discusses 23 different patterns, classified under three purposes (all of
which revolve around the particular aspect that can vary). The three purposes
are:
</FONT><P></DIV><DIV ALIGN=LEFT><A NAME="Index2929"></A><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> <A NAME="Index2930"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Creational</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
how an object can be created. This often involves isolating the details of
object creation so your code isn’t dependent on what types of objects
there are and thus doesn’t have to be changed when you add a new type of
object. The aforementioned
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Singleton</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is classified as a creational pattern, and later in this chapter you’ll
see examples of
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Factory
Method
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Prototype</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><LI><A NAME="Index2931"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> <A NAME="Index2932"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Structural</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
designing objects to satisfy particular project constraints. These work with
the way objects are connected with other objects to ensure that changes in the
system don’t require changes to those connections.
</FONT><LI><A NAME="Index2933"></A><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"> <A NAME="Index2934"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Behavioral</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
objects that handle particular types of actions within a program. These
encapsulate processes that you want to perform, such as interpreting a
language, fulfilling a request, moving through a sequence (as in an iterator),
or implementing an algorithm. This chapter contains examples of the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Observer</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Visitor</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
patterns.
</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Design
Patterns
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
book has a section on each of its 23 patterns along with one or more examples
for each, typically in C++ but sometimes in Smalltalk. (You’ll find that
this doesn’t matter too much since you can easily translate the concepts
from either language into Java.) This book will not repeat all the patterns
shown in
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>Design
Patterns
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
since that book stands on its own and should be studied separately. Instead,
this chapter will give some examples that should provide you with a decent feel
for what patterns are about and why they are so important.
</FONT><a name="_Toc408018797"></a><a name="_Toc375545413"></a><P></DIV>
<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0173.html">Prev</a> | <a href="tij0175.html">Next</a>
</div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -