﻿<?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>博客园-Retrieve the power...-最新评论</title><link>http://www.cnblogs.com/Dabay/CommentsRSS.aspx</link><description /><language>zh-cn</language><pubDate>Sat, 05 Jan 2008 07:14:01 GMT</pubDate><lastBuildDate>Sat, 05 Jan 2008 07:14:01 GMT</lastBuildDate><generator>cnblogs</generator><item><title>re: Person owns Dog...</title><link>http://www.cnblogs.com/Dabay/archive/2008/01/07/530901.html#1028991</link><dc:creator>Dabay</dc:creator><author>Dabay</author><pubDate>Mon, 07 Jan 2008 06:35:00 GMT</pubDate><guid>http://www.cnblogs.com/Dabay/archive/2008/01/07/530901.html#1028991</guid><description><![CDATA[Person Owns Dog问题<br><br>这个经典的关于Person和Dog的面向对象编程的问题。它引入了一个有numOfDogsOwned属性和getNumDogsOwned方法的Person类。问题在于这样的设计对于好的设计来说不是必要的，因为Person和Dog是两个独立的概念，Dog的所属关系在Person的最基本的设计中不是必需的。<br>Page-Jones把这个问题叫做混合关系聚合（mixed-role cohesion），他这样描述这个问题：如果我们想在一个没有Dog参与的应用程序中复用这个Person该怎么办呢？你可以这么做，但是在你的Person类中有一些不必要的包袱。为什么不增加一些更多的诸如numOfCarsOwned、numOfBoatsOwned、numOfCatsOwned的属性，让这个Person类看起来更富有、更通用呢？<br>这是一个有趣的问题，因为在面对对象的领域里面还没有一个完美的解决方案。Adrian在给出了一个在AspectJ中实现的方式：<br><br>public class Person {<br>  private String name;<br>  public String getName() { return name; }<br>}<br><br>要建立一个拥有Dog的Person，但是拥有Dog的并非一定是Person，所以可以把拥有Dog这样一个限定语抽象到一个方面中：<br><br>public aspect DogOwnership {<br>  public interface IDogOwnerShip {};<br>  private List IDogOwnerShip.dogs = new ArrayList();<br>  public void IDogOwnerShip.addOneDog(Dog d) { dogs.add(d); }<br>public int IDogOwnerShip.getNumDogsOwned() { return dogs.size(); }<br>}<br><br>这个方面呈现了一个Dog所属关系的单一模块的概念。其中定义了IDogOwner这样一个接口（它并非一定要实现为一个内部接口的形式，但是这样的可以让他们更好的聚合在一起），然后申明了接口内部的dogs 成员数组，以及addOneDog和getNumDogsOwned来为所有Dog的拥有者提供调用。<br>到目前为止，还没有这样一个拥有Dog的Person，因为上面的方面只是把Dog的所属关系独立了出来。如果应用程序需要一个拥有Dog的Person的时候，可以这样编写： <br><br>public aspect PersonOwnsDog {<br>   declare parents : Person implements DogOwnership.IDogOwner;<br>}<br><br>Class Person和Aspect DogOwnership都是独立的、可复用的，而且用具体方面声名PersonOwnsDog把他们两者绑定起来也是很清楚和简单的。之后，应用程序中完全可以像下面这么使用getNumDogsOwned方法（笔者在Eclipse+ AJDT的环境下已测试通过）：<br><br>Person person = new Person();<br>person.getNumDogsOwned();<br><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/Dabay/" target="_blank">Dabay</a> 2008-01-07 14:35 <a href="http://www.cnblogs.com/Dabay/archive/2008/01/07/530901.html#1028991#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>re: 设计模式 - 构造器</title><link>http://www.cnblogs.com/Dabay/archive/2006/03/31/363112.html#363426</link><dc:creator>萧寒</dc:creator><author>萧寒</author><pubDate>Fri, 31 Mar 2006 03:58:00 GMT</pubDate><guid>http://www.cnblogs.com/Dabay/archive/2006/03/31/363112.html#363426</guid><description><![CDATA[有点意思<br><br><div align=right><a style="text-decoration:none;" href="http://www.cnblogs.com/Dabay/" target="_blank">萧寒</a> 2006-03-31 11:58 <a href="http://www.cnblogs.com/Dabay/archive/2006/03/31/363112.html#363426#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>
