经典软件体系结构风格(四)

来源:互联网 发布:net java做什么用 编辑:程序博客网 时间:2024/06/06 03:39

 层次风格

#层次系统

–在层次系统中,系统被组织成若干个层次,每个层次由一系列组件组成

–下层组件向上层组件提供服务

–上层组件被看作是下层组件的客户

#系统中的每一层都要承担两个角色。

–首先,它要为结构中的上层提供服务;

–其次,它要作为结构中下面层次的客户,调用下层提供的功能函数。

–最高层和最低层例外。

 基本组件:各层次内部包含的组件

 连接件:层间的交互协议

 拓扑结构:分层

 拓扑约束:对相邻层间交互的约束

  –集中式部署(Mainframe)

  –分布式部署(Distributed)

#优点:

–支持基于抽象程度递增的系统设计,有利于设计者对一个复杂系统进行分解;

–局部依赖性,因为每一层至多和相邻的上下层交互,因此功能的改变通常影响相邻的上下层;

–可复用性,如果某独立层保证了功能的完整性并且提供了文档化的接口,便可在多个语境中复用。

–可替换性,只要提供的服务接口定义不变,同一层的不同实现可以交换使用。这样,就可以定义一组标准的接口,而允许各种不同的实现方法。

–对标准化的支持。清晰定义并且广泛接受的抽象层次能够促进实现标准化的任务和接口开发,同样接口的不同实现能够互换使用。

–可测试性。具有定义明确的层接口以及交换层接口的各个实现的能力提高了可测试性。

#缺点:

并不是每个系统都可以很容易地划分为分层的模式,甚至即使一个系统的逻辑结构是层次化的,出于对系统性能的考虑,系统设计师不得不把一些低级或高级的功能综合起来;

#效率的降低:

–由分层风格构成的系统,运行效率往往低于整体结构。

–在上层中的服务如果有很多依赖于最底层,则相关的数据必须通过一些中间层的若干次转化,才能传到;

  很难找到合适的、正确的层次抽象方法:

–层数太少,分层不能完全发挥这种风格的可复用性、可更改性和可移植性上的潜力。

–层数过多,则引入不必要的复杂性和层间隔离冗余以及层间传输开销。

–目前,没有可行的广为人们所认可的层粒度的确定和层任务的分配方法。


程序:



#第一层:图形用户界面层
 ResultVerification.java
