package
?state;

import
?context.Context;


public
?
class
?DesignTemplateState?
implements
?State?
{


????
public
?String?toString()?
{
????????
return
?
"
設(shè)計(jì)模板狀態(tài)
"
;
????}
????
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
?
"
審批模板狀態(tài)
"
;
????}
????
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
?
"
修改模塊狀態(tài)
"
;
????}
????
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
?
"
填寫(xiě)數(shù)據(jù)狀態(tài)
"
;
????}
????
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
?
"
審批數(shù)據(jù)狀態(tài)
"
;
????}
????
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
?
"
修改數(shù)據(jù)狀態(tài)
"
;
????}
????
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
?
"
下載數(shù)據(jù)狀態(tài)
"
;
????}
????
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(
"
當(dāng)前狀態(tài)是:
"
);?
????????????????System.out.println(context.getState());
????????????????System.out.println(
"
進(jìn)行下一作業(yè)“N”,還是回上一作業(yè)“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);?
????????????}
????????????
????????}
????}
}