添加游客评论功能设计之测试计划

今天,添加游客评论功能设计所计划的功能已经基本实现。

1、实现过程中的改动:

  GuestArtInfo.java用原有的ArticleInfo.java代替;CookieManager.java因为所需要的代码少,另开一个类反而麻烦,功能直接写在了Action中;在util包中添加了一个处理解析存储在帖子正文头部的游客信息的工具类:GuestArtProcessor.java

2、一点心得:

  原来以为struts-config.xml中的“input="/disGoalContentAction.do"”只能用jsp文件,结果证明用*.do的路径也行,而且有时还必须如此。如下面:


        <action

            attribute="guestArtForm"

            input="/disGoalContentAction.do"

            name="guestArtForm"

            path="/postGuestArtAction"

            scope="request"

            type="com.learndiary.website.action.disgoal.PostGuestArtAction">

           

            <!--forward name="goalSuccess" path="/processGoalAction.do" /-->

           

            <!-- forward name="diarySuccess" path="/disall/disgoal/afterPostDiarySelect.jsp" /-->

            <forward name="goalSuccess" path="/toSendMailOfGoalAction.do" />

            <forward name="diarySuccess" path="/toSendMailOfGoalAction.do" />

            <forward name="isBackSubmit" path="/processGoalAction.do" />

            <forward name="isBackSubmit1" path="/disGoalContentAction.do" />

            <forward name="isBackSubmitGoal" path="/processGoalAction.do" />

            <forward name="adviceSuccess" path="/disGoalContentAction.do" />

            <forward name="adviceOfDiarySuccess" path="/disGoalContentAction.do" />

            <forward name="adviceOfGoalSuccess" path="/toSendMailOfGoalAction.do" />

            <forward name="messageSuccess" path="/main.do" />

            <forward name="failure" path="/disall/disgoal/disGoalContent.jsp" />

            <forward name="nonUser" path="/main.do" />

            <forward name="noParentArt" path="/main.do" />

        </action>

自己感觉有必要写个测试列表,进行逐项测试:

1,每项功能经过游客、注册用户、管理员的测试;每项功能在日记的评论和目标的评论中都要经过测试;

2,理想状态功能使用:

填写所有字段,无无效字符串。

3,用户信息为空,或只填用户名、邮件、网址其中一项或多项;

4,帖子内容或标题为空;

5,填写了字段,但是格式错误;

6,测试在其它页面游客发的帖子是否显示正常;

7,测试有游客发帖时的邮件发送内容是否正常;

8,测试有游客发帖时的RSS订阅内容是否正常。

9,传到网上公开测试;

例外:

1,因为软件暂时没有想到解决办法或麻烦,下列项暂时搁置:

1),网址格式过滤;

2),重复提交问题;

添加游客评论功能设计

本来,这个站根本没有朋友来回复(见:停止guest公共帐号,启用公共测试帐号),我又怕麻烦,又想学点东西,近段时间就不打算写网站的程序了。但是,我想,万一有某个朋友有心发表点意见,一看还要注册、登录,结果想在这里留点我认为弥足珍贵的文字也打退堂鼓了。基于“不怕一万,就怕万一”的理论,万一有朋友来访,我们不能拒朋友于门外吧。

1、基本功能:

就是一般博客站的游客回复功能(如http://www.jiehoo.comhttp://www.chinamyhosting.com等),具体为:

1),回复表单在显示帖子内容的下方;

2),字段包括:

名字(必须),邮件(必须),网站(可选),标题(必须,系统自动给出re:....的标题,可以修改),内容(必须);

3),朋友在提交评论后,写入cookie,以后再次访问时不用输入除标题和内容的字段;

2,以上功能在本站实现的要点:

1),回复表单在显示帖子内容的下方:

可以参考上传文件使用的frame结构,为了简化实现,游客回复去掉上传文件功能,但可以使用UBB语法;

2),因为本站已有的数据结构和为了简化实现,所有游客的回复在系统中划为一个游客帐号的帖子;但是,为了显示上述字段,可以把上面的“名字(必须),邮件(必须),网站(可选)”字段加在内容前面,并加入特殊的分隔符,把上述字段连同内容存入数据库;要显示游客的回复的时候,把从数据库中取出的内容字段分离出“名字(必须),邮件(必须),网站(可选)”字段用于显示相关内容。

这样,在取出一篇日记或目标的评论列表,并把它们显示出来的时候,对划归游客帐号的帖子的显示格式有所变化;

3),游客发表的帖子不能被普通用户修改,但管理员可以修改和删除(例如有可能出现的垃圾信息)。

4),因为游客帐号需要保存的cookie信息没有密码,也用不着把游客的相关历史信息存入数据库。

3,具体实现:

1),jsp页面文件:只有一个显示日记或目标内容的页面需要在下部加一个游客留言的frame;

2),java文件:传递游客信息需要增加一个GuestArtInfo.java,一个相应的FormBean:GuestArtForm.java,一个提交游客评论的Action:PostGuestArtAction.java,一个游客每次提交评论成功后的cookie写入方法,可以把这个方法归入到util包,为了高效,单独建一个类CookieManager.java,方法的名字就是writeCookie(Ojbect cookieInfo),然后,在用户以后来访问的时候,有一个读取cookie信息的方法:Object readCookie()也放在CookieManager中,同样。在这里,读了cookie还有一个传入游客评论frame页面的过程,这时,又需要建立一个保持cookie信息的GuestInfo.java

3),为了把游客的评论归入系统的单独一个游客帐户统一管理和简化实现,关健是要把“名字(必须),邮件(必须),网站(可选)”字段以特殊的标记加在内容前面,要显示时再把这些字段分离出来(日记的作者和管理员可以看见这些特殊内容),而且这个过程对用户来说是透明的。

初步设想如下:

存入数据库格式:


[guest]

 [name]

   littlebat

 [/name]

 [gemail]

   mdx-xx@tom.com

 [/gemail]

 [site]

   http://java.learndiary.com

 [/site]

[/quest]

评论内容

分离数据方式:

用正则表达式分离[guest]和[/guest]之间的内容,再用正则表达式把各个字段分离出来封装进上面保持游客cookie信息的GuestInfo.java类中。这个类的对象传入显示页面再读出来进行显示。

游客评论的显示格式为:标题  <a href=游客网站>游客名字</a> 发表时间

4、实现步骤:

1),依次完成GuestArtInfo.java,GuestInfo.java,CookieManager.java,GuestArtForm.java,PostGuestArtAction.java

2),完成jsp页面

3),测试和发布

5、时间:

想做就做,做完为止。

用"堆栈区数据复制"理解Java赋值和参数传递机制的心得

提纲:

1、版权声明

2、前言

3、正文

4、结论

5、附注

6、参考资料

===========================================================================================================================

1、版权声明:

  本文作者:littlebat,原始出处:用"堆栈区数据复制"理解Java赋值和参数传递机制的心得 (http://java.learndiary.com/disDiaryContentAction.do?goalID=2716),邮件:mdx-xx@tom.com。如有任何反馈意见请联系作者,作者会在本文原始出处随时更新此文。转载及引用请保留此版权声明,谢谢。

2、前言:

  在所有一切之前,先让我们看一个例子,如果您能得出这个例子的正确答案,下面的内容您应该基本上领会了。当然,请注意,本文不光只是阐述Java方法的参数传递问题,站在一个统一的角度还包括Java的赋值操作。如果您有兴趣,不妨把它看完:)

  例子1源码:Tester.java(摘改自:Java参数传递方式)


public class Tester {

    public static void main(String[] args) {

        int primitive = 2;

        changePrimitive(primitive);

        System.out.println("Step 1, primitive = " + primitive); //Step 1

        //问题1:请问,Step 1这里primitive的值是多少?

        MyClass myClass = new MyClass(); // Step 2

        System.out.println("Step 3, myClass = " + myClass); //Step 3

        changeObject(myClass);

        System.out.println("Step 8, myClass = " + myClass); //Step 8

        System.out.println("Step 9, myClass.i = " + myClass.i); //Step 9

        //问题2:请问,Step 8这里myClass指向的是执行方法changeObject(MyClass)之前的Step 3的那个对象,

        //还是在方法changeObject(MyClass)中的Step 5新生成的那个对象?

        //问题3:现在,Step 9中,myClass.i等于多少了?

    }

    public static void changePrimitive(int primitive) {

        primitive = 3;

    }

    public static void changeObject(MyClass myClass) {

        System.out.println("Step 4, myClass = " + myClass); //Step 4

        myClass.i = 3;

        myClass = new MyClass(); //Step 5

        System.out.println("Step 6, myClass = " + myClass); //Step 6

        System.out.println("Step 7, myClass.i = " + myClass.i); //Step 7

    }

}

class MyClass {

    int i = 0;

}

请认真想好,例子中3个问题您的答案是什么?

这里是标准答案:

问题1:primitive = 2

问题2:myClass仍然指向的是执行方法changeObject(MyClass)之前的Step 2的那个对象。

问题3:myClass.i = 3

您做对了吗(首先坦白,在写这篇文章之前,我得出了错误的答案:(,见:Java参数传递方式(转帖))?怎么,像我一样,没做对?那么您该好好读一下下面的内容了,读了之后还做不对的话就把我这篇帖子扔进垃圾堆吧。不过,扔之前能给我回复一下为什么我将感激不尽:)

3、正文:

  关于Java的赋值和参数传递是按值(by value)进行的还是按引用(by reference)进行的,这个问题曾经迷惑了很多人,包括我。而且,我想,这个问题还将继续迷惑一些人,包括那些C++的高手。

  在这里,我不准备用“按值(by value)”和“按引用(by value)”这样的术语来阐述这个问题。因为,从字面的理解来看,这样的术语在不同的人头脑里有不同的含义。我试图从Java数据(包括原始类型(primitive type)和对象(ojbect))在内存中的存储这个角度,用一个自创的“术语”来阐述我对这个问题的理解。这个术语就是:“堆栈区数据复制(Stack Data Copy,简称SDC)”。详细一点就是:在Java中,不管是赋值操作还是参数传递操作--针对原始类型(primitive type),是对堆栈区的原始类型的值进行复制;针对对象,是对储存在堆栈区的,对象的引用中所储存的_对象的值被存储的地址_进行复制。

  像上面抠字眼的句子读起来比较费力,我在后面将用两个例子并结合一些示意图来阐述我对这个问题的理解。希望各位朋友帮助纠正错误。

1)、赋值操作:

例子2源码:(Assign.java)


public class Assign{

  public static void main(String[] args){

    int i = 1;

    Object o = new Object();

    System.out.println("i = " + i + " ; o = " + o ); // Step 1 (示意图:3-1-1)

    int j = i;  

    Object p = o;

    System.out.println("i = " + i + " ; j = " + j + " ; o = " + o + " ; p = " + p); //Step 2 (示意图:3-1-2)

    j++;

    p = new Object();

    System.out.println("i = " + i + " ; j = " + j + " ; o = " + o + " ; p = " + p); //Step 3 (示意图:3-1-3)

  }

}

对上面例子的说明:

(1),Step 1中,整数i和对象o得到赋值。

示意图3-1-1

从示意图3-1-1中可以看出:整数i存储在堆栈区(Stack);对象o的引用存储在了堆栈区,但是对象o的值却存储在了内存堆中(Heap),对象o的引用存储了对象o的地址。

Step 1在我的机器上的一次输出结果:


i = 1 ; o = java.lang.Object@a90653

至于对象o的值输出来怎么会是那个样子,我只能告诉您:在java程序的一次运行过程中,每个Object对象输出这样的值是唯一的,因此可以借此来判断对象的引用指向的对象是否发生了改变。详情请参考Java API 文档(下同,这里给出的是:J2SE 1.5.0 API 中文版):

http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/io/PrintStream.html#println(java.lang.Object)

http://gceclub.sun.com.cn/Java_Docs/html/zh_CN/api/java/lang/Object.html#toString()

(2),Step 2中,把整数i赋值给了整数j,把对象o赋值给了对象p。

示意图3-1-2

从示意图3-1-2中可以看出:整数i的值复制给了整数j,整数j同样存储在堆栈区;存储在堆栈区的对象o的引用中存储的_对象o的值的地址C_复制给了对象p的引用,对象p的引用同样在堆栈区中。因为对象p的引用_得到了对象o的引用复制过来的_对象o的值的存储地址C,所以对象p的引用_和对象o的引用_都指向了在堆(heap)中的同一个对象,并且,这个对象的地址是地址C。

Step 2在我的机器上的一次输出结果:


i = 1 ; j = 1 ; o = java.lang.Object@a90653 ; p = java.lang.Object@a90653

(3),Step 3中,整数j的值加1,赋给了对象p新的对象值。

示意图3-1-3

从示意图3-1-3中可以看出:整数i的值不变,整数j的值加1变为2,整数在堆栈区中;新生成的对象的值存储在了堆(Heap)中,地址为F。新生成对象的值的地址F_存储在了堆栈区p的引用中,替换了原来存储在其中的地址C。于是,p的引用就指向了新生成的对象,这个新生成的对象的值的地址_是地址F。而整数i和对象o的(包括对象o的引用)没有改变也不曾有任何改变(除了初次赋值)。

Step 3在我的机器上的一次输出结果:


i = 1 ; j = 2 ; o = java.lang.Object@a90653 ; p = java.lang.Object@de6ced

至此,通过上面的例子及其示意图和说明,我得到一个结论:

在Java赋值操作中,针对原始类型(primitive type),是对堆栈区的原始类型的值进行复制;针对对象,是对储存在堆栈区的,对象的引用中所储存的_对象的值被存储的地址进行复制。这就是术语:“堆栈区数据复制(Stack Data Copy,简称SDC)”在Java赋值操作中的阐述。

2)、方法中的参数传递操作:

例子2源码:(PassParameter.java)


public class PassParameter{

  static void showMe(int pi, Object po){

    System.out.println("pi = " + pi + " ; po = " + po); // Step 2 (示意图:3-2-2)

    pi++;

    po = new Object();

    System.out.println("pi = " + pi + " ; po = " + po); // Step 3 (示意图:3-2-3)

  }

  public static void main(String[] args){

    int i = 1;

    Object o = new Object();

    System.out.println("i = " + i + " ; o = " + o); // Step 1 (示意图:3-1-1)

    showMe(i, o);

    System.out.println("i = " + i + " ; o = " + o); // Step 4 (示意图:3-2-3)

  }

}

对上面例子的说明:

(1),Step 1中,与上面Assign.java中的Step 1相同,略,下面重复其示意图3-1-1。

示意图3-1-1

Step 1在我的机器上的一次输出结果:


i = 1 ; o = java.lang.Object@a90653

(2),Step 2中,与上面Assign.java中的Step 2类似,只是Assign.java中的整数j和对象p变成了这里的方法showMe()中的参数:整数pi和对象po。并且,由于这里是参数传递,把Assign.java示意图3-1-2中的“=”替换成PassParameter.java示意图3-2-2中的“<--”,以此表示是参数传递。据我的理解,它们是一回事。

示意图3-2-2

Step 2在我的机器上的一次输出结果:


pi = 1 ; po = java.lang.Object@a90653

