⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 disc3.htm

📁 Design Pattern 设计模式
💻 HTM
字号:
<HTML><HEAD>	<TITLE>Discussion of Creational Patterns</TITLE><SCRIPT>function setFocus() {		if ((navigator.appName != "Netscape") && (parseFloat(navigator.appVersion) == 2)) {	return;	} else {	self.focus();	}}</SCRIPT></HEAD><BODY   TOPMARGIN       = 4        LEFTMARGIN      = 4        BGCOLOR         = #FFFFFFonLoad="setFocus()";><A NAME="top"><A NAME="creational_discussion"></A><A NAME="disc3-1"></A><P>There are two common ways to parameterize a system by the classes ofobjects it creates.  One way is to subclass the class that creates theobjects; this corresponds to using the<A HREF="pat3cfs.htm" TARGET="_mainDisplayFrame">Factory Method (107)</A> pattern.  The main drawback of thisapproach is that it can require creating a new subclass just to changethe class of the product.  Such changes can cascade.  For example,when the product creator is itself created by a factory method, thenyou have to override its creator as well.</P><A NAME="disc3-2"></A><A NAME="protocol-vs-abstrfact"></A><A NAME="build-vs-abstfact"></A><P>The other way to parameterize a system relies more on objectcomposition: Define an object that's responsible for knowing the classof the product objects, and make it a parameter of the system.  Thisis a key aspect of the<A HREF="pat3afs.htm" TARGET="_mainDisplayFrame">Abstract Factory (87)</A>,<A HREF="pat3bfs.htm" TARGET="_mainDisplayFrame">Builder (97)</A>, and<A HREF="pat3dfs.htm" TARGET="_mainDisplayFrame">Prototype (117)</A>patterns.  All three involve creating a new "factory object" whoseresponsibility is to create product objects.  Abstract Factory has thefactory object producing objects of several classes.  Builder has thefactory object building a complex product incrementally using acorrespondingly complex protocol.  Prototype has the factory objectbuilding a product by copying a prototype object.  In this case, thefactory object and the prototype are the same object, because theprototype is responsible for returning the product.</P><A NAME="disc3-3"></A><P>Consider the drawing editor framework described in the Prototypepattern.  There are several ways to parameterize a GraphicTool by theclass of product:</P><UL><A NAME="disc3-4"></A><LI>By applying the Factory Method pattern, a subclass of GraphicTool will becreated for each subclass of Graphic in the palette.  GraphicTool willhave a NewGraphic operation that each GraphicTool subclass willredefine.</LI><P></P><A NAME="disc3-5"></A><LI>By applying the Abstract Factory pattern, there will be a class hierarchyof GraphicsFactories, one for each Graphic subclass.  Each factorycreates just one product in this case: CircleFactory will createCircles, LineFactory will create Lines, and so on.  A GraphicTool willbe parameterized with a factory for creating the appropriate kind ofGraphics.</LI><P></P><A NAME="disc3-6"></A><LI>By applying the Prototype pattern, each subclass of Graphics willimplement the Clone operation, and a GraphicTool will be parameterizedwith a prototype of the Graphic it creates.</LI></UL><A NAME="disc3-7"></A><P>Which pattern is best depends on many factors.  In our drawing editorframework, the Factory Method pattern is easiest to use at first.It's easy to define a new subclass of GraphicTool, and the instancesof GraphicTool are created only when the palette is defined.  The maindisadvantage here is that GraphicTool subclasses proliferate, and noneof them does very much.</P><A NAME="disc3-8"></A><P>Abstract Factory doesn't offer much of an improvement, because itrequires an equally large GraphicsFactory class hierarchy.  AbstractFactory would be preferable to Factory Method only if there werealready a GraphicsFactory class hierarchy&#151;either because thecompiler provides it automatically (as in Smalltalk or Objective C) orbecause it's needed in another part of the system.</P><A NAME="disc3-9"></A><P>Overall, the Prototype pattern is probably the best for the drawingeditor framework, because it only requires implementing a Cloneoperation on each Graphics class.  That reduces the number of classes,and Clone can be used for purposes other than pure instantiation (e.g.,a Duplicate menu operation).</P><A NAME="disc3-10"></A><P>Factory Method makes a design more customizable and only a little morecomplicated.  Other design patterns require new classes, whereasFactory Method only requires a new operation.  People often useFactory Method as the standard way to create objects, but it isn'tnecessary when the class that's instantiated never changes or wheninstantiation takes place in an operation that subclasses can easilyoverride, such as an initialization operation.</P><A NAME="disc3-11"></A><P>Designs that use Abstract Factory, Prototype, or Builder are even moreflexible than those that use Factory Method, but they're also morecomplex.  Often, designs start out using Factory Method and evolvetoward the other creational patterns as the designer discovers wheremore flexibility is needed.  Knowing many design patterns gives youmore choices when trading off one design criterion against another.</P><A NAME="disc3-12"></A><A NAME="last"></A><P><A HREF="#top"><IMG SRC="gifsb/up3.gif" BORDER=0></A><BR><A HREF="chap4fs.htm" TARGET="_mainDisplayFrame"><IMG SRC="gifsb/rightar3.gif"	ALIGN=TOP BORDER=0></A> <A HREF="chap4fs.htm"	TARGET="_mainDisplayFrame">Structural Patterns</A><BR><A HREF="pat3efs.htm" TARGET="_mainDisplayFrame"><IMG SRC="gifsb/leftarr3.gif"	ALIGN=TOP BORDER=0></A> <A HREF="pat3efs.htm"	TARGET="_mainDisplayFrame">Singleton</A></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -