我考虑了一种方法,不知如何?
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();

解决方案 »

  1.   

    我觉得只要三个方法就行了,用模板模式可行.
    具体代码: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); //发送信息


    }

    }
      

  2.   

    呵呵 我用了命令模式写了下这个程序 先讲下思路 我首先就是把发货和提货作为一个命令,然后至于到底是哪种方式的发货或提货则由命令自己委托给真正的执行者(发货人和提货人)处理。呵呵 这样考虑到后期的通知方式的扩展性,不管你那种通知方式,只要实现了通知方式的接口就OK 原代码不需要改变。
    接下来贴代码:
    先贴接口代码:
    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();
        }
    }
      

  3.   

    额不好意思 测试类写错了 修正下
    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();
        }
    }
      

  4.   

    chengxu2011 的方法可能更接近我的业务模式,我再好好看看。