(3),Step 3和Step 4合并起来,见示意图3-2-3同样,与上面Assign.java中的Step 3类似。

示意图3-2-3

Step 3和Step 4在我的机器上的一次输出结果:


pi = 2 ; po = java.lang.Object@de6ced

i = 1 ; o = java.lang.Object@a90653

至此,通过上面的例子及其示意图和说明,我得到一个结论:

在Java方法参数传递操作中,针对原始类型(primitive type),是对堆栈区的原始类型的值进行复制;针对对象,是对储存在堆栈区的,对象的引用中所储存的_对象的值被存储的地址进行复制。这就是术语:“堆栈区数据复制(Stack Data Copy,简称SDC)”在Java方法参数传递操作中的阐述。

4,结论

综上所述:在Java中,不管是赋值操作还是方法的参数传递操作--针对原始类型(primitive type),是对堆栈区的原始类型的值进行复制;针对对象,是对储存在堆栈区的,对象的引用中所储存的_对象的值被存储的地址进行复制。

所以,据我的理解,术语:“堆栈区数据复制(Stack Data Copy,简称SDC)”能够有助于理解在Java中进行赋值和传递参数的机制,能够有助于在一定程度上消除“传值”、“传引用”等语义上的多变性的负面影响,可以提出来供大家交流。

5,附注:

由于本人水平有限,上面的一切全是基于实践进行的带有一些推测成分在内的个人心得总结。我也以上面的自创术语去成功解释过一些文章中的有关问题(如下面参考资料中的例程)。谨希望在能为部分Java初学者提供一个理解Java赋值和参数传递的手段的同时,更能得到各位朋友的斧正,以便能够对这个问题形成更加正确和准确的认识。在我提高认识的同时,我会在本文原始出处:用"堆栈区数据复制"理解Java赋值和参数传递机制的心得 (http://java.learndiary.com/disDiaryContentAction.do?goalID=2716)中随时更新此文。再次贴出我的邮件:mdx-xx@tom.com。谢谢。

6,参考资料:

1),Java参数传递方式 (http://www.jiehoo.com/java-pass-parameter.htm)

2),破除java神话之二:参数是传址的 (http://www.javaresearch.org/article/showarticle.jsp?column=544&thread=443)

3),Java 应用程序中的按值传递语义 (http://www.javaresearch.org/article/showarticle.jsp?column=1&thread=706)

4),我对《Java 应用程序中的按值传递语义》的理解 (http://www.javaresearch.org/article/showarticle.jsp?column=1&thread=3156)

5),Thinking in Java, 3rd Edition in Java (http://www.mindviewinc.com/downloads/TIJ-3rd-edition4.0.zip)

                                                              全文完

                                                              2006年11月22日午首稿

                                                              2006年11月24日下午第一次更新

                                                              2006年11月28日早晨第二次更新(局部小更新)

Thinking in Patterns chapter 10: Connecting different types

Thinking in Patterns with Java V0.9: chapter 10: Connecting different types: TIPatterns.htm#_Toc41169731

1, The sections name of today's learning;

Adapter. 84

Bridge. 87

Exercises. 93

2, What is it talk about in these sections and my comments and feeling for the content:

Maybe, I have gotten most feeling in this chapter. After some reading and thinking, I think I have understood these two patterns. In addition, through this chapter, I have known a little more about Proxy pattern.

1), Adapter

From my feeling, Adapter can be seen as a generlization of Proxy, and Proxy is a specific of Adapter. My understanding is below:(UML diagram is at the end of this diary)

1>, From the example: adapter:SimpleAdapter.java for Adapter and the example: proxy:ProxyDemo.java for Proxy, we can see little difference between them;

2>, From the example: adapter:AdapterVariations.java for Adapter, I haven't seen a little piece of Proxy.

Then, let us see their effects:

Proxy: fronting for another object

Adapter: Connecting different types

So, I can say in Proxy pattern: the proxy object and the implementation object these two types objects are connected by the Proxy pattern;

And, Adapter has more usage, it can unite different objects to achieve a job.

Review Bruce's words about proxy(in the end of section: Proxy: fronting for another object in chapter: Object decoupling): Of course, it isn’t necessary that Implementation have the same interface as Proxy; as long as Proxy is somehow “speaking for” the class that it is referring method calls to then the basic idea is satisfied (note that this statement is at odds with the definition for Proxy in GoF).

Then look at this statement at the end of this section: I’m taking liberties with the term “proxy” here, because in Design Patterns they assert that a proxy must have an identical interface with the object that it is a surrogate for. 

Maybe, I have found the reason of GoF's "assert that a proxy must have an identical interface with the object that it is a surrogate for. ", only in this way, Proxy pattern can be a standalone pattern in 23 patterns, otherwise, maybe it can be mixed into adaper, then, there are only 22 patterns exist now:)

And, maybe, Proxy has been used long time by people, we can't ignore its existence, it must be a standalone pattern. And, its work focuses on "fronting for other".

I can't express my entire feeling with my poor english, maybe, writing my diary in english is an error?

2), Bridge:

