层次风格

层次风格

了解很多其它软件体系结构风格

层次系统

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

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

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

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

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

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

–最高层和最低层例外。

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

§连接件:层间的交互协议

§拓扑结构:分层

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

–集中式部署(Mainframe)

–分布式部署(Distributed)

长处:

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

系统进行分解。

–局部依赖性,由于每一层至多和相邻的上下层交互。因此功能

的改变通常影响相邻的上下层;

–可复用性,假设某独立层保证了功能的完整性而且提供了文档

化的接口。便可在多个语境中复用。

–可替换性,仅仅要提供的服务接口定义不变,同一层的不同实现

能够交换使用。这样,就能够定义一组标准的接口,而同意各

种不同的实现方法。

–对标准化的支持。清晰定义而且广泛接受的抽象层次可以促进

实现标准化的任务和接口开发,相同接口的不同实现可以互换

使用。

–可測试性。具有定义明白的层接口以及交换层接口的各个实现

的能力提高了可測试性。

缺点:

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

§效率的减少:

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

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

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

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

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

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

程序:

第一层为用户图形界面层

importjava.awt.*;

importjava.util.*;

importjavax.swing.*;

importjava.awt.event.*;

importcom.sun.java.swing.plaf.windows.*;

public class TestingGUI extendsJPanel{

  private JTextArea txtTestInfo, txtTestcase;

  private JLabel lblTestcases;

  private JPanel buttonPanel;

  private JComboBox cmbTestcases;

  private static final String CASE_BUBBLE= "TC1-Test BubbleSort";

  private static final String CASE_HEAP= "TC2-Test Heap Sort";

  private static final String CASE_INSERTION= "TC3-Test InsertionSort";

  private static final String EXECUTE = "Execute";

  private static final String EXIT = "Exit";

  public TestingGUI(){

         txtTestInfo=new JTextArea("Test outputfrom source shown here ", 6, 20);

         txtTestInfo.setLineWrap(true);

         txtTestcase = new JTextArea("Testcaseinfo and test validation shown here ", 4, 15);

         txtTestcase.setLineWrap(true);

      buildUpScrollGUI();

  }

  private void buildUpScrollGUI(){

      setUpButtonPanel();

         JScrollPane btnPane = new JScrollPane(buttonPanel);

         JScrollPane textPane = newJScrollPane(txtTestcase);

         textPane.setMinimumSize(new Dimension(250,150));

         JScrollPane testDataPane = newJScrollPane(txtTestInfo);

         JSplitPane upSplitPane = newJSplitPane(JSplitPane.HORIZONTAL_SPLIT);

         upSplitPane.setLeftComponent(btnPane);

         upSplitPane.setRightComponent(testDataPane);

         JScrollPane downPane = newJScrollPane(textPane);

         Dimension minimumSize = new Dimension(130,100);

         btnPane.setMinimumSize(minimumSize);

         textPane.setMinimumSize(new Dimension(100,100));

         upSplitPane.setDividerLocation(270);

         upSplitPane.setPreferredSize(newDimension(500, 300));

         JSplitPane bigSplitPane = newJSplitPane(JSplitPane.VERTICAL_SPLIT, upSplitPane, downPane);

         bigSplitPane.setDividerLocation(190);

         add(bigSplitPane);

         setSize(new Dimension(500, 400));

      setVisible(true);

 }

 private void setUpButtonPanel(){

         lblTestcases = new JLabel("TestCases:");

         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 = newBtnListener();

     // add action Listener

    executeBtn.addActionListener(objButtonHandler);

    exitButton.addActionListener(objButtonHandler);

     buttonPanel = new JPanel();

     GridBagLayout gridbag = newGridBagLayout();

     buttonPanel.setLayout(gridbag);

     GridBagConstraints gbc = newGridBagConstraints();

     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+" ");

 }

 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_BUBBLE))

                                      test = new TestcaseBubble();

                             else if(selectedTestcase.equals(CASE_HEAP))

                                      test = new TestcaseHeap();

                             elseif(selectedTestcase.equals(CASE_INSERTION))

                                      test = new TestcaseInsertion();

                             output = test.execute(3000);

                             showTestInfo(output);

         }

          showErrors(selectedTestcase);

          boolean result =ResultVerification.isResultCorrect(output );

          showErrors("No Error found =" +result);

          long timeTaken = test.getTimeTaken();

          showErrors("Testing Time takes =" + timeTaken+" ");

  }

 } // End of class BtnListener

 private static void createAndShowGUI(){

    JFrame.setDefaultLookAndFeelDecorated(true);

     JFrame frame = new JFrame("LayeredArchitecture- Software Testing");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     TestingGUI newContentPane = newTestingGUI();

     newContentPane.setOpaque(true);

     frame.setContentPane(newContentPane);

    frame.pack();

     frame.setVisible(true);

 }

 static public void main(String argv[]) {

        javax.swing.SwingUtilities.invokeLater(newRunnable() {

           public void run() {

                 createAndShowGUI();

              }

        });

 }

}

