软件体系结构——工厂模式

(1)UML设计图

简单工厂模式UML:

 

 

普通工厂模式UML

 

 

抽象工厂模式UML

 

 

(2)核心实现代码

简单工厂模式:

public class Simple extends javax.swing.JFrame {

    private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        // TODO add your handling code here:

        String option = (String)chooseInsur.getSelectedItem();

        AutoInsurance ai = null;

        

        //再获取具体的保险理赔对象

        switch (option) {

            case "驾驶员身体受伤":

                ai = new BodyInjur();

                break;

            case "汽车损坏":

                ai = new CarInjur();

                break;

            case "人员伤亡":

                ai = new PersonInjur();

                break;

            case "多种事故":

                ai = new OtherAccident();

                break;

        }

                

        insurInfoText.setText(ai.getInsurInfo());

    }                                          

 

 

 

 

 

 

普通工厂模式:

public class Normal extends javax.swing.JFrame {

    private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        // TODO add your handling code here:

        String option = (String)chooseInsur.getSelectedItem();

        

 

        PolicyProducer pp = null;

 

        

        //再获取具体的保险理赔对象

        switch (option) {

            case "驾驶员身体受伤":

                pp = new BodyPolicy();

                break;

            case "汽车损坏":

                pp = new CarPolicy();

                break;

            case "人员伤亡":

                pp = new PersonPolicy();

                break;

            case "多种事故":

                pp = new OtherPolicy();

                break;

        }

                AutoInsurance ai = pp.getInsurObj();

        insurInfoText.setText(ai.getInsurInfo());

    }                                    

 

 

      

 

 

抽象工厂模式:

 

public class Abstract extends javax.swing.JFrame {                  

 

    private void viewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        // TODO add your handling code here:

        String company = (String)chooseCompany.getSelectedItem();

        String option = (String)chooseInsur.getSelectedItem();

        

        AutoInsurance ai = null;

        

        //先获取具体的保险公司对象

        Company c = Company.getCompany(company);

        

        //再获取具体的保险理赔对象

        switch (option) {

            case "驾驶员身体受伤":

                ai = c.getBodyInjur();

                break;

            case "汽车损坏":

                ai = c.getCarInjur();

                break;

            case "人员伤亡":

                ai = c.getPersonInjur();

                break;

            case "多种事故":

                ai = c.getOtherInjur();

                break;

        }

        

        insurInfoText.setText(company+"\n"+ai.getInsurInfo());

    }                                          

 

posted @ 2014-05-31 23:16  漫游者Reynold  阅读(654)  评论(0编辑  收藏  举报