The most feeling in this chapter is: don't think that authority's words is all right, including GoF's. Bruce got a result after listening some other's idea about Bridge pattern, this result is: GoF's Design Patterns did "a miserable job of describing Bridge", and, there is only one place in GoF's Design Patterns is a lightspot-Bruce says continuely:"except in one place not the general structure chart describing the pattern, which wasn't helpful, but in the structure chart describing their specific example. Only if you stare at that for a bit does Bridge begin to make sense.". From this only lightspot, Bruce put up with a example of his Bridge pattern (and, maybe, is also GoF's), and, at least, I know the Bridge pattern is "often a construct that is used to help you write code. ", and it's goal is "to allow you to structure your code so that you can easily add new kinds of front-end objects which are implemented with functionality in new kinds of back-end objects."

An another meaningful statement for me is "But keep in mind that the vector of change with Bridge is typically happening at coding time: it keeps your code organized when you are dealing with an increasing number of options for implementing functionality."

Bridge pattern is a bridge between "front-end objects" and "back-end objects", regardless the number and the type of all these objects, it can provide an interface at front-end, receive all other front-end objects' requests and hand these requests to back-end interface, and it provides an interface at back-end, receive the requests which come from the front-end interface and hand these requests to back-end objects to fulfill the requests.  

3, My questions about the program itself;

1), the real application of these two patterns;

2), exercises;

4, My questions about the english language;

1), Strange words:

adaptee, take liberty with, unenlightened, begin by, conference, perspectives, give (a) talk, armed with, misinformation, delve, miserable, construct, in common, delegate, combinatorial, bookkeeping, clerking, associative, radically, Effectively

===============================================================================

//: proxy:ProxyDemo.java

// Simple demonstration of the Proxy pattern.

********************************************************************

//: adapter:SimpleAdapter.java

// "Object Adapter" from GoF diagram

********************************************************************

//: adapter:AdapterVariations.java

// Variations on the Adapter pattern.

********************************************************************

//: bridge:BridgeStructure.java

// A demonstration of the structure and operation

// of the Bridge Pattern.

********************************************************************

demonstrate the structure of Bridge

=================================================================================

                          incomplete

Java参数传递方式(转帖)

转自:http://www.jiehoo.com/java-pass-parameter.htm

Java参数传递方式

其实这个问题我原来翻译(破除java神话之二:参数是传址的 )、转帖别人的详细解释(Java 应用程序中的按值传递语义 )和专门解释( 我对《Java 应用程序中的按值传递语义》的理解)过,不过现在看来,原来翻译或者解释的角度是有问题的,从底层的角度解释并不直观,在交流的时候也容易引起误解,最终不能达成一致意见。下面以最终的效果来解释参数的传递方式:

1、对于原始数据类型,也就是int、 long、char之类的类型,是传值的,如果你在方法中修改了值,方法调用结束后,那个变量的值没用改变。

2、对于对象类型,也就是Object的子类,如果你在方法中修改了它的成员的值,那个修改是生效的,方法调用结束后,它的成员是新的值,但是如果你把它指向一个其它的对象,方法调用结束后,原来对它的引用并没用指向新的对象。

代码如下:

public class Tester {

    public static void main(String[] args) {

        int primitive = 2;

        changePrimitive(primitive);

        //primitive的值依然是2

        MyClass myClass = new MyClass();

        changeObject(myClass);

        //myClass仍然指向的是执行changeObject之前的那个对象

        //但是myClass.i等于3了

    }

    public static void changePrimitive(int primitive) {

        primitive = 3;

    }

    public static void changeObject(MyClass myClass) {

        myClass.i = 3;

        myClass = new MyClass();

    }

}

class MyClass {

    int i;

}

对于远程调用,无论是什么类型,调用结束后,传入的参数和以前没用任何变化(当然前途是直接调用远程方法,如果中间经过其它的Proxy类或者Facade类,不能保证那些类对对象没用修改)。至于是通过Locale接口进行调用的,我不太清楚是否属于远程调用。以后确定了再来更新。

作者: Cherami

原载: Java参数传递方式

版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

相关日志

    * 修复不能评论的问题

    * 使用参数方式还是页面配置方式

    * 我使用的WordPress插件

    * 解析XML的时候完全忽略DTD

随机日志

    * 机场也可以种树

    * Response被关闭

    * Java面试中的经典对比问题

    * 在目录中查找类位于哪个jar包中

添加到网摘

[del.icio.us]  [新浪 VIVI]  [365key]  [YouNote]  [博采中心]  [Poco]  [SOHU狐摘]  [天极网摘]  [和讯网摘]

喜欢这个插件?

当前日志信息

标题:

    Java参数传递方式

发表:

    2006-11-15

分类:

    Java

标签:

    java, 参数传递, 传值, 传引用

点击:

    28

评价:

    Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 Votes | Average: 0 out of 5 (暂无评价)

    Loading ... Loading ...

该日志共有 3 条评论

