桃之夭夭
知天之所为,知人之所为者,至矣。知天之所为者,天而生也;知人之所为者,以其知之所知以养其知之所不知,终其天年而不中道夭者:是知之盛也。
BlogJava | 首页 | 发新随笔 | 发新文章 | 联系 | 聚合 | 管理

2005年12月8日

Say sorry to Object-Oriented Methodology

Recently I came to know and started to learn Smalltalk, an age-old pure Object-Oriented programming language. I fell in love with it quickly. Smalltalk is a very small language with some consistent concepts and abundant class libraries. I was puzzled at the very begining,  as Smalltalk has very few keywords(maybe 5 I think), and those keywords don't include any control structure(a.k.a if, while, for), how could we write programs? Althougth I know we could implement iterative sturucture via recursion, how about conditional execution? Finally I figured out that Smalltalk provides control strucutre in its class library only using some basic OO concepts. Followings are the codes from Smalltalk class library.


 1Boolean>>ifTrue: alternativeBlock 
 2  self subclassResponsibility
 3
 4Boolean>>ifFalse: alternativeBlock
 5  self subclassResponsibility
 6
 7"and True and False are subclasses of Boolean."
 8
 9True>>ifTrue: alternativeBlock 
10  ^alternativeBlock value
11
12True>>ifFalse: alternativeBlock
13  ^nil
14
15False>>ifTrue: alternativeBlock 
16  ^nil
17
18False>>ifFalse: alternativeBlock
19  ^alternativeBlock value

and then in some codes, we could do the conditional execution as follows

14 > 3 ifTrue: [Transcript show: 'Hello']


In Smalltalk, everything is Object, so that the code above means sending a message named '>' to an Integer object, whose value is 4, with an Integer object parameter. And '>' would return a Boolean object, and then we sent a message  named 'ifTrue' to it. This is a typical usage of State Pattern. Here are the equivalent Java codes.


 1public class abstract Boolean {
 2   public static final TRUE = new True();
 3   public static final FALSE = new False();
 4
 5   public abstract Object ifTrue(Block aBlock);
 6   public abstract Object ifFalse(Block aBlock);
 7}

 8
 9class True extends Boolean {
10
11   public Object ifTrue(Block aBlock) {
12       return aBlock.execute();
13   }

14
15   public Object ifFalse(Block aBlock) {
16       return null;
17   }

18}

19
20class False extends Boolean {
21
22   public Object ifTrue(Block aBlock) {
23       return null;
24   }

25
26   public Object ifFalse(Block aBlock) {
27       return aBlock.execute();       
28   }

29}

30
31public class Integer{
32  
33   public Boolean greaterThan(Integer anInteger) {
34      
35   }

36}

37

So that we could get the following code

 

14 〉3 ifTrue: [Transcript show: 'Hello']

 

which could be translated in Java like this:

  

new Integer(4).isGreaterThan(new Integer(3)).ifTrue(new BlockClosure() {

   
public Object value() {
      System.out.println(
"Hello");
      
return null;
   }


}
);

 

That's quite simple(some dynamic languages on JVM ,e.g. Groovy, do the same thing), but it indeed brings some profound changes to my thought.


First, considering that 'if','for' and 'while' are no longer pre-defined keywords, we could define our own control structures. For example, we could define Order has its own way to do something.

 

1anOrder ifExpired: [ do reactive order]
2        ifCanceled: [do something else]

 

Taking no account of performance and semantic speaking, there is no difference between the code above and undermentioned:

 

1   4 >3 ifTrue: [do something]
2        ifFalse: [do somthing]

 

More fancy, we could define a new test structure like this:

 

1anOrderTest should: [some assert] when: [do some initalize].

 

So we could have programmable control structure in Smalltalk ( something like we do in Lisp via continuation:) ), and define our own DSL easily.That's the amazing lightweight syntax feature in Smalltalk!

 

Second, we could get full capability of Von Nouma Style Programming Language via pure OO concepts.We could construct software in consistent OO concepts.

 

I began my adventure in Object technology by using C++ in 1995, and then I chose Java as my main working language. All my experiences about OO come from half-blooded OO language.So that for a long time, I thought Object-Oriented Programming is a big patch to imperative languages. The imperative languages are desgined  to record the sequence of instruments which are used to manipulate the computer, so they are lack in semantic, and poor in abstraction. Though the Object technology introduces a successful type system and provides some abstraction mechanism, I still feel sucks to program in inconsistent concepts, because I should be careful about keeping programming more in OO-style rather than in procedural-style(something like Domina Model or not, sucks!).

Once I accused Object-Oriented Methodology of all the fault, I blamed imperfection on Object-Oriented Methodology.But now, I found out it's only because that I have little talent and learning in OO, it turned out to be my fault, I should say sorry to Object-Oriented Methodology.

posted @ 2005-12-08 20:39 Raimundox 阅读(2666) | 评论 (4) | 编辑 收藏
 
Apologize to Object-Oriented Methodology

最近学习了一下Smalltalk,然后深深的喜欢上了这个古老的语言。Smalltalk语言只具有一个很小的语言核心,这个核心由大约10几个关键字和一些基础的面向对象语义构成。而且关键字都是象: . ; ( ) [ ] | := 之类的简单符号,并没有提供最基本控制的流程。最开始的时候这让我很迷惑,虽然循环结构可以用递归表示,但是分支怎么办?然后发现了一个很酷的特性,Smalltalk可以仅仅通过面向对象的语义来实现分支结构(其实就是State Pattern),具体的代码如下

Boolean>>ifTrue: aBlock
  self subclassResponsibility

Boolean>>ifFalse: aBlock
  self subclassResponsibility

True>>ifTrue: aBlock
  ^aBlock value.

True>>ifFalse: aBlock
  ^nil.

