package
state;
import
context.Context;
public
class
DesignTemplateState
implements
State
{
public
String toString()
{
return
"
设计模板状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
InspectTemplateState());
}
public
void
previousState(Context c)
{
}
}
package
state;
import
context.Context;
public
class
InspectTemplateState
implements
State
{
public
String toString()
{
return
"
审批模板状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
FillDataState());
}
public
void
previousState(Context c)
{
c.setState(
new
ReworkTemplateState());
}
}
package
state;
import
context.Context;
public
class
ReworkTemplateState
implements
State
{
public
String toString()
{
return
"
修改模块状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
InspectTemplateState());
}
public
void
previousState(Context c)
{
c.setState(
new
InspectTemplateState());
}
}
package
state;
import
context.Context;
public
class
FillDataState
implements
State
{
public
String toString()
{
return
"
填写数据状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
InspectDataState());
}
public
void
previousState(Context c)
{
}
}
package
state;
import
context.Context;
public
class
InspectDataState
implements
State
{
public
String toString()
{
return
"
审批数据状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
DownloadData());
}
public
void
previousState(Context c)
{
c.setState(
new
ReworkDataState());
}
}
package
state;
import
context.Context;
public
class
ReworkDataState
implements
State
{
public
String toString()
{
return
"
修改数据状态
"
;
}
public
void
nextState(Context c)
{
c.setState(
new
InspectDataState());
}
public
void
previousState(Context c)
{
c.setState(
new
InspectDataState());
}
}
package
state;
import
context.Context;
public
class
DownloadData
implements
State
{
public
String toString()
{
return
"
下载数据状态
"
;
}
public
void
nextState(Context c)
{
}
public
void
previousState(Context c)
{
}
}
package
context;
import
state.State;
public
interface
Context
{
public
void
setState(State state);
public
void
pass();
public
void
noPass();
public
State getState();
}
package
context;
import
state.State;
public
class
NewContext
implements
Context
{
State state;
public
void
setState(State state)
{
this
.state
=
state;
}
public
void
pass()
{
state.nextState(
this
);
}
public
void
noPass()
{
state.previousState(
this
);
}
public
State getState()
{
return
state;
}
}
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
//
import java.util.StringTokenizer;
import
state.DesignTemplateState;
import
state.State;
import
context.Context;
import
context.NewContext;
public
class
TestState
{
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
{
Context context
=
new
NewContext();
State dts
=
new
DesignTemplateState();
context.setState(dts);
InputStreamReader is
=
new
InputStreamReader(System.in);
BufferedReader br
=
new
BufferedReader(is);
//
StringTokenizer st;
while
(
true
)
{
try
{
System.out.print(
"
当前状态是:
"
);
System.out.println(context.getState());
System.out.println(
"
进行下一作业“N”,还是回上一作业“P”,“Q”退出:
"
);
System.out.flush();
String myline
=
br.readLine();
if
(myline.equals(
"
N
"
))
context.pass();
else
if
(myline.equals(
"
P
"
))
context.noPass();
else
if
(myline.equals(
"
Q
"
))
break
;
//
st=new StringTokenizer(myline);
//
i=Integer.parseInt(st.nextToken());
//
if(i<0)
//
break;
//
System.out.println("got:"+i);
}
catch
(IOException ioe)
{
System.out.println(
"
IO error:
"
+
ioe);
}
}
}
}