发表评论 | RSS订阅 | 反向链接

   1. oneal 2006-11-15

      local 调用相当于同一个虚拟机上的对普通对象方法的调用。

      上面那段代码眼熟啊,上个月做过,哈哈。

   2. Cherami 2006-11-16

      local调用可能是本地调用,但是感觉有可能会换成远程调用,这样如果没有意识到,可能会有潜在的bug。因为本地调用和远程调用的特性是不一样的。可能会误用特性。

   3. English Study and Sharing » 博客文章 » Java参数传递方式 2006-11-19

      […] 对于远程调用,无论是什么类型,调用结束后,传入的参数和以前没用任何变化(当然前途是直接调用远程方法,如果中间经过其它的Proxy类或者 Facade类,不能保证那些类对对象没用修改)。至于是通过Locale接口进行调用的,我不太清楚是否属于远程调用。以后确定了再来更新。 作者: Cherami 原载: Java参数传递方式 […]

Thinking in Patterns chapter 9: too many

Thinking in Patterns with Java V0.9: chapter 9: too many: TIPatterns.htm#_Toc41169721

1, The sections name of today's learning;

  Flyweight: too many objects. 66

  Decorator: too many classes. 69

    Basic decorator structure. 70

    A coffee example. 70

    Class for each combination. 70

    The decorator approach. 73

    Compromise. 78

    Other considerations. 82

    Exercises. 83

2, What is it talk about in these sections and my comments and feeling for the content;

  I have never felt the design pattern's importance so strong until I read this chapter, especially the section Decorator: too many chasses.

  1), Flyweight: too many objects. 66

  For my understanding, the key of Flyweight pattern is: put all the objects of a class into one object which I named it "all_in_one", get the any object of many object from this "all_in_one" object by passing the "index" of any object of many object into this "all_in_one".

  By running the modified example in this section, I am sure it's really true.

  I modified "size = 1000000" to "size = 5000000" in flyweight:ManyObjects.java, and ran it, it reported:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

        at ManyObjects.main(ManyObjects.java:22)

The same "size = 5000000" in flyweight:FlyWeightObjects.java works well, and the running time is less much than the running time in flyweight:ManyObjects.java with "size = 1000000"( with a less size, it works.).

At the end of this section, Bruce warn us don't do a "premature optimization" by using flyweight pattern unless discovering performance bottlenecks of too many objects by using a profiler tool, I think if I haven't misunderstood his words.

2), Decorator: too many classes. 69

Just this pattern give me so strong of pattern's importance. And, the example of "coffee shop" posted by Bruce is so natural and proper. I can understand the meaning of this pattern by this example even the program detail isn't very clear for me.

For my understand, I put up another example for this pattern: "man", then decorate "man" with "good man", "wise man", "old man", "strong man", at last, a strong old wise good man appears.

Without the Decorator pattern, there will be many class need to be created in Bruce's coffee shop example.

With the Decorator pattern, there are two approachs, one is "pure" decorator, and another is compromise which mixes the approach of without decorator and with decorator.

In most real stiuations, a compromise way should be taken.

This is really a book which "Problem-Solving Techniques using Java".

The uml classes diagrams of these three approachs of solving too many classes problem which from this book is at the end of this diary.

3, My questions about the program itself;

1), "implicit this pointer in method calls, the “this index” is passed in as the first argument.", how does the implicit "this" pointer work step by step in JVM? 

4, My questions about the english language;

1), Strange words:

flyweight, odd, in the company of, hack, prohibitive, excessive, externalize, pretend, untenable, admonishment, guesswork, layered, refer to as, account for, go down to, espresso, latte, teas, whip, decaf, sizeable, Mocha, steamed, foam, conform to, mug, assembling, arduous, Compromise, present, get away, syrup, Follow the compromise approach to create a menu consisting of a Margherita, Hawaiian, Regina, and Vegetarian pizzas, with toppings (decorators) of Garlic, Olives, Spinach, Avocado, Feta and Pepperdews. Create a Hawaiian pizza, as well as a Margherita decorated with Spinach, Feta, Pepperdews and Olives. detaching, beverage

2), Strange sentences:

1>, Firstly, the combinations are fixed statically so that any combination a customer may wish to order needs to be created up front.

2>, However, the implications to the all decorator or compromise approaches are the same - one extra class is created.

=============================================================================

Basic decorator structure

******************************************************************

Class for each combination

******************************************************************

The decorator approach

******************************************************************

Compromise

不能对孩子开这样的玩笑!

昨天中午,我送孩子上幼儿园。到了幼儿园门口,她非要零食吃。孩子这段时间吃零食太多了。在家里才吃了零食,所以我不准备给她买。店主在里面,我没有自己喊。孩子自己喊了几声没反应。我就开玩笑地对她说:“你看,老板不在,买不成。你想吃,自己去拿吧,看拿不拿得到?”

她站在玻璃柜台前面,往里看了又看,看玻璃柜后面。很像跃跃欲试的感觉。我一看,马上意识到我已经无意中犯了一个错误,也许还不小。就自己喊了老板给孩子买了零食吃。

这样的玩笑不能够对孩子开。孩子就像一张白纸,是没有任何对错是非观念的。你叫她去柜台里直接拿吃的,也许在她的理解就是:大人都叫我拿,这肯定是可以拿的了。如果经常反复这样,对孩子的行为习惯的塑造极为不利。

