发送消息 现在我们要实现这样一个功能:发送消息。从业务上看,消息又分成普通消息、加急消息和特急消息多种,不同的消息类型,业务功能处理是不一样的,比如加急消息是在消息上添加“加急”字样,而特急消息除了添加特急外,还会做一条催促的记录,多久不完成会继续催促。从发送消息的手段上看,又有系统内短消息、手机短消息、邮件等等。现在要实现这样的发送提示消息的功能,该如何实现呢?
UML图:
代码:
package xaioxifasong;
public abstract class AbstractMeesage {
protected MessageMethod impl;
public AbstractMeesage(MessageMethod impl)
{this.impl=impl;
}
public abstract void sendMessage(String message,String user);
}
package xaioxifasong;
public interface MessageMethod {
public void send(String message,String user);
}
package xaioxifasong;
public class CommonMessage extends AbstractMeesage {
public CommonMessage(MessageMethod impl) {
super(impl);
}
@Override
public void sendMessage(String message, String user) {
impl.send(message,user);
}
}
package xaioxifasong;
public class UrgentMessage extends AbstractMeesage {
public UrgentMessage(MessageMethod impl) {
super(impl);
// TODO 自动生成的构造函数存根
}
@Override
public void sendMessage(String message, String user) {
message="加急"+message;
impl.send(message,user);
}
public Object watch(String messageid)
{return null;
}
}
package xaioxifasong;
public class SpecialMessage extends AbstractMeesage{
public SpecialMessage(MessageMethod impl) {
super(impl);
}
@Override
public void sendMessage(String message, String user) {
message="特急"+message;
impl.send(message,user);
}
public void hurry(String messageid)
{System.out.println("快点");
}
}
package xaioxifasong;
public class SMS implements MessageMethod {
@Override
public void send(String message, String user) {
System.out.println("使用站内消息发送方式发送消息"+message+"给"+user);
}
}
package xaioxifasong;
public class Email implements MessageMethod {
@Override
public void send(String message, String user) {
System.out.println("以邮件方式发送消息"+message+"给"+user);
}
}
package xaioxifasong;
public class MobileMeeage implements MessageMethod {
@Override
public void send(String message, String user) {
System.out.println("手机短消息方式发送信息"+message+"给"+user);
}
}
package xaioxifasong;
public class Test {
public static void main(String args[])
{MessageMethod impl=new SMS();
AbstractMeesage m=new CommonMessage(impl);
m.sendMessage("喝一杯茶", "小李");
MessageMethod impl2=new Email();
MessageMethod impl3=new MobileMeeage();
AbstractMeesage p=new UrgentMessage(impl2);
AbstractMeesage q=new SpecialMessage(impl3);
p.sendMessage("喝一杯茶", "小王");
q.sendMessage("喝一杯茶", "老张");
}
}