Skip to content

Commit 11a133a

Browse files
committed
修正!~~
1 parent 5bb785d commit 11a133a

File tree

8 files changed

+80
-98
lines changed

8 files changed

+80
-98
lines changed

command/lijunhuayc/readme.md

+12-41
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ Android设计模式源码解析之命令模式
3333
## 3. 模式的简单实现
3434
### 简单实现的介绍
3535
命令模式其实就是对命令进行封装,将命令请求者和命令执行者的责任分离开来实现松耦合。
36-
`阐述一下你的示例要实现的功能`
36+
这里我们通过一个简单的实例来剖析一下命令模式:命令接收者ReceiverRole拥有一个PeopleBean类型成员,通过Invoker发出不同的命令来修改PeopleBean的相对应的属性,具体命令实现类ConcreteCommandImpl1执行修改年龄命令,ConcreteCommandImpl2执行修改姓名的命令等等,ClientRole负责组装各个部分。
37+
例子代码如下(resource目录下也可以查看)。
3738

3839
### 实现源码
3940

@@ -337,13 +338,6 @@ public class Thread implements Runnable {
337338
Runnable target;
338339
private static int count = 0;
339340
340-
/**
341-
* Starts the new Thread of execution. The <code>run()</code> method of
342-
* the receiver will be called by the receiver Thread itself (and not the
343-
* Thread calling <code>start()</code>).
344-
* @throws IllegalThreadStateException if the Thread has been started before
345-
* @see Thread#run
346-
*/
347341
public synchronized void start() {
348342
if (hasBeenStarted) {
349343
throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?
@@ -397,10 +391,6 @@ bool dvmCreateInterpThread(Object* threadObj, int reqStackSize){
397391
398392
}
399393
400-
/*
401-
* Alloc and initialize a Thread struct.
402-
* Does not create any objects, just stuff on the system (malloc) heap.
403-
*/
404394
static Thread* allocThread(int interpStackSize)
405395
{
406396
Thread* thread;
@@ -416,17 +406,13 @@ static Thread* allocThread(int interpStackSize)
416406
接下来在12行通过pthread_create创建pthread线程,并让这个线程start,这样就会进入该线程的thread entry运行,下来我们来看新线程的thread entry方法 interpThreadStart,同样只列出关键的地方:
417407

418408
```
419-
/*
420-
* pthread entry function for threads started from interpreted code.
421-
*/
409+
//pthread entry function for threads started from interpreted code.
422410
static void* interpThreadStart(void* arg){
423411
Thread* self = (Thread*) arg;
424412
std::string threadName(dvmGetThreadName(self));
425413
setThreadName(threadName.c_str());
426414
427-
/*
428-
* Finish initializing the Thread struct.
429-
*/
415+
//Finish initializing the Thread struct.
430416
dvmLockThreadList(self);
431417
prepareThread(self);
432418
@@ -440,48 +426,33 @@ static void* interpThreadStart(void* arg){
440426
*/
441427
self->jniEnv = dvmCreateJNIEnv(self);
442428
443-
/*
444-
* Change our state so the GC will wait for us from now on. If a GC is
445-
* in progress this call will suspend us.
446-
*/
429+
//修改状态为THREAD_RUNNING
447430
dvmChangeStatus(self, THREAD_RUNNING);
448431
449-
/*
450-
* Execute the "run" method.
451-
* At this point our stack is empty, so somebody who comes looking for
452-
* stack traces right now won't have much to look at. This is normal.
453-
*/
432+
//执行run方法
454433
Method* run = self->threadObj->clazz->vtable[gDvm.voffJavaLangThread_run];
434+
455435
JValue unused;
456436
ALOGV("threadid=%d: calling run()", self->threadId);
457437
assert(strcmp(run->name, "run") == 0);
458438
dvmCallMethod(self, run, self->threadObj, &unused);
459439
ALOGV("threadid=%d: exiting", self->threadId);
460-
/*
461-
* Remove the thread from various lists, report its death, and free
462-
* its resources.
463-
*/
440+
441+
//移出线程并释放资源
464442
dvmDetachCurrentThread();
465443
return NULL;
466444
}
467445
468-
/*
469-
* Finish initialization of a Thread struct.
470-
* This must be called while executing in the new thread, but before the
471-
* thread is added to the thread list.
472-
* NOTE: The threadListLock must be held by the caller (needed for
473-
* assignThreadId()).
474-
*/
446+
//Finish initialization of a Thread struct.
475447
static bool prepareThread(Thread* thread){
476448
assignThreadId(thread);
477449
thread->handle = pthread_self();
478450
thread->systemTid = dvmGetSysThreadId();
479451
setThreadSelf(thread);
480452
return true;
481453
}
482-
/*
483-
* Explore our sense of self. Stuffs the thread pointer into TLS.
484-
*/
454+
455+
//Explore our sense of self. Stuffs the thread pointer into TLS.
485456
static void setThreadSelf(Thread* thread){
486457
int cc;
487458
cc = pthread_setspecific(gDvm.pthreadKeySelf, thread);

command/lijunhuayc/resource/command/ClientRole.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@
99
public class ClientRole {
1010

1111
/**
12-
* @Description: 组装操作
12+
* @Description:
1313
* @author (ljh) @date 2015-3-16 上午11:13:06
1414
* @return void
1515
*/
1616
public void assembleAction() {
1717
ReceiverRole receiverRole1 = new ReceiverRole();//创建一个命令接收者
1818
Command command1 = new ConcreteCommandImpl1(receiverRole1);//创建一个命令的具体实现对象,并指定命令接收者
19+
Command command2 = new ConcreteCommandImpl2(receiverRole1);
1920

20-
ReceiverRole receiverRole2 = new ReceiverRole(new PeopleBean(9999, "神仙"));
21-
Command command2 = new ConcreteCommandImpl2(receiverRole2);
22-
23-
//PS:command1 修改people为(18, "西北狼")
24-
//PS:command2 修改people为(35, "lijunhuayc")
25-
21+
//PS:command1 修改people 年龄
22+
//PS:command2 修改people 姓名
23+
//PS:command23修改people 年龄和姓名
24+
2625
InvokerRole invokerRole = new InvokerRole();//创建一个命令调用者
2726
invokerRole.setCommand1(command1);//为调用者指定命令对象1
2827
invokerRole.setCommand2(command2);//为调用者指定命令对象2
29-
invokerRole.invoke(null);//发起调用命令请求
30-
31-
ReceiverRole receiverRole3 = new ReceiverRole(new PeopleBean(9999, "神仙"));
32-
Command command3 = new ConcreteCommandImpl3(receiverRole2);
33-
invokerRole.invoke(command3);//发起调用命令请求
28+
invokerRole.invoke(0);//发起调用命令请求
29+
invokerRole.invoke(1);//发起调用命令请求
3430

3531
}
3632

command/lijunhuayc/resource/command/ConcreteCommandImpl1.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.command;
22

33
/**
4-
* @Desc: 命令接口的实现类[具体命令角色]
4+
* @Desc: 更新年龄的命令类[具体命令角色]
55
* @author ljh
66
* @date 2015-3-16 上午11:04:51
77
*/
@@ -17,19 +17,17 @@ public void execute() {
1717
/*
1818
* 可以加入命令排队等等,未执行的命令支持redo操作
1919
*/
20-
receiverRole1.opAction(18, "西北狼");//执行具体的命令操作
20+
receiverRole1.opActionUpdateAge(1001);//执行具体的命令操作
2121
}
2222

2323
@Override
2424
public void undo() {
25-
receiverRole1.rollBack();//执行具体的撤销回滚操作
25+
receiverRole1.rollBackAge();//执行具体的撤销回滚操作
2626
}
2727

2828
@Override
2929
public void redo() {
3030
//在命令执行前可以修改命令的执行
3131
}
3232

33-
34-
3533
}

command/lijunhuayc/resource/command/ConcreteCommandImpl2.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.command;
22

33
/**
4-
* @Desc: 命令接口的实现类[具体命令角色]
4+
* @Desc: 更新姓名的命令类[具体命令角色]
55
* @author ljh
66
* @date 2015-3-16 上午11:04:51
77
*/
@@ -17,19 +17,17 @@ public void execute() {
1717
/*
1818
* 可以加入命令排队等等,未执行的命令支持redo操作
1919
*/
20-
receiverRole1.opAction(35, "lijunhuayc");//执行具体的命令操作
20+
receiverRole1.opActionUpdateName("lijunhuayc");//执行具体的命令操作
2121
}
2222

2323
@Override
2424
public void undo() {
25-
receiverRole1.rollBack();//执行具体的撤销回滚操作
25+
receiverRole1.rollBackName();//执行具体的撤销回滚操作
2626
}
2727

2828
@Override
2929
public void redo() {
3030
//在命令执行前可以修改命令的执行
3131
}
3232

33-
34-
3533
}

command/lijunhuayc/resource/command/InvokerRole.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ public void setCommand2(Command command2) {
1818
this.command2 = command2;
1919
}
2020

21-
public void invoke(Command command) {
22-
//根据具体情况选择执行某些命令
23-
if(null != command){
24-
command.execute();
25-
return;
21+
/**
22+
* @Description:
23+
* @author (ljh) @date 2015-3-16 下午1:40:54
24+
* @param args 0执行正常命令,1执行回滚命令
25+
* @return void
26+
*/
27+
public void invoke(int args) {
28+
//可以根据具体情况选择执行某些命令
29+
if(args == 0){
30+
command1.execute();
31+
command2.execute();
32+
}else if(args == 1){
33+
command1.undo();
34+
command2.undo();
2635
}
27-
command1.execute();
28-
command2.execute();
2936
}
3037

31-
32-
3338
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package com.command;
22

3-
43
public class MainTest {
5-
64
public static void main(String[] args) {
75
ClientRole client = new ClientRole();
86
client.assembleAction();
9-
107
}
11-
128
}

command/lijunhuayc/resource/command/PeopleBean.java

+22-15
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,47 @@
88
public class PeopleBean {
99
private int age = -1; //年龄
1010
private String name = "NULL"; //姓名
11-
1211
public PeopleBean() {
1312
}
1413
public PeopleBean(int age, String name) {
1514
this.age = age;
1615
this.name = name;
1716
}
18-
1917
public void update(int age, String name) {
2018
this.age = age;
2119
this.name = name;
2220
}
23-
21+
public void update(int age) {
22+
this.age = age;
23+
}
24+
public void update(String name) {
25+
this.name = name;
26+
}
2427
/**
2528
* @return 返回一个PeopleBean的克隆对象
2629
*/
2730
protected PeopleBean clone(){
2831
return new PeopleBean(age, name);
2932
}
33+
@Override
34+
public String toString() {
35+
return " 【年龄:" + age + "\t姓名:" + name + "】";
36+
}
3037

31-
/**
32-
* @Description: 还原操作
33-
* @author (ljh) @date 2015-3-16 上午11:45:23
34-
* @param cache
35-
* @return void
36-
*/
37-
protected void undo(PeopleBean cache) {
38-
this.age = cache.age;
39-
this.name = cache.name;
38+
public int getAge() {
39+
return age;
4040
}
4141

42-
@Override
43-
public String toString() {
44-
return " 【姓名:" + name + "\t年龄:" + age + "】\n";
42+
public void setAge(int age) {
43+
this.age = age;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
4552
}
4653

4754
}

command/lijunhuayc/resource/command/ReceiverRole.java

+18-7
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,31 @@ public ReceiverRole(PeopleBean people) {
2323
* @author (ljh) @date 2015-3-16 上午11:07:32
2424
* @return void
2525
*/
26-
public void opAction(int age, String name) {
27-
peopleCache = people.clone();//缓存people
28-
people.update(age, name);
29-
System.out.println("执行命令后:"+people.toString());
26+
//修改年龄
27+
public void opActionUpdateAge(int age) {
28+
System.out.println("执行命令前:"+people.toString());
29+
this.people.update(age);
30+
System.out.println("执行命令后:"+people.toString()+"\n");
31+
}
32+
//修改姓名
33+
public void opActionUpdateName(String name) {
34+
System.out.println("执行命令前:"+people.toString());
35+
this.people.update(name);
36+
System.out.println("执行命令后:"+people.toString()+"\n");
3037
}
3138

3239
/**
3340
* @Description: 回滚操作,用于撤销opAction执行的改变
3441
* @author (ljh) @date 2015-3-16 上午11:34:41
3542
* @return void
3643
*/
37-
public void rollBack() {
38-
people.undo(peopleCache);
39-
System.out.println("命令回滚后:"+people.toString());
44+
public void rollBackAge() {
45+
people.setAge(peopleCache.getAge());
46+
System.out.println("命令回滚后:"+people.toString()+"\n");
47+
}
48+
public void rollBackName() {
49+
people.setName(peopleCache.getName());
50+
System.out.println("命令回滚后:"+people.toString()+"\n");
4051
}
4152

4253
}

0 commit comments

Comments
 (0)