我记得自己读小学1,2年级的时候,自己还算老实吧,跟一个很调皮的孩子一起玩。然后就很自然的一起从一个缝隙钻到大伯屋里偷偷拿了大伯的钓鱼杆,这事还引起了大人之间的误会。在我当时的印象中,根本就不知道这叫作“偷窃”。

所以,我想起,有些大人爱跟小孩子开玩笑,比如,叫孩子去拿别人的东西,去打别人,去骂别人(如不雅的外号),等等。结果,在大人的哈哈大笑中,可能就给小孩的行为习惯甚至道德的培养上留下了阴影。

所以,在孩子面前开玩笑,大人们还需要三思而后行!

Thinking in Patterns chapter 8: Specialized creation

Thinking in Patterns with Java V0.9: chapter 8: Specialized creation: TIPatterns.htm#_Toc41169717

1, The sections name of today's learning;

Prototype. 61

Builder. 62

Exercises. 66

2, What is it talk about in these sections my comments and feeling of the content:

1), Prototype

Come up with it at this chapter, but will descript it later.

2), Builder.

"They never define exactly what they mean by representation. ", "but alas, ", etc... Again, Bruce appears to be not satisfied with the GoF's work about this part. At the end of this section, Bruce gives a simple definition of "Builder" pattern is: "Thus, Builder could be described as using a Policy to create objects."

And, maybe I have known a difference between "State" and "Builder" is: State "simply forwarding the requests" , Builder "has a sequence of operations to perform", and both "State" and "Builder" is called by a context to perform its job. At this point, maybe I have seen a little meaning of the "context" which mentioned at the begin of this book as "it will often be the controller that manages the operation of the pattern". In the example: state:StateDemo.java, the context class is "ServiceProvider", in the example: builder:BuildMedia.java, the context is "MediaDirector".

And, maybe I have also known a little of statement appeared at the section of State pattern: "State can be found everywhere because it’s such a fundamental idea. For example, in Builder, the “Director” uses a backend Builder object to produce different behaviors.".

At this point, the difference of "State", "Strategy", "Policy", "Builder" patterns make me a bit dizzy. But, I think if I read more source code of these patterns and apply these patterns in some real projects (like LearnDiary), maybe I will master these patterns really, and maybe create my own design patterns, isn't it?

3, My questions about the program;

1), Although the resulting “object” (the entire converted text file) is created over time, if you consider the conversion of each RTF directive to be an object, this feels to me a little more like Bridge, because the specific types of converters extend the interface of the base class.

2), exercises;

4, My questions about the english language;

1), Strange words:

representation, spread out, over time, essence, maze, alas, overwhelmingly, compelling, a.k.a. (Also known as.), in some ways

2), Strange sentences:

1>, The final variation of the maze builder is something that doesn’t create mazes at all, but instead counts the rooms in an existing maze.

===============================================================================================================

UML classes diagrams of examples mentioned in this chapter:

**********************************************************************************************

//: state:StateDemo.java

// Simple demonstration of the State pattern.

 

*****************************************************************************************************

//: strategy:StrategyPattern.java

//Strategy: choosing the algorithm at run-time

***************************************************************************************************

//: builder:BuildMedia.java

// Example of the Builder pattern

                                                      incomplete

Thinking in Patterns chapter 7: Encapsulating creation

Thinking in Patterns with Java V0.9: chapter 7: Encapsulating creation: TIPatterns.htm#_Toc41169712

1, The sections name of today's learning;

simple Factory method. 53

Polymorphic factories. 55

Abstract factories. 58

Exercises. 61

2, What is it talk about in these sections and my comments and feeling for the content:

Without the deeply thinking about the content of this chapter, and maybe I

have not many experience on program, I haven't known what matters I will meet

if creating object without "factory". But, I can see what it is talking about

in this chapter.

1), simple Factory method

The key of this pattern is a static factory method in base class can create

the objects of all kinds of sub classes according to the parameter passed into

this static factory method.

From the viewpoint of OOP, this method isn't a pure OO, because there are some

condictional testing in this method like below: 


    if(type.equals("Circle")) return new Circle();

    if(type.equals("Square")) return new Square();

Is there any way make this example more OO?

2), Polymorphic factories.

Just like Bruce's words in this section as below:


However, it seems that much of the time you

don?t need the intricacies of the polymorphic factory method, and a single

static method in the base class (as shown in ShapeFactory1.java) will work

fine.

After reading the example of this pattern, I can understand the base concept

of this pattern, I rewrite the process of this example with my own words

below:

Let's start from the main method, tracking the running step of this

example:

1>, suppose ShapeFactory.createShape("Circle"), and suppose no "Circle" in the

Map "factories" in abstract class ShapeFactory, the statement

[Class.forName("factory.shapefact2.Circle");] will create a class object

of class "factory.shapefact2.Circle", so the static block in this class will

process, here is below:


  static {

    ShapeFactory.addFactory(

      "Circle", new Factory());

  }

At this point, key-value pair: "Circle", instance object of Circle's inner class Factory which is a sub class of ShapeFactory has been puted into Map "factories";

2>, then, the statement below will create a "Circle" object and return it into

 the statement in main method:


return

      ((ShapeFactory)factories.get(id)).create();

In this way, we can add any number's new type without modifing any existed

