1、 实例概况
电子商务网站常有这样的功能:发送消息通知,比如订货发货通知等,从业务上看,消息分为普通消息、加急消息和特急消息多种不同的消息类型,其业务处理是不一样的,比如加急消息是在消息上添加加急标记,而特急消息是添加了加急标记外,还会做一条催促的记录,多久不完成会继续催促。从发送消息的手段上看,又有系统内消息、手机短信消息、邮件等。请使用合适的设计模式进行设计,绘制所选择模式的结构图,并绘制该案例的类图,并编写代码实现并进行演示。
2、所用模式结构视图
3、实例类图
4、实例实现代码
4.1实现类接口ZC32Type(消息类型类)
public interface ZC32Type {
void besend(String MeansType,String counts);
}
4.11具体实现类ZC32CommonNews(普通消息类)
public class ZC32CommonNews implements ZC32Type{
public void besend(String MeansType,String counts) {
System.out.println( MeansType+"普通消息"+counts+".");
}
}
4.12具体实现类ZC32UNews(加急消息类)
public class ZC32UNews implements ZC32Type{
public void besend(String MeansType,String counts) {
System.out.println( MeansType+"加急消息"+counts+".");
}
}
4.13具体实现类ZC32EuNews(特急消息类)
public class ZC32EuNews implements ZC32Type{
public void besend(String MeansType,String counts) {
System.out.println( MeansType+"特急消息"+counts+".");
}
}
4.2抽象类ZC32Means(发送消息手段类)
public abstract class ZC32Means {
protected ZC32Type type;
public void setType(ZC32Type type) {
this.type=type;
}
public abstract void send(String counts);
}
4.21扩充抽象类ZC32SystemNews(系统内消息类)
public class ZC32SystemNews extends ZC32Means{
public void send(String counts)
{
String MeansType="系统内消息";
this.type.besend(MeansType, counts);
}
}
4.22扩充抽象类ZC32PhoneNews(手机短信消息类)
public class ZC32PhoneNews extends ZC32Means{
public void send(String counts)
{
String MeansType="手机短信消息";
this.type.besend(MeansType, counts);
}
}
4.23扩充抽象类ZC32MailNews(邮件消息类)
public class ZC32MailNews extends ZC32Means{
public void send(String counts)
{
String MeansType="邮件消息";
this.type.besend(MeansType, counts);
}
}
4.3客户端测试类ZC32Client
public class ZC32Client {
public static void main(String args[]) {
ZC32Type type1 =new ZC32EuNews();
ZC32Means means1=new ZC32MailNews();
means1.setType(type1);
means1.send("一条");
ZC32Type type2 =new ZC32UNews();
ZC32Means means2=new ZC32PhoneNews();
means2.setType(type2);
means2.send("五条");
ZC32Type type3 =new ZC32CommonNews();
ZC32Means means3=new ZC32SystemNews();
means3.setType(type3);
means3.send("八条");
}
}