我一直好想睡覺
好想睡
這幾天到了晚上7-8點總是很想睡覺
好像每年到了這個時期都會這樣?
去年也是, 我早早就回家睡了
今天主要在講Java Bean, 老師說這玩意非常的重要
但怎麼看,不就是Reflection的應用嗎?
另外就是大家Follow一個Pattern來Design
像是
private Color background;
public void setBackground(Color c);
public Color getBackground();
用這樣的pattern Design出來的軟體,在藉由reflection取得attribure
就可以plug-in方式加入
Java的一些IDE, NetBeans, Eclipse 都可以利用Beans增加一些元件...
作業要我們寫一個whiteboard 可以用Bean畫圖, 定義一個介面
然後要可以把屬性利用server boradcast給其他client,
恩 先把reflection弄清楚在開始寫吧
2008年4月30日 星期三
今天上課
2008年4月28日 星期一
2008年4月27日 星期日
2008年4月26日 星期六
2008年4月23日 星期三
昨天上課
介紹了Reflection, JNI, JIT
Reflection是一種相當重要的機制,有點像是DLL
能夠在動態取得類別物件的資訊並產生新的物件
有點抽象,以Java而言其參考並不是像C++這樣指向記憶體
為了能夠作garbage collection他至少跳了兩次
先指向一個固定size的欄位,這個欄位放在一個表格
這個表格由一個GC執行緒管理, 這個欄位至少有指向兩個東西
一個是這個物件的資料(code seg, data seg),另一個欄位指向一個特殊的class
"Class",這個class(注意大小寫),儲存了關於這個class的Method, Filed, Superclass, Interface訊息及Static成員資料,相同類別的物件都指向這個Class
使用Class.forName(String)這個方法可以將class檔動態載入,取得其資訊
在利用Class.newInstance方法產生物件,預設是沒有建構參數,有的話要多一些手續
Reflection加上介面抽象化很容易便可以設計一個相當flexible的系統
像是Applet, RMI, IDL, Beans...
Simple Factory Design Pattern + Reflection 更能發揮其power
JNI則是C/C++與Java程式溝通的方法
透過JVM提供的一些方式呼叫/被呼叫Java函式
沒什麼道理,就是這樣用而已,知道有這個東西就好,非必要不要碰
JIT(Just in Time Compile)是一個相當重要的概念
JVM將class檔load到資料結構後,JVM其實就是用C/C++實作的
開始執行時將一部分bytecode翻譯成native code,這個地方牽涉到許多最佳化的問題
一開始是one by one mapping, 從byecode to Assembly,所花的時間是線性的
但是當一段code被執行的次數增多(JVM通常會去profileing)
會多花一些時間進行Compiler這段code以減少cpu cycle
2008年4月21日 星期一
2008年4月16日 星期三
z-words movie
http://imdb.com/title/tt0365748/
今天看了這部直得讚賞的殭屍片
之前看了這個導演的Hot Fuzz,我很喜歡他的風格
非常的Funny, 這難道就是英式幽默? 嘿 我好喜歡英國腔
其中有一段與僵屍格鬥時,點唱機撥出了Queen的Don't Stop Me Now
主角說Kill the Queen指要關掉這個點唱機
所有人都愣住(我猜以為他要人家放Killer Queen這首XD)
有感人的地方,又充滿歡樂的元素
至於為什麼人會變成Zombie, who care?(電影從頭到尾都沒有提到 哈哈)
2008年4月15日 星期二
2008年4月14日 星期一
2008年4月13日 星期日
2008年4月11日 星期五
2008年4月9日 星期三
昨天上課
發表了 HW2 & 3
真夠勁,打算用禮拜六日來寫
講到了Class檔的format
如何把java所有資訊塞到class檔.const data, code segement這些東西
Java在執行過程中擁有完整的資訊,所以可以實現Reflection機制
及一些安全性的東西(validation)
JVM在class還沒開始執行前會檢查檔案是否被修改過
由於register是一個stack,它會檢查這個stack在每個instruction的狀態
這節課讓我對JVM的design有初步認識
把一個class dump出來看看,長這樣
這是一種二進位檔的format, 有點像ELF/PE這類的東西
有標頭檔, Data, code segment, 給JVM吃的
其中Code Segment就沒有解碼了
u4 magic; cafebabe
u2 minor_version; 0000
u2 major_version; 0032
u2 constant_pool_count; 0039 (57)
constant_pool:
01: 10: Methodref(class: 15, 29)
02: 9: Field(class: 11, 30)
03: 9: Field(class: 31, 32)
04: 7: Class(33)
05: 10: Methodref(class: 4, 29)
06: 8: String(34)
07: 10: Methodref(class: 4, 35)
08: 10: Methodref(class: 4, 36)
09: 10: Methodref(class: 4, 37)
10: 10: Methodref(class: 38, 39)
11: 7: Class(40)
12: 10: Methodref(class: 11, 41)
13: 10: Methodref(class: 11, 42)
14: 8: String(43)
15: 7: Class(44)
16: 7: Class(45)
17: 1: UTF8(4,"data")
18: 1: UTF8(1,"I")
19: 1: UTF8(6,"")
20: 1: UTF8(4,"(I)V")
21: 1: UTF8(4,"Code")
22: 1: UTF8(15,"LineNumberTable")
23: 1: UTF8(3,"run")
24: 1: UTF8(3,"()V")
25: 1: UTF8(4,"main")
26: 1: UTF8(22,"([Ljava/lang/String;)V")
27: 1: UTF8(10,"SourceFile")
28: 1: UTF8(13,"MyThread.java")
29: 12: NameAndType(19,24)
30: 12: NameAndType(17,18)
31: 7: Class(46)
32: 12: NameAndType(47,48)
33: 1: UTF8(23,"java/lang/String/Builder")
34: 1: UTF8(4,"Run ")
35: 12: NameAndType(49,50)
36: 12: NameAndType(49,51)
37: 12: NameAndType(52,53)
38: 7: Class(54)
39: 12: NameAndType(55,56)
40: 1: UTF8(8,"MyThread")
41: 12: NameAndType(19,20)
42: 12: NameAndType(23,24)
43: 1: UTF8(9,"JVM test!")
44: 1: UTF8(16,"java/lang/Object")
45: 1: UTF8(18,"java/lang/Runnable")
46: 1: UTF8(16,"java/lang/System")
47: 1: UTF8(3,"out")
48: 1: UTF8(21,"Ljava/io/PrintStream;")
49: 1: UTF8(6,"append")
50: 1: UTF8(45,"(Ljava/lang/String;)Ljava/lang/StringBuilder;")
51: 1: UTF8(28,"(I)Ljava/lang/StringBuilder;")
52: 1: UTF8(8,"toString")
53: 1: UTF8(20,"()Ljava/lang/String;")
54: 1: UTF8(19,"java/io/PrintStream")
55: 1: UTF8(7,"println")
56: 1: UTF8(21,"(Ljava/lang/String;)V")
u2 access_flags; 0020
u2 this_class; 000b
u2 super_class; 000f
u2 interfaces_count; 0001
u2 interfaces[interfaces_count]; 0010
u2 fields_count; 0001
field_info fields[fields_count];
filelds[1] {
u2 access_flages; 0000
u2 name_index; 0011
u2 descriptor_index; 0012
u2 attributes_count; 0000
attribute_info attributes;
}
u2 methods_count; 0003
Method[1] {
u2 access_flags; 0000
u2 name_index; 0013
u2 descriptor_index; 0014
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 002a (42)
u1 info[attribute_length];
00 02 00 02 00 00 00 0a 2a b7 00 01 2a 1b b5 00 02 b1 00 00 00 01 00 16 00 00 00 0e 00 03 00 00 00 07 00 04 00 08 00 09 00 09
}
Method[2] {
u2 access_flags; 0001
u2 name_index; 0017
u2 descriptor_index; 0018
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 0039 (57)
u1 info[attribute_length];
00 03 00 01 00 00 00 1d b2 00 03 bb 00 04 59 b7 00 05 12 06 b6 00 07 2a b4 00 02 b6 00 08 b6 00 09 b6 00 0a b1 00 00 00 01 00 16 00 00 00 0a 00 02 00 00 00 0b 00 1c 00 0c
}
Methid[3] {
u2 access_flags; 0009
u2 name_index; 0019
u2 descriptor_index; 001a
u2 attributes_count; 0001
u2 attribute_name_index; 0015
u4 attribute_length; 0000 003b (59)
u1 info[attribute_length];
00 03 00 02 00 00 00 17 bb 00 0b 59 10 0a b7 00 0c 4c 2b b6 00 0d b2 00 03 12 0e b6 00 0a b1 00 00 00 01 00 16 00 00 00 12 00 04 00 00 00 0e 00 0a 00 0f 00 0e 00 10 00 16 00 11
}
Attributes count: 0001
Attributes:
SourceFile_attribute {
u2 attribute_name_index; 001b
u4 attribute_length; 0000 0002
u2 sourcefile_index; 001c
}
2008年4月7日 星期一
善與惡
今天來學校的路上剛好想到
一個人的善跟惡根本沒有辦法判斷
就像愛跟恨一樣,它們是同一種感情
我不是好人,也不是壞人
有時候會做好事,也做了不少壞事
所以評論一個人是好人壞人根本沒什麼意義
罪與罰那個主角,他基於一個善的想法
去做了一件惡的事情,最後接受法律制裁
也有看過一則新聞,有個在醫院打工的小夥子
強姦了一個準備送到太平間的女孩
但那個女的卻因此活過來,似乎是陷入某種假死的狀態
這個人基於一個惡的行為,卻導致善的結果,但他還是必須接受法律制裁
那個女孩家屬相當感謝那個小夥子還幫他打官司
------------------------------------
剛剛看了"Once"這部電影
充滿很多音樂元素的
關於愛情的處理也相當不錯
在台灣好像還沒上映
電影中捷克女孩好美...
我喜歡這部電影!
-------------
因為感冒一個多禮拜沒有跑步
今天又開始跑
...
體力變差了
..
明天早上去游泳!
2008年4月6日 星期日
Decorator
Decorator 用來將功能擴充
先定義一個介面,Decorator具有該介面,且將之擴充功能
Java的IO用到很多這個Pattern
class Base {
public:
virtual void Action(){}
};
class Decorator : public Base {
private:
Base* base;
public:
Decorator(){};
Decorator(Base* base)
{
this->base = base;
}
virtual void Action()
{
this->base->Action();
this->Add_on();
}
virtual void Add_on(){}
};
class BaseClass : public Base {
public:
virtual void Action()
{
cout << "Action:\n";
}
};
class GoodDecorator : public Decorator {
public:
GoodDecorator(Base* base) : Decorator(base)
{}
virtual void Add_on()
{
cout << "Good\n";
}
};
class BadDecorator : public Decorator {
public:
BadDecorator(Base *base) : Decorator(base)
{}
virtual void Add_on()
{
cout << "Bad\n";
}
};
int main()
{
BaseClass *bs = new BaseClass();
GoodDecorator *good = new GoodDecorator(bs);
BadDecorator *bad = new BadDecorator(bs);
bs->Action();
good->Action();
bad->Action();
return 0;
}
2008年4月5日 星期六
Composite
Composite
很難解釋
簡單的說應該像是樹狀結構
我們定義一個樹狀結構節點的介面
每個節點又包含其他節點
這些實做這個介面的物件就能夠用組合起來(如樹狀結構般)
Java SWING的component就是用這個方法設計的
#include
#include
using namespace std;
class component {
private:
listchildren;
public:
component() {
}
virtual void Action()
{}
void Trace()
{
this->Action();
list::iterator i;
for( i = children.begin(); i != children.end(); ++i)
{
(*i)->Trace();
}
}
void Add(component* child) {
children.push_back(child);
}
void Remove(component* child) {
children.remove(child);
}
};
class NodeA : public component {
public:
virtual void Action()
{cout << "NodeA\n";}
};
class NodeB : public component {
public:
virtual void Action()
{cout << "NodeB\n";}
};
class NodeC : public component {
public:
virtual void Action()
{cout << "NodeC\n";}
};
int main()
{
NodeA a;
NodeB b1;
NodeB b2;
NodeC c1;
NodeC c2;
NodeC c3;
a.Add(&b1);
a.Add(&b2);
b1.Add(&c1);
b1.Add(&c2);
b2.Add(&c3);
a.Trace();
return 0;
}
2008年4月4日 星期五
Chain of Responsibility
Chain of Responsibility
目的在將一個訊息傳遞下去
讓每個物件都判斷是否有該處理的事情
就像老闆交代一件事情下來,所有員工都問問看,有沒有人會
java awt就是使用這個pattern
有點像linked-list
#define ART 0
#define ENGINEERING 1
#define SCIENCE 2
#define LABOR 3
#define POLITICS 4
class Request {
public:
int request_type;
Request(){}
Request(int type)
{
request_type = type;
}
};
class Handler {
public:
int handler_type;
Handler* next_handler;
Handler()
{
this->next_handler = NULL;
}
void try_handle(Request* request) {
cout << "review request...\n";
if( request->request_type == this->handler_type ) {
this->handle(request);
}
if( this->next_handler != NULL ) {
this->next_handler->try_handle(request);
}
}
void set_next_handler(Handler* next_handler)
{
this->next_handler = next_handler;
}
virtual void handle(Request* request) {
}
};
class DirtyRequest : public Request {
public:
DirtyRequest() : Request(POLITICS) // 這邊算是trick, invoke superclass的constructor必須在code block外面
{}
};
class Engineer : public Handler {
public:
Engineer()
{
this->handler_type = ENGINEERING;
}
virtual void handle(Request* request) {
cout << "Engineer Soloved\n";
}
};
class Artist : public Handler {
public:
Artist()
{
this->handler_type = ART;
}
virtual void handle(Request* request) {
cout << "Artist Soloved\n";
}
};
class Politican : public Handler {
public:
Politican()
{
this->handler_type = POLITICS;
}
virtual void handle(Request* request) {
cout << "Politican Soloved\n";
}
};
int main()
{
Engineer *eng = new Engineer();
Artist *art = new Artist();
art->set_next_handler(eng);
Politican *pol = new Politican();
pol->set_next_handler(art);
pol->try_handle( new DirtyRequest() );
return 0;
}
Builder
Builder Design Pattern
用在Java的Thrad其Runnable介面即是利用Builder Design
寫Java其實你已經跟著他的步驟在使用Pattern
只是渾然不覺
使用別人寫的系統時不如花點時間思考一下
為什麼別人要這樣設計?
class Ibuilder {
public:
virtual void build(){}
};
class good_builder : public Ibuilder {
public:
virtual void build()
{
cout << "good building\n";
}
};
class bad_builder : public Ibuilder {
public:
virtual void build()
{
cout << "bad building\n";
}
};
class who_use_builder : public Ibuilder {
private:
Ibuilder *builder;
public:
who_use_builder(Ibuilder *builder)
{
this->builder = builder;
}
who_use_builder()
{
this->builder = this;
}
virtual void build()
{
}
void start_build()
{
this->builder->build();
}
};
class I_use_builder : public who_use_builder {
public:
virtual void build()
{
cout << "fair building\n";
}
};
int main()
{
// like extends Thread
I_use_builder* me = new I_use_builder();
me->start_build();
// like implements Runnable interface
who_use_builder* w1 = new who_use_builder(new good_builder);
w1->start_build();
who_use_builder* w2 = new who_use_builder(new bad_builder);
w2->start_build();
return 0;
}
Abstract Factory
Abstract Factory
跟Simple Factory不同的是
進而將Factory抽象化,實現不同的工廠
必要時工廠能夠快速的抽換
例如在版本控制或是跨平台
class Iproduct_Food {
public:
virtual void eat(){}
};
class Iproduct_Game {
public:
virtual void play(){}
};
class abstract_factory {
public:
virtual Iproduct_Food* create_Food(){}
virtual Iproduct_Game* create_Game(){}
};
class good_food : public Iproduct_Food {
public:
virtual void eat()
{
cout << "eating good food\n";
}
};
class good_game : public Iproduct_Game {
public:
virtual void play()
{
cout << "playing good game\n";
}
};
class good_factory : public abstract_factory {
public:
virtual Iproduct_Food* create_Food()
{
return new good_food();
}
virtual Iproduct_Game* create_Game()
{
return new good_game();
}
};
class bad_food : public Iproduct_Food {
public:
virtual void eat()
{
cout << "eating bad food\n";
}
};
class bad_game : public Iproduct_Game {
public:
virtual void play()
{
cout << "playing bad game\n";
}
};
class bad_factory : public abstract_factory {
public:
virtual Iproduct_Food* create_Food()
{
return new bad_food();
}
virtual Iproduct_Game* create_Game()
{
return new bad_game();
}
};
int main()
{
abstract_factory* f1 = new good_factory();
abstract_factory* f2 = new bad_factory();
f1->create_Food()->eat();
f1->create_Game()->play();
f2->create_Food()->eat();
f2->create_Game()->play();
return 0;
}
2008年4月3日 星期四
Simple Factory
目的在對一個產品進行抽象化,將其介面獨立出來
建立產品去實現這個介面
再由一個工廠產生,實作此介面的物件
C++沒有Reflection,如果要用的話應該要使用一些library
class Imust_imp {
public :
virtual void do_something(){};
};
class Imp1 : public Imust_imp {
public:
virtual void do_something()
{
cout << "I am singing\n";
}
};
class Imp2 : public Imust_imp {
public:
virtual void do_something()
{
cout << "I am playing\n";
}
};
class Simple_factory {
public:
Imust_imp* create_by_name(char* class_name)
{
Imust_imp* ret;
if( strcmp(class_name, "Imp1") == 0 ) {
ret = new Imp1();
return ret;
}
if( strcmp(class_name, "Imp2") == 0 ) {
ret = new Imp2();
return ret;
}
return 0;
}
};
int main()
{
Simple_factory *fac = new Simple_factory();
Imust_imp* a = fac->create_by_name("Imp1");
Imust_imp* b = fac->create_by_name("Imp2");
a->do_something();
b->do_something();
return 0;
}
2008年4月2日 星期三
昨天上課
講到了Awt跟Swing
一開始awt的design方式是用abstract factory這個design patten
所有的元件都是用作業系統的,一個按鈕在MAC跟在X-window顯示出來會不一樣
由於這樣做,牽涉到許多context switch會造成許多overhead(因為交給OS的componet處理)
作什麼事情,當你需要快時,就先用dirty solution,但是記著這邊有問題,以後有餘力要回來改
(有點像考試前先死記,之後再融會貫通XD)
當初發展JDK 1.0時也是抱持著這樣的想法,等Java紅了之後,他們派人來處理這塊
於是誕生了Swing
Swing的元件彈性很大,因為他們之所以看起來一樣是他們都是"畫"出來的
所以在MAC與Windows看起來都一樣,不再使用OS的widget(除了JFrame以外)
但Swing實際上也存在著一些問題需要解決,例如:因為是軟體畫的,無法有效運用顯示卡
然後提到了
paint & repaint & update & double buffering & GUI thread & M-V-C & applet & jar
當畫面需要更新: repaint -> update()
當視窗移動或改變: paint
如果你沒有override update時,java會先把你的影像清掉然候等待時機再paint
(考慮 GUI thread的安全) 這時會閃爍
要避免可以override update()方法 然後call paint( 相反也可以 )
或是用double buffering先畫在一個image再更新到Graphic
介紹了一下Model View Control的概念
例如JTree 這個swing componet將其樹狀資料(Model)及控制與外觀分開
還有applet與jar
把用到的class包成jar會比一個個去load快(這跟網路連線有關, TTL)
jar其實是zip , 有點像tar方式包裝
複習一下
tar -zxvf xx.tar.gz des
tar -zcvf xx.tar.gz des
jar很類似