False>>ifTrue: aBlock
  ^nil.

False>>ifFalse: aBlock
  ^aBlock value.

然后就可以,

4 〉3 ifTrue: [Transcript show: 'Hello']

因为在Smalltalk里,一切皆对象且从左到右求值,于是4 > 3 返回true,true是类True的唯一实例,然后就可以对它发送消息,ifTrue:,于是调用了^aBlock value.来对传进去的BlockClosure求值。


下面是类似的java的类似代码。

 

 1public class abstract Boolean {
 2   public static final TRUE = new True();
 3   public static final FALSE = new False();
 4
 5   public abstract Object ifTrue(Block aBlock);
 6   public abstract Object ifFalse(Block aBlock);
 7}

 8
 9class True extends Boolean {
10
11   public Object ifTrue(Block aBlock) {
12       return aBlock.execute();
13   }

14
15   public Object ifFalse(Block aBlock) {
16       return null;
17   }

18}

19
20class False extends Boolean {
21
22   public Object ifTrue(Block aBlock) {
23       return null;
24   }

25
26   public Object ifFalse(Block aBlock) {
27       return aBlock.execute();       
28   }

29}

30

4 〉3 ifTrue: [Transcript show: 'Hello']就可以对应翻译为:

 

1Boolean condition = new Integer(4).isGreaterThan(new Integer(3));
2condition.ifTrue(new BlockClosure() {
3   public Object execite() {
4      System.out.println("Hello");
5      return null;
6   }

7}
);
8
9

这个看似简单的应用,却带来了两个有深刻影响的性质。

第一,由于if,else等结构不再是预定义的语法了,而与我们自己写的代码一样,属于莫一个类的特定消息,那么也就意味着,我们可以像ifTrue一样,定义自己的分支结构。
比如
   aUser ifNotRegistered: [ do redirect to register page ]
         ifExpired: [ do redirect to active page ]

在不考虑性能优化的前提下,Smalltalk认为和
   4 >3 ifTrue: [do something]
        ifFalse: [do somthing]

是具有一样的语义的。并不因为Boolean属于Kernel就有什么不同。因此控制结构也属于一个可编程的因素,这就是Smalltalk的轻语法特性。

第二,在简单的语法和完全的面向对象语义中,构造与冯诺依曼式语言完全等价的能力(这种能力在语法上表现为赋值,分支,迭代和子程序调用),于是我们可以完全用一致的面向对象的方法来构造软件。

很长一段时间以来,我都认为面向对象方法论是在命令式的冯诺依曼式语言的基础上,通过引入类型系统然后修修补补的得到的,由于冯诺依曼语言式的语言是面向操作层面上的,只是为了更好的刻画操作计算的一个命令的序列,因此冯语言不可避免的具有不完备的语义,混乱的抽象表达以及等等一系列的问题。作为冯语言的一个大补丁的面向对象方法,我也想当然的以为他虽然有了些进步,但是基础问题上面还是不能避免的,加之面向对象缺乏一种一致的构造方法,很多时候我们不得不回归到命令式或者过程式的方法来构造系统,从而破坏掉一种一致清晰的思路,在过程和对象之间不住地权衡(比如Domain Model之争),这个让人非常的不爽。在尝试了一些面向对象语言之后(我是在95年接触C++的时候开始了解面向对象的,而后主要使用Java做为开发语言),我发现这个问题是很难避免,于是我断言这是面向对象技术本身的问题,现在看来不过自己所学有限,没有真正用过纯面向对象语言而已,汗颜得很啊。这里向面向对象方法道个歉,嘿嘿。

posted @ 2005-12-08 15:11 Raimundox 阅读(1582) | 评论 (1) | 编辑 收藏
 
随笔:35 文章:0 评论:85 引用:0
<2005年12月>
日一二三四五六
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿(12)

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • ThoughtBlog(20) (rss)
  • 学而篇第一(15) (rss)

随笔档案

  • 2007年11月 (1)
  • 2007年9月 (2)
  • 2007年8月 (1)
  • 2007年7月 (1)
  • 2007年6月 (1)
  • 2007年5月 (2)
  • 2007年3月 (8)
  • 2007年1月 (1)
  • 2006年10月 (1)
  • 2006年8月 (1)
  • 2006年3月 (1)
  • 2006年1月 (3)
  • 2005年12月 (12)

搜索

  •  

最新评论

  • 1. re: A Day of Two Pragmatic Programmers' Life
  • 能否将联系方式发到silkcut@163.com
  • --jj
  • 2. re: Misquotation
  • 哈哈
  • --Jack.Wang
  • 3. re: Selenium Better Pratice
  • 评论内容较长,点击标题查看
  • --林勇
  • 4. re: Selenium Better Pratice
  • 你好!
    还有个疑问:看sourceforge 上的文章,说Selenium是支持SAFS。这在应用中如何体现出来呢??
    非常感谢!
  • --joycezhou
  • 5. re: Selenium Better Pratice
  • 评论内容较长,点击标题查看
  • --joycezhou

阅读排行榜

  • 1. Selenium Better Pratice(6528)
  • 2. Feng Shui for Standard ML Programmers(5605)
  • 3. The Keyword 'end' Drives Me Crazy(5478)
  • 4. Why Rails so effective? Delphi Inside(5224)
  • 5. I'm Smalltalk, Which Programming Language are You?(5210)

评论排行榜

  • 1. 丧钟为谁鸣?(1)(10)
  • 2. Selenium Better Pratice(10)
  • 3. A Day of Two Pragmatic Programmers' Life(9)
  • 4. Say sorry to Object-Oriented Methodology(4)
  • 5. Agile 101: Pair Programming & Simple Design(4)

Powered by: 博客园
模板提供:沪江博客
Copyright ©2008 Raimundox