package com.main;public class ResultVerification{     static boolean flag = true;     public static String isResultCorrect(int[] arr){     for(int k=0; k<arr.length-1; k++){         if(arr[k] > arr[k+1]){             flag=false;             System.out.println("error  "+ k);             //break;             return "存在错误!";         }     }         return "没有错误!";      }}

TestingGUI.java
package com.main;import javax.swing.JPanel;import java.awt.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import com.sun.java.swing.plaf.windows.*;import com.testcase.Testcase;import com.testcase.TestcaseInsertion;public class TestingGUI extends JPanel{   private JTextArea txtTestInfo, txtTestcase;   private JLabel lblTestcases;   private JPanel buttonPanel;   private JComboBox cmbTestcases;   //private static final String CASE_BUBBLE= "TC1-Test Bubble Sort";   //private static final String CASE_HEAP= "TC2-Test Heap Sort";   private static final String CASE_INSERTION= "插入排序";   private static final String EXECUTE = "执行";   private static final String EXIT = "退出";   public TestingGUI(){  txtTestInfo=new JTextArea("测试输出的结果将展示在这里\n", 6, 20);  txtTestInfo.setLineWrap(true);  txtTestcase = new JTextArea("测试用例得到结果如下:\n", 4, 15);  txtTestcase.setLineWrap(true);      buildUpScrollGUI();   }   private void buildUpScrollGUI(){      setUpButtonPanel();  JScrollPane btnPane = new JScrollPane(buttonPanel);  JScrollPane textPane = new JScrollPane(txtTestcase);  textPane.setMinimumSize(new Dimension(250, 150));  JScrollPane testDataPane = new JScrollPane(txtTestInfo);  JSplitPane upSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);  upSplitPane.setLeftComponent(btnPane);  upSplitPane.setRightComponent(testDataPane);  JScrollPane downPane = new JScrollPane(textPane);  Dimension minimumSize = new Dimension(130, 100);  btnPane.setMinimumSize(minimumSize);  textPane.setMinimumSize(new Dimension(100, 100));  upSplitPane.setDividerLocation(270);  upSplitPane.setPreferredSize(new Dimension(500, 300));  JSplitPane bigSplitPane=new JSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, downPane);  bigSplitPane.setDividerLocation(190);  add(bigSplitPane);  setSize(new Dimension(500, 400));      setVisible(true);  }   private void setUpButtonPanel(){  lblTestcases = new JLabel("你要测试的算法是:");  cmbTestcases = new JComboBox();      //cmbTestcases.addItem(CASE_BUBBLE);     // cmbTestcases.addItem(CASE_HEAP);      cmbTestcases.addItem(CASE_INSERTION);     //Create the open button     JButton executeBtn = new JButton(EXECUTE);     executeBtn.setMnemonic(KeyEvent.VK_S);     JButton exitButton = new JButton(EXIT);     exitButton.setMnemonic(KeyEvent.VK_X);     BtnListener objButtonHandler = new BtnListener();     // add action Listener     executeBtn.addActionListener(objButtonHandler);     exitButton.addActionListener(objButtonHandler);     buttonPanel = new JPanel();     GridBagLayout gridbag = new GridBagLayout();     buttonPanel.setLayout(gridbag);     GridBagConstraints gbc = new GridBagConstraints();     buttonPanel.add(lblTestcases);     buttonPanel.add(cmbTestcases);     buttonPanel.add(executeBtn);     buttonPanel.add(exitButton);     gbc.insets.top = 5;     gbc.insets.bottom = 5;     gbc.insets.left = 5;     gbc.insets.right = 5;     gbc.anchor = GridBagConstraints.EAST;     gbc.gridx = 0;     gbc.gridy = 0;     gridbag.setConstraints(lblTestcases, gbc);     gbc.anchor = GridBagConstraints.WEST;     gbc.gridx = 1;     gbc.gridy = 0;     gridbag.setConstraints(cmbTestcases, gbc);     gbc.anchor = GridBagConstraints.EAST;     gbc.insets.left = 2;     gbc.insets.right = 2;     gbc.insets.top = 25;     gbc.anchor = GridBagConstraints.EAST;     gbc.gridx = 0;     gbc.gridy = 7;     gridbag.setConstraints(executeBtn, gbc);     gbc.anchor = GridBagConstraints.WEST;     gbc.gridx = 1;     gbc.gridy = 7;     gridbag.setConstraints(exitButton, gbc);  }  public void showTestInfo(int[] str ){  txtTestInfo.setText("");  for(int n=0; n< str.length; n++)        txtTestInfo.append(""+str[n]+" ");  }  public void showErrors(String err){ txtTestcase.append(err+"\n");  }  public String getSelectedTestcase() {      return (String) cmbTestcases.getSelectedItem();  } class BtnListener implements ActionListener{    private Testcase test;    private String selectedTestcase;    public void actionPerformed(ActionEvent e){      String searchResult = null;      int[] output=null;      if (e.getActionCommand().equals(EXIT)){         System.exit(1);      }      if (e.getActionCommand().equals(EXECUTE)){ selectedTestcase = getSelectedTestcase(); if(selectedTestcase.equals(CASE_INSERTION)) test = new TestcaseInsertion(); output = test.execute(3000); showTestInfo(output);         }          showErrors(selectedTestcase);          String result = ResultVerification.isResultCorrect(output );          showErrors("测试是否存在错误? = " +result);          long timeTaken = test.getTimeTaken();          showErrors("测试时间  = " + timeTaken+"\n");   } } // End of class BtnListener private static void createAndShowGUI(){     JFrame.setDefaultLookAndFeelDecorated(true);     JFrame frame = new JFrame("算法测试软件");     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     TestingGUI newContentPane = new TestingGUI();     newContentPane.setOpaque(true);     frame.setContentPane(newContentPane);     frame.pack();     frame.setVisible(true);  }  static public void main(String argv[]) { javax.swing.SwingUtilities.invokeLater(new Runnable() {    public void run() {   createAndShowGUI();}        });  }}

 #第二层:测试案例层,包括软件测试工程师所编写的测试案例
Context.java
package com.testcase;import com.sortAlgorithm.SortAlgorithm;public class Context { SortAlgorithm alg;public Context(SortAlgorithm alg) {super();this.alg = alg;}public int[] sortIntArray(int[] a) {return this.alg.sort(a);} }

Testcase.java
package com.testcase;public interface Testcase {public  abstract  int[]execute(int len);    public  abstract  long getTimeTaken();}

IntegerArrGenerator.java
package com.testcase;import java.util.Random;public class IntegerArrGenerator{    public static int[] generateInput(int len){ int[] input= new int[len]; Random r = new Random();         for(int m=0; m< len; m++){ input[m] = r.nextInt(len);     }         return input;    }}

TestcaseInsertion.java
package com.testcase;import com.sortAlgorithm.InsertSort;import com.sortAlgorithm.SortAlgorithm;public class TestcaseInsertion implements Testcase {    private long startTime;    private long timeTaken=0;public int[] execute(int len) {// TODO Auto-generated method stubstartTime = System.currentTimeMillis();int []input = IntegerArrGenerator.generateInput(len);SortAlgorithm sa = new InsertSort();Context context = new Context(sa);int []intArray = context.sortIntArray(input);timeTaken = System.currentTimeMillis()-startTime;return intArray;}public long getTimeTaken() {// TODO Auto-generated method stubreturn timeTaken;}}

 #第三层:被测试软件层(排序算法)
SortAlgorithm.java
package com.sortAlgorithm;public interface SortAlgorithm {    int []sort(int []nums);}

InsertSort.java
package com.sortAlgorithm;public class InsertSort implements SortAlgorithm {    public int[] sort(int[] nums){       for (int i = 1; i < nums.length; i++){          int j = i;          int numToBeInserted = nums[i];          while ((j > 0) && (nums[j-1] > numToBeInserted) ) {             nums[j] = nums[j-1];             j--;          }          nums[j] = numToBeInserted;       }       return nums;   }}

#截图:




0 0