public class ResultVerification{

     static boolean flag = true;

     public static booleanisResultCorrect(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 flag;

      }

}

第二层为測试案例层,包含软件測试project师所编写的測试案例

publicinterface Testcase{

     public abstract  int[] execute(int len);

     public abstract  long getTimeTaken();

}

class Context {

    SortAlgorithm alg;

    // Constructor

    public Context(SortAlgorithm alg) {

        this.alg = alg;

    }

    public int[] sortIntArray(int[] a) {

        return this.alg.sort(a);

    }

}

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;

    }

}

import java.util.*;

public class TestcaseBubbleimplements Testcase{

        private long startTime;

        private long timeTaken=0;

     public int[] execute(int len) {

                startTime = System.currentTimeMillis();

                int[] input =IntegerArrGenerator.generateInput(len);

            SortAlgorithm sa = new BubbleSort();

            Context context = new Context(sa);

            int[] intArray = context.sortIntArray(input);

          timeTaken =System.currentTimeMillis() - startTime;

            return intArray;

    }

    public long getTimeTaken(){

                return        timeTaken;

       }

}

import java.util.*;

public class TestcaseHeapimplements Testcase{

     //static long time;

     private long startTime;

        private long timeTaken=0;

     public int[] execute(int len){

              startTime= System.currentTimeMillis();

              int[]input = IntegerArrGenerator.generateInput(len);

              SortAlgorithmsa = new HeapSort();

              Contextcontext = new Context(sa);

              int[]intArray = context.sortIntArray(input);

              timeTaken= System.currentTimeMillis()-startTime;

        return intArray;

    }

    public long getTimeTaken(){

                       return        timeTaken;

       }

}

import java.util.*;

public class TestcaseInsertionimplements Testcase{

     private long startTime;

        private long timeTaken=0;

     public int[] execute(int len){

                  startTime = 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(){

                       return        timeTaken;

       }

}

第三层为被測试软件层(排序算法)

public interface SortAlgorithm {

     int[] sort(int[] nums);

}

public class BubbleSort implementsSortAlgorithm {

  public int[] sort(int[] nums){

      for(int i = nums.length; --i >= 0;)

         for(int j = 0; j < i; j++){

            if(nums[j] > nums[j + 1]){

               //exchange nums[j+1] withnums[j]

               int T =nums[j];

               nums[j] = nums[j + 1];

               nums[j + 1] = T;

            }

         }

      return nums;

  }

}

public class HeapSort implementsSortAlgorithm{

     public int[] sort(int nums[ ]) {

          for(int i=nums.length; i>1; i--){

               buildBinaryHeapTree(nums, i -1);

              swapLeadingNodeWithLastNode(nums, i - 1);

          }

          return nums;

     }

     public void buildBinaryHeapTree(intarray[], int arrayBound){

              int leftChild, rightChild, biggerChild,temp;

              int root = (arrayBound-1)/2;

           // Find the bigger child index

           for(int i=root; i>=0; i--) {

                 leftChild = (2*i)+1;

                 rightChild = (2*i)+2;

                 if((leftChild <= arrayBound)&& (rightChild <= arrayBound)){

                    if(array[rightChild] >=array[leftChild])

                        biggerChild =rightChild;

                    else

                       biggerChild = leftChild;

                 }

                else{

                    if(rightChild >arrayBound)

                       biggerChild = leftChild;

                   else

                      biggerChild = rightChild;

                }

               //swap the integer contained inthe bigger child index

               //with that in the currentparent node

               if(array[i] <array[biggerChild]){

                   temp = array[i];

                  array[i] =array[biggerChild];

                  array[biggerChild] = temp;

               }

          }

           return;

       }

       publicstatic void swapLeadingNodeWithLastNode(int array[], int arrayBound){

             int temp;

             temp = array[0];

         array[0] = array[arrayBound];

         array[arrayBound] = temp;

          return;

       }

}

public class InsertSortimplements 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;

  }

}

原文地址:https://www.cnblogs.com/mfmdaoyou/p/6938205.html