﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-笔记-随笔分类-OO设计</title><link>http://www.blogjava.net/yuxh/category/42994.html</link><description>way</description><language>zh-cn</language><lastBuildDate>Thu, 10 Dec 2009 18:43:40 GMT</lastBuildDate><pubDate>Thu, 10 Dec 2009 18:43:40 GMT</pubDate><ttl>60</ttl><item><title>Beginning JO(2 Collection)</title><link>http://www.blogjava.net/yuxh/archive/2009/12/10/305397.html</link><dc:creator>yuxh</dc:creator><author>yuxh</author><pubDate>Thu, 10 Dec 2009 03:38:00 GMT</pubDate><guid>http://www.blogjava.net/yuxh/archive/2009/12/10/305397.html</guid><wfw:comment>http://www.blogjava.net/yuxh/comments/305397.html</wfw:comment><comments>http://www.blogjava.net/yuxh/archive/2009/12/10/305397.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yuxh/comments/commentRss/305397.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yuxh/services/trackbacks/305397.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;数组可以装基本类型或者引用，collections只能装引用。<br />
&nbsp;&nbsp;&nbsp; 通常有两种方法可以扩展collection 来满足一些需要：继承某种集合类型和封装某种集合类型。第一种的优点是初始化的时候在内存中只产生一个对象，这是继承特性决定的。后者的优点是我们可以方便控制被封装集合的各种属性。<br />
Whenever possible, it&#8217;s desirable to bury implementation details inside of a class rather than exposing client code to such details。例：<br />
法1：<br />
public class Student {<br />
&nbsp;&nbsp;&nbsp;&nbsp;private String name;<br />
&nbsp;&nbsp;&nbsp;&nbsp;private String studentId;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;private ArrayList&lt;TranscriptEntry&gt; transcript; //成绩报告单<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void addTranscriptEntry(TranscriptEntry te) {&nbsp;&nbsp;&nbsp;// 操作transcript达到记录成绩<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Store the TranscriptEntry in our ArrayList.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transcript.add(te);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
客户端调用代码：<br />
Student s = new Student("1234567", "James Huddleston");<br />
Course c = new Course("LANG 800", "Advanced Language Studies");<br />
TranscriptEntry te = new TranscriptEntry(c, "Fall 2006", "B+");<br />
s.addTranscriptEntry(te);<br />
法2：<br />
建立新对象，封装一个ArrayList：<br />
public class Transcript {<br />
&nbsp;&nbsp;&nbsp;&nbsp;private ArrayList&lt;TranscriptEntry&gt; transcriptEntries;<br />
&nbsp;&nbsp;&nbsp;&nbsp;public void courseCompleted(Course c, String semester, String grade) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Instantiate and insert a brand-new TranscriptEntry object into the<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ArrayList - details hidden away!<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;transcriptEntries.add(new TranscriptEntry(c, semester, grade);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
public class Student {<br />
&nbsp;&nbsp;&nbsp;&nbsp;private String name;<br />
&nbsp;&nbsp;&nbsp;&nbsp;private String studentId;<br />
&nbsp;&nbsp;&nbsp;&nbsp;// This used to be declared as an ArrayList.<br />
&nbsp;&nbsp;&nbsp;&nbsp;private Transcript transcript;<br />
&nbsp;&nbsp;&nbsp;&nbsp;// etc.<br />
}<br />
客户端代码：<br />
s.courseCompleted(c, "Spring 2006", "A+");<br />
第二种方法使Student处理更少的细节，不用管transcripts怎么表达，看不到TranscriptEntry的存在。客户端代码更简单。
<img src ="http://www.blogjava.net/yuxh/aggbug/305397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yuxh/" target="_blank">yuxh</a> 2009-12-10 11:38 <a href="http://www.blogjava.net/yuxh/archive/2009/12/10/305397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Beginning Java Objects</title><link>http://www.blogjava.net/yuxh/archive/2009/12/08/305181.html</link><dc:creator>yuxh</dc:creator><author>yuxh</author><pubDate>Tue, 08 Dec 2009 08:57:00 GMT</pubDate><guid>http://www.blogjava.net/yuxh/archive/2009/12/08/305181.html</guid><wfw:comment>http://www.blogjava.net/yuxh/comments/305181.html</wfw:comment><comments>http://www.blogjava.net/yuxh/archive/2009/12/08/305181.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/yuxh/comments/commentRss/305181.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/yuxh/services/trackbacks/305181.html</trackback:ping><description><![CDATA[<strong>Aggregation</strong> is a special form of association, alternatively referred to as the &#8220;consists of&#8221;, &#8220;is composed of&#8221;, or &#8220;has a&#8221; relationship.Like an association, an aggregation is used to represent a relationship between two classes, A and B. But, with an aggregation, we&#8217;re representing more than mere relationship: we&#8217;re stating that an object belonging to class A, known as an <strong>aggregate</strong>,is composed of, or contains, <strong>component objects</strong> belonging to class B.<br />
&nbsp; Note that these aggregation statements appear very similar to associations, where the name of the association just so happens to be is composed of or contains. That&#8217;s because an aggregation is an association in the broad sense of the term（aggregation 是association的一种特殊表现形式）!aggregation&nbsp;和associations UML表现不同但最终代码表现形式一样<br />
&nbsp; <strong>Composition</strong> is a strong form of aggregation, in which the &#8220;parts&#8221; cannot exist without the &#8220;whole.&#8221; As an example, given the relationship &#8220;a Book is composed of many Chapters&#8221;, we could argue that a chapter cannot exist if the book to which it belongs ceases to exist; whereas given the relationship &#8220;a Car is composed of many Wheels&#8221;, we know that a wheel can be removed from a car and still serve a useful purpose. Thus, we&#8217;d categorize the Book&#8211;Chapter relationship as composition and the Car&#8211;Wheel relationship as aggregation.<br />
继承没留意的好处：<br />
&#8226; <strong>Best of all, we can derive a new class from an existing class even if we don&#8217;t own the source code for the latter</strong>! As long as we have the compiled bytecode version of a class, the inheritance mechanism works just fine; we don&#8217;t need the original source code of a class in order to extend it. <strong>This is one of the most dramatic ways to achieve productivity with an objectoriented language</strong>: find a class (either one written by someone else or one that is built into the language) that does much of what you need, and create a subclass of that class,adding just those features that you need for your own purposes.<br />
&#8226; <strong>classification is the natural way that humans organize information</strong>; so, it only makes sense that we&#8217;d organize software along the same lines, making it much more intuitive and hence easier to develop, maintain,extend, and communicate with users about.<br />
继承与Association, aggregation异同（P186）：<br />
Association, aggregation, and inheritance are all said to be relationships between classes. Where inheritance differs from association and aggregation is at the <strong>object</strong> level.inheritance is indeed a relationship between <strong>classes</strong>, but <strong>not</strong> between distinct <strong>objects</strong>.<br />
<span style="color: red">注意</span>：避免连锁反应，<em>Whenever possible, avoid adding features to non-leaf classes once they have been established in code form in an application, to avoid ripple effects throughout an inheritance hierarchy</em>. 说比做容易，这就要求在编码之前尽可多的花时间在需求分析和对象建模阶段，虽然不能避免新需求出现，但至少避免忽视遗漏了当前的需求。 <br />
&nbsp;&nbsp;&nbsp;&nbsp; Overriding:子类继承父类，重写唯一能改变的是方法的访问控制，而且只能比父类更宽松，如父类用的是private，子类可以用public。参考了下thinking in java 4th P202 发现这种说法不对，而且是一个<span style="color: red">陷阱</span>：父类的该方法根本对子类不可见！子类的该方法实际上是一个全新的方法，连重载都算不上。所以只能重写non-private方法。遇到private方法你得小心，没有编译错误，但不会像你想象的工作，最好给方法重新取名，避免陷阱。<br />
<br />
不要做的事情：<br />
<strong>We shouldn&#8217;t change the semantics—that is, the intention, or meaning—of a feature</strong>.For example:<br />
&#8226; If the print method of a superclass such as Student is intended to display the values of all of an object&#8217;s attributes in the command window, then the print method of a subclass such as GraduateStudent shouldn&#8217;t, for example, be overridden so that it directs all of its output to a file instead.<br />
&#8226; If the name attribute of a superclass such as Person is intended to store a person&#8217;s name in &#8220;last name, first name&#8221; order, then the name attribute of a subclass such as Student should be used in the same fashion.<br />
&nbsp; We can&#8217;t physically eliminate features, nor should we effectively eliminate them by ignoring them. To attempt to do so would break the spirit of the &#8220;is a&#8221; hierarchy. By definition, inheritance requires that all features of all ancestors of a class A must also apply to class A itself in order for A to truly be a proper subclass. If a GraduateStudent could eliminate the degreeSought attribute&nbsp;that it inherits from Student, for example, is a GraduateStudent really a Student after all? Strictly speaking, the answer is no.<br />
&nbsp; 进一步从实用角度说，如果我们重写一个方法但不在这方法里做任何事情，其他继承我们类的人就会出问题：他们觉得我们的方法是有意义的（特别是他们不能看到我们源代码的时候）。而我们则打破了&#8220;is a&#8221; 原则，所以绝不要这样做！<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected关键字的运用，用于控制继承的访问控制。<br />
&nbsp;&nbsp;&nbsp;&nbsp; 运用super(arguments) 减少子类构造函数重复父类构造函数代码，和this类似必须在构造函数最开始调用。<br />
Student s = new Student("Fred", "123-45-6789"); 执行这段代码，Object的构造函数会自动执行，接着Student 的父类Person构造函数仔细，然后是我们调用的Student构造函数，如果调用的Student构造函数没有显示调用父类构造函数，则相当于默认调用super() 。<br />
java没有类的多继承，多继承很复杂的一点，如果两个父类都有相同的方法，子类怎么处理？ 
<img src ="http://www.blogjava.net/yuxh/aggbug/305181.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/yuxh/" target="_blank">yuxh</a> 2009-12-08 16:57 <a href="http://www.blogjava.net/yuxh/archive/2009/12/08/305181.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>