设计模式学习第十二天

    xiaoxiao2026-04-24  10

    学习设计模式了解了下AWT,学习AWT了解了下Swing,然后接触了点SWT。

    总之,CS架构搞这个确实麻烦许多。看看DELPHI,PB多爽。

    罢了,罢了,eclipse还是NetBeans好用些,主要是学习设计模式,而且这个话题已经拖着够长时间了。

    学习效率确实太低,尤其是工作、生活之余。OK,开始......

    先工厂模式实现下收银软件模式。

    父类:

    public abstract class CashSuper {     public abstract double acceptCash(double money);     public void test() {           String ss;           ss = "me 么";     } }

    子类:

    public class CashNormal extends CashSuper{     /**      *      * @param money      * @return      */     @Override     public double  acceptCash(double money) {         return money;     }      }

    public class CashRebate extends CashSuper{     private  double moneyRebate = 0.0;          public CashRebate(String moneyRebate) {       this.moneyRebate = Double.parseDouble(moneyRebate);     }     @Override     public double acceptCash(double money) {             return money * moneyRebate;           }      }

    public class CashReturn extends CashSuper {         private double moneyCondition = 0.0;     private double moneyReturn = 0.0;          public CashReturn (String moneyCondition, String moneyReturn) {       this.moneyCondition = Double.parseDouble(moneyCondition);       this.moneyReturn = Double.parseDouble(moneyReturn);     }     @Override     public double acceptCash(double money) {        double result = money;        if(money >= moneyCondition)            result = money - Math.floor(money / moneyCondition) * moneyReturn;         return result;     }      }

    工厂:

    public class CashFactory {     public static CashSuper createCashAccept(Integer type) {         CashSuper cs = null;         switch(type) {              case 0 :                cs = new CashNormal();               break;           case 1 :                CashReturn cr1 = new CashReturn("300","100");                cs = cr1;               break;           case 2:               CashRebate cr2 = new CashRebate("0.8");               cs = cr2;               break;           default :               break;         }         return cs;     } }

     客户端调用:

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                                 // TODO add your handling code here:       DecimalFormat df = new DecimalFormat("#.####");       Double dj = Double.parseDouble(this.textField1.getText().toString().trim());       Double sl = Double.parseDouble(this.textField2.getText().toString().trim());              Double hj = 0.0;       CashSuper cs = CashFactory.createCashAccept(this.xx.getSelectedIndex());       hj = cs.acceptCash(dj * sl);              sum = sum + hj;              show = "单价:" + this.textField1.getText() + "数量:" + this.textField2.getText() + "价格为:" + df.format(hj).toString() + "\n"+show ;       this.textArea1.setText(show);        this.label4.setText(df.format(sum).toString());     }      

    转载请注明原文地址: https://ju.6miu.com/read-1309194.html
    最新回复(0)