code, even include the factory method in class ShapeFactory.

This is a real pure OO style objects creation schema just a little complex.

Maybe, I know the reason of GoF's "However, the Design Patterns book

emphasizes that the reason for the Factory Method pattern is so that different

types of factories can be subclassed from the basic factory (the above design

is mentioned as a special case)."

Maybe because GoF want come up with a pure OO factory schema, so they say that

the simple factory method is a special case of their "intricacies of the

polymorphic factory method", I think.

I feel that Bruce maybe prefer "simple factory method" than that "Polymorphic

factories" between his words. I think Bruce is a pragmatism master, if the

non-pure OO schema can resolve problem well, why to chose a more complex

implementation? just I think.

For me, if no enough reason for chosing a complex OO impelmentation, maybe I will chose that "simple factory method", at least, I can reduce many lines' coding. 

3), Abstract factories

The situation of applying this pattern in my mind maybe is below:

1>, There are several scene in the application;

2>, There are several class' object involved in every scene;

3>, The several class' object's composition is defferent from one scene to

another scene;

4>, So, every scene need a specific object factory;

5>, Create an Abstract factory is used as an interface of the specific object

factory above;

I haven't understood this pattern very clearly. If I meet some situations

future like above, I will remember this "Abstract factories" and will try its application too if I haven't forgotten this book.

3, My questions about the program itself;

1), The idea is that at the point of creation of the factory object, you decide how all the objects created by that factory will be used; 

2), The use of Abstract factories;

3), exercises;

4), what are there problems when creating objects in a real application without these factories pattern above?

4, My questions about the english language;

1), Strange words:

  sensible, chase down, matter, likely, suspect, happen to, presumably, sophisticated, intricacies, portability, Obstacle, Puzzle,

2), Strange sentences:

  1>,It happens to be the creation of the type that matters in this case rather than the use of the type (which is taken care of by polymorphism), but the effect is the same: adding a new type can cause problems.

  2>, If all the code in your program must go through this factory

whenever it needs to create one of your objects, then all you must do when you

add a new object is to modify the factory.

================================================================================

Example's uml class diagram reversed by ArgoUML0.22 (need some notion for these diagram, maybe I will do it future)

--------------------------------------------------------------------------------

Example:

//: factory:shapefact1:ShapeFactory1.java

// A simple static factory method.

*******************************************************************************

Example:

//: factory:shapefact2:ShapeFactory2.java

// Polymorphic factory methods.

********************************************************************************

Example:

//: factory:Games.java

// An example of the Abstract Factory pattern.



================================================================================

Thinking in Patterns chapter 6: Factoring commonality

Thinking in Patterns with Java V0.9: chapter 6: Factoring commonality: TIPatterns.htm#_Toc41169707

1, The sections name of today's learning;

Strategy: choosing the algorithm at run-time  48

Policy: generalized strategy. 50

Template method. 50

Exercises

2, What is it talk about in these sections and comments and feeling for the content:

  the “once and only once” principle, said at the begin of this chapter, I can't know it's meaning very clear, is it saying: do only one thing in a method?

  And, State, Strategy, Policy, Template method, it seems to me these four patterns has very similiar character, I can't distinguish them very clear.

  1), Strategy: choosing the algorithm at run-time

  However, I haven't find there is any difference bwteen State and Strategy now except their names.  class ServiceProvider in state:StateDemo.java just like class MinimaSolver in strategy:StrategyPattern.java, and so on.

  2), Policy: generalized strategy.

  No example in this section, it just says:"It also seems generally useful to distinguish Strategies with single methods from Policies with multiple methods."

  3), Template method.

  What is Template method?

  From my understanding, there exist two definitions : one is Template method Patterns; and another is a specific method in a base class, the mothod is usually called by the initialization method of this base class (I can't know if it is always called by that initialization method), this template method then call other methods in the base class to perform some actions, these performing real actions' methods can be overridden by the derived class.

  Although it says:"Note similarity with template method ? TM claims distinction that it has more than one method to call, does things piecewise. However, it?s not unlikely that strategy object would have more than one method call;" I can't feel there are too many similar places between State pattern and Template method pattern. Maybe, after reviewing this book, I will change my feeling a lot.

  I have found a bit difficult to learn this book, and write the diary in

english is a bit difficult too, if the difficult degree over my knowledge

scope a lot, maybe I will seek another resource to learn Design Patterns,

maybe a chinese book is better for me?

3, My questions about the program itself;

  1), Can't understand: Applying the "once and only once" principle produces the  most basic pattern of putting code that changes into a method.

  2), The difference between State and strategy pattern;

  3), Policy's example

  4), The difference between State and Template method.

4, My questions about the english language;

  1), Strange words:

  factoring commonality, similarity, piecewise, piecewise, shallowway, fulfullment, Policy, generalized, customs, undetermined,

  2), Strange sentences:

  1>, A fundamental concept in the application framework is the Template Method which is typically hidden beneath the covers and drives the application by calling the various methods in the base class (some of which you have overridden in order to create the application).

*****************************************************************************************************

//: strategy:StrategyPattern.java

//Strategy: choosing the algorithm at run-time

                                                      incomplete