我考虑了一种方法,不知如何?
public class Transfer; //运输类
public class Customer; //客户类
public class Notification; //通知类public class Shipment extends Transfer; //发货
public class Delivery extends Transfer; //收货public class Consignor extends Customer; //发货人
public class Consignee extends Customer; //收货人public class SmsNotification extends Notification; //短信通知类
public class VoiceNotification extends Notification; //语音通知类Transfer shipment = new Shipment();
Customer consignor = new Consignor(shipment);Notification smsNotification = SmsNotification();
smsNotification.setTransfer(shipment);
smsNotification.setCustomer(consignor);
smsNotification.send();
public class Transfer; //运输类
public class Customer; //客户类
public class Notification; //通知类public class Shipment extends Transfer; //发货
public class Delivery extends Transfer; //收货public class Consignor extends Customer; //发货人
public class Consignee extends Customer; //收货人public class SmsNotification extends Notification; //短信通知类
public class VoiceNotification extends Notification; //语音通知类Transfer shipment = new Shipment();
Customer consignor = new Consignor(shipment);Notification smsNotification = SmsNotification();
smsNotification.setTransfer(shipment);
smsNotification.setCustomer(consignor);
smsNotification.send();
解决方案 »
- jacob对word进行操作的问题
- 关于hibernate二级缓存的疑问
- struts2如何从action跳到带参数的action
- Validator启用自定义规则客户端验证的问题以及ValidateWhen的资料
- tomcat启动问题。
- 请到http://220.114.108.6:8080/jxc添加条记录
- 类型转换简单问题
- JDBC For SQLServer我安装了,也设置好了但下面这段代码就是连不上数据库,请大家给帮忙看看。。。谢谢了!
- 请问,怎样可以将JAVA文件编译成可执行文件?
- tree结构封装问题
- 请教.invoke如何注入继承类的属性?高手来指点吧
- struts2+ajaxSubmit上传文件在IE9中不兼容
具体代码:abstract class Action
{
public static final int SEND = 1; // 收货
public static final int ACCEPT = 2; // 提货
public void sendInfo(int flag)
{
switch (flag)
{
case 1:
this.smsNotice(); // 收货人短信通知
this.voiceNotice(); // 收货人语音通知
break; case 2:
this.smsNotice(); // 提货人短信通知
this.voiceNotice(); // 提货人语音通知
break;
}
}
public abstract void voiceNotice(); //语音通知
public abstract void smsNotice(); //短信通知
}
class Consignee extends Action// 收貨人
{ @Override
public void smsNotice()
{
System.out.println("发送收货人短信。。");
} @Override
public void voiceNotice()
{
System.out.println("发送收货人语音。。");
}
}class DiHuoRen extends Action// 提貨人
{ @Override
public void smsNotice()
{
System.out.println("发送提货人短信。。");
} @Override
public void voiceNotice()
{
System.out.println("发送提货人语音。。");
}
}public class Test
{
public static void main(String []args)
{
Action cg = new Consignee();
cg.sendInfo(1); //发送信息 System.out.println("****************");
Action dr = new DiHuoRen();
dr.sendInfo(2); //发送信息
}
}
接下来贴代码:
先贴接口代码:
package com.djk.design.test;/**
* 命令接口
* @author djk
*
*/
public interface ICommond
{
void execute();
}
package com.djk.design.test;/**
* 通知形式
* @author djk
*
*/
public interface InfocationNotice
{
void say();
}
接下来是调用者: package com.djk.design.test;/**
* 调用者 调用命令的人
* @author djk
*
*/
public class Invoker
{
/**
* 发货
*/
private ICommond sendGood;
/**
* 提货
*/
private ICommond acceptGood;
/**
*
*构造器 放入发货命令和提货命令
* @param sendGood
* @param acceptGood
*/
public Invoker(ICommond sendGood,ICommond acceptGood)
{
this.sendGood = sendGood;
this.acceptGood = acceptGood;
}
/**
* 执行发货
*
*/
public void sendGood()
{
sendGood.execute();
}
/**
* 执行提货
*/
public void acaeptGood()
{
acceptGood.execute();
}
}具体的发货命令和提货命令:package com.djk.design.test;/**
* 发货命令的实现类
* @author djk
*
*/
public class SendCommond implements ICommond
{
private InfocationNotice infocationNotice;
public SendCommond(InfocationNotice infocationNotice)
{
this.infocationNotice = infocationNotice;
}
public InfocationNotice getInfocationNotice()
{
return infocationNotice;
} public void setInfocationNotice(InfocationNotice infocationNotice)
{
this.infocationNotice = infocationNotice;
} @Override
public void execute()
{
infocationNotice.say();
}}
package com.djk.design.test;/**
* 接收命令
* @author djk
*
*/
public class AcceptCommond implements ICommond
{
private InfocationNotice infocationNotice;
public AcceptCommond(InfocationNotice infocationNotice)
{
this.infocationNotice = infocationNotice;
}
public InfocationNotice getInfocationNotice()
{
return infocationNotice;
} public void setInfocationNotice(InfocationNotice infocationNotice)
{
this.infocationNotice = infocationNotice;
}
@Override
public void execute()
{
infocationNotice.say();
}
}接下来是具体的命令执行者,命令执行者都要实现通知接口来确定自己是那种通知方式,目前是2个短信和语音后期如果增加一种通知方式则只要再增加一个通知接口的实现类(就是命令的真正执行者就OK)
package com.djk.design.test;/**
* 提货人(短信方式)
* @author djk
*
*/
public class SMSAccept implements InfocationNotice
{ @Override
public void say()
{
System.out.println("发送提货短信");
}}package com.djk.design.test;/**
* 发货人(短信方式)
* @author djk
*
*/
public class SMSSend implements InfocationNotice
{ @Override
public void say()
{
System.out.println("发送发货短信");
}}
package com.djk.design.test;/**
* 提货人(语音)
* @author djk
*
*/
public class VoiceAccept implements InfocationNotice
{ @Override
public void say()
{
System.out.println("提货人 语音。");
}}
package com.djk.design.test;/**
* 发货人(短信提醒)
* @author djk
*
*/
public class VoiceSend implements InfocationNotice
{ @Override
public void say()
{
System.out.println("发送发货语音");
}}
最后提供测试类:package com.djk.design.test;/**
* 测试类
* @author djk
*
*/
public class Client
{
public static void main(String[] args)
{
//用短信发送的真正执行者
InfocationNotice sms = new SMSSend();
//发货命令
ICommond sendCommind = new SendCommond(sms);
sendCommind.execute();
//用语音发送的真正执行者
InfocationNotice voice = new VoiceSend();
//提货命令
ICommond acceptCommond = new AcceptCommond(voice);
acceptCommond.execute();
}
}
package com.djk.design.test;/**
* 测试类
* @author djk
*
*/
public class Client
{
public static void main(String[] args)
{
//用短信发送的真正执行者
InfocationNotice sms = new SMSSend();
//发货命令
ICommond sendCommind = new SendCommond(sms);
//用语音发送的真正执行者
InfocationNotice voice = new VoiceSend();
//提货命令
ICommond acceptCommond = new AcceptCommond(voice);
Invoker invoker = new Invoker(sendCommind,acceptCommond);
//提货
invoker.acaeptGood();
//发货
invoker.sendGood();
}
}