Presentation is loading. Please wait.

Presentation is loading. Please wait.

NCCUCS 軟工概論 Software Design: Modules (KWIC case study)

Similar presentations


Presentation on theme: "NCCUCS 軟工概論 Software Design: Modules (KWIC case study)"— Presentation transcript:

1 NCCUCS 軟工概論 Software Design: Modules (KWIC case study)
Prof. Chen, Kung April 21, 2014

2 Agenda Assignment 2: Graph ADT Modularity Decomposing into Modules
Interface and Implementtaion Coupling and Cohesion Decomposing into Modules The KWIC Example

3 Assignment 2: the Graph ADT
Problems Text answers Java code

4 Assignment 2: the Graph ADT and Path Finder
MapQuick represents street connectivity with a directed graph. Streets as Nodes Interconnections as Edges Path finder: shorted path Testing driver Test scripts JUnit

5 Assignment 2: Sample I/O
Testing Driver Test script Output

6 Given Code Skeleton

7 Your Repo to Submit 文字檔答案與程式碼,測試案例 Problem 2: c, d 不必寫
Reflection: c不必寫

8 Assignment 2 仔細閱讀題目,全部先都瀏覽一遍,再開始寫 有少數內容我們沒教,可略過 作答:
Graph class API: specification and implementation Test cases and test scripts Description of your work and thoughts Java code 加 “某某某ps3readme.txt” 說明一下你上傳的檔案

9 Testing Part to Handin

10 Modularization (模組化): Decomposing a System into modules
Tackling Design Complexity Modularization (模組化): Decomposing a System into modules

11 Tackling Design Complexity
How: Divide-and-Conquer But how to divide? What modules? 疱丁解牛?

12 The most important principle in (software) engineering
Separation of Interface and Implementation Abstraction:

13 Interface – so that parts can be changed independently
Body Head Interface Front Leg Read Leg

14 Properties of Modularization
What are good modularization? Properties of Modularization Cohesion and Coupling

15 Bad modularization 糾結在一起,不好切割 Tight Coupling

16 How Worse It Could Be! 牽一髮而動全身

17 Good Modularization Rule of Modularity: Write simple parts connected by clean interfaces.

18 Good Modularization 好分工

19 Modularity: Simple Parts
Cohesion: “The measure of the strength of functional relatedness of elements within a module” Each module should do one thing well. Aims for high cohesion. 不相干的不要放在一起 Reading: Basics of the Unix Philosophy

20 Cohesion Example public class MyClass { private int value1;
public int Method1() return value1 + value2; } public int Method2() return value1 - value2; // doesn't belong on this class? public int Method3() return value3 * 2; Cohesion Example

21 Connected by Clean Interface
Reduce the dependencies between modules. Dependency leads to coupling. Strives for low-coupling Depends on (needs, uses) A B 改了B可能就要改A

22 Degree of Coupling Uncoupled - no dependencies Loosely coupled -
some dependencies High coupling makes modifying parts of the system difficult, Highly coupled - many dependencies

23 (Hidden) Data Coupling
Undesirable data coupling may not be easy to notice. More discussion next time. public class HiddenCoupling { public bar someMethod(SomeType x) { AnotherType y = x.getY(); y.foo(); // blah; blah; ba; } B, X, internal representation of X, and Y are coupled

24 Modularity: Cohesion and Coupling
Effective modularization = max. cohesion within modules + min. coupling between modules

25 Modules and Testing Testing a module Testability: 好不好測試,尤其是寫程式測程式
Isolation and Mockup Objects

26 Software Modularization
As software is very flexible, software modularization may not be an easy task.

27 Customization or Made by Composition (more later)
沙堡 versus 樂高

28 如何切模組? Module granularity? 劃分的標準與原則?
Modularization 如何切模組? Module granularity? 劃分的標準與原則?

29 Module Decomposition Top-Down Approach Bottom-Up Approach Hybrid: 下上下上
Action (Verb) Oriented Bottom-Up Approach Object (Noun) Oriented Hybrid: 下上下上

30 Top-Down Design: Decomposition into Modules
Module A 分工合作 Module A1 Module C Module B Module A11 Module A12

31 Case Study The KWIC Example Key Word In Context (KWIC)
About modularization

32 A Classical Paper on Modularity
David Parnas “On the Criteria To Be Used in Decomposing Systems into Modules” Comm. ACM 15, 12 (Dec. 1972), Discusses “modularization” Describes two ways to modularize a program that generates KWIC (Key Word in Context) indices.

33 Exercise

34 The KWIC Program

35 Key Word in Context Input: sort Output: The C Programming Language
C Programming Language The Cat in the Hat The Hat The Cat in the Language The C Programming Programming Language The C The C Programming Language The Cat in the Hat in the Hat The Cat the Hat The Cat in Input: The C Programming Language The Cat in the Hat Cyclic shift The C Programming Language C Programming Language The Programming Language The C The Cat in the Hat Cat in the Hat The sort

36 Design for the KWIC Problem
What modules to have? Input-Process-Output

37 Solution 1 Input-Process-Output Function modules

38 Architecture of Solution 1
Function modules Direct Memory Access Shared data I/O Media Processing Subprogram Call System I/O Master Control 1 3 4 5 Input Circular Shift Alphabetizer Output 2 Viewed graphically, this architecture is centered on a set of shared data structures. A master controller determines the ordering of processing steps. Alphabetized Characters Index Index Output Input 潛在問題? Medium Medium file

39 背景補充說明 1972年代,大型主機電腦的記憶體以word為單位,長度可能是36~80(?) bytes
一個字元如果占8個bytes, 一個word可以儲存4個以上的字元 為提升效率,論文中的circular shift可能只是以改變每個字的index來實現,而非真的搬移字元。 論文中一些關於characters, word, index等的部份,現今應可以略過。

40 Shared Data Storage 2-D circular-shift array
Abb.: TU Graz (siehe Literatur/ Links auf Folie 30)

41 Index Structures Abb.: TU Graz (siehe Literatur/ Links auf Folie 30)

42 Java Code for Modularization 1
Source code available for downloading public class Kwic { /* shared data structure*/ public static void main(String[] args) { // file name to read KWIC kwic = new KWIC(); if (args.length != 1) { System.err.println( "KWIC Usage: java kwic.ms.KWIC file_name"); System.exit(1); } kwic.input(args[0]); kwic.circularShift(); kwic.alphabetizing(); kwic.output(); private char[] chars_; private int[] line_index_; private int[][] circular_shifts_; private int[][] alphabetized_;

43 Problems of Solution 1: No Interfaces
Direct Memory Access Shared data I/O Media Processing Subprogram Call System I/O Master Control Input Circular Shift Alphabetizer Output Alphabetized Characters Index Viewed graphically, this architecture is centered on a set of shared data structures. A master controller determines the ordering of processing steps. Index 共用資料 Data coupling Output Input Medium Medium 一旦資料格式改變,相關模組都要跟著改變。

44 延伸思考:Design for testability
How do you do unit testing for Solution I? Difficult as it lacks clean interfaces for the modules No API

45 Background of Parnas’ Idea
提出另一種模組化的切割作法(ADT) Because maintenance costs dominate development costs, it is desirable to design a system that can easily adapt to change Change is costly when a whole system must be examined/altered to deal with a “small” change Anticipate changes Importance of Modularization

46 Parnas: What is a Module?
A Work Assignment. Not a PL unit Represented by a design decision specific to itself and unknown to other modules Support flexibility in implementation Do Not represent steps in processing Low coupling, high cohesion.

47 Terminology Module - parts that can be put together to make a complete system - work assignment, subroutine, memory load , functional component Information Hiding - design principle Encapsulation - language facility for enforcing information hiding. (private/public in C#/Java)

48 Modularization 2 for KWIC
Maintain same flow of control, but Organize solution around set of data managers (objects): for initial lines shifted lines alphabetized lines Each manager: handles the representation of the data provides procedural interface for accessing the data (API) Now consider an alternative architecture in which the data is no longer shared, but rather encapsulated in a set of objects that are accessed through their procedural interfaces.

49 KWIC: Solution 2 What important decisions to hide?
Information hiding as a tool for modularization What important decisions to hide? hide data representation could be array + indices, as before or lines could be stored explicitly hide internal algorithm used to process that data could be batch or interactive processing

50 Architecture of Solution 2
Subprogram Call Master Control Processing System I/O I/O Media 1 8 Input 4 6 Output 5. get 3. set 7. get 9. get 2 10 But now the shared data structures are absent, and each of the computational elements has a well-defined interface. The design is more “object oriented”. Alphabetic Shifts LineStorage Circular Shift Input Medium Output 3 Data Modules (managers) Medium

51 Solution 2: Modularization
Module 1: Line storage Manages lines and characters; procedural interface Storage format: not specified at this point Module 2: Input Reads data lines and stores using “Line Storage” Module 3: Circular Shift Provides access functions to characters in circular shifts Requires SETUP as initialization after Input is done We have many of the same modules as before.

52 Solution 2: Modularization (Cont’d)
Module 4: Alphabetize Provides index of circular shift (ITH) ALPH called to initialize after Circular Shift Module 5: Output Prints formatted output of shifted lines Module 6: Master Control (main) Handles sequencing of other modules

53 Slightly Revised Architecture of Solution 2 (Java Code available for downloading)
(Kwic class) 3 Data Modules (managers)

54 KWIC 2: Input and Storage
LineStorage lines = new LineStorage(); Input input = new Input(); // text file into LineStorage lines input.parse(file, lines); LineStorage: two-dimension ArrayList This is a book API lines

55 KWIC 2: Input and Storage (2/2)
LineStorage lines = new LineStorage(); Input input = new Input(); // text file into LineStorage lines input.parse(file, lines); Input. parse(..) public void parse(String file, LineStorage line_storage){ try{ BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); while(line != null){ StringTokenizer tokenizer = new StringTokenizer(line); if(tokenizer.countTokens() > 0) line_storage.addEmptyLine(); while(tokenizer.hasMoreTokens()) line_storage.addWord(tokenizer.nextToken(), line_storage.getLineCount() - 1); line = reader.readLine(); } }catch(FileNotFoundException exc){

56 KWIC.execute() //main Create objects (modules) Start the work to do
public void execute(String file) { LineStorage lines = new LineStorage(); Input input = new Input(); CircularShifter shifter = new CircularShifter(); Alphabetizer alphabetizer = new Alphabetizer(); Output output = new Output(); input.parse(file, lines); // text file into LineStorage lines shifter.setup(lines); alphabetizer.alpha(shifter); output.print(alphabetizer); }

57 Source code available public class Kwic {
public static void main(String[] args){ if (args.length != 1){ System.err.println("KWIC Usage: java kwic.ms.KWIC file_name"); System.exit(1); } KWIC kwic = new KWIC(); kwic.execute(args[0]); // master control public void execute(String file) { LineStorage lines = new LineStorage(); Input input = new Input(); CircularShifter shifter = new CircularShifter(); Alphabetizer alphabetizer = new Alphabetizer(); Output output = new Output(); input.parse(file, lines); // text file into LineStorage lines shifter.setup(lines); alphabetizer.alpha(shifter); output.print(alphabetizer); Source code available

58 shifter.setup(lines) public void setup(LineStorage lines) {
shifts_ = new LineStorage(); for (int i = 0; i < lines.getLineCount(); i++){ String[] line = lines.getLine(i); for (int j = 0; j < line.length; j++){ shifts_.addEmptyLine(); for (int k = j; k < (line.length + j); k++) shifts_.addWord( line[k % line.length], shifts_.getLineCount() - 1); }

59 Sequence Diagram for KWIC, 2
time

60 Effects on Current Programming
“Fathered” key ideas of ADT (OOP): Information hiding Encapsulation before functional relations Easier understandability/maintainability Design more important than implementation Good design leads to good implementation Proper design allows for different implementations (easily modifiable)

61 Possible Changes: Change the Input Mode
將以檔案輸入方式 改成以 交談式方式輸入文字列 p之後輸出結果: Add, Print, Quit: a Star Wars The Empire Strikes Back The Return of the Jedi Add, Print, Quit: p Back The Empire Strikes Empire Strikes Back The Jedi The Return of the Return of the Jedi The Star Wars Strikes Back The Empire The Empire Strikes Back The Return of the Jedi Wars Star of the Jedi The Return the Jedi The Return of

62 Change of Input Style

63 延伸思考:Design for testability
How do you do unit testing for Solution I? Difficult as it lacks clean interfaces for the modules No API

64 UI v.s. API UI: User <-> Program API: Program <-> Program

65 應用系統的基本架構: UI Traditional approach: Input – Process – Ouput (IPO) 對比:分為 (G)UI and Functional Part (FP) AP API UI FP

66 Advantages of Tiered Approach
Easily replace UI Easily enhance FP Facilitate Regression Tests Regression Tester Prog. API UI FP 注意:單向或雙向 Model-View-Controller next time.


Download ppt "NCCUCS 軟工概論 Software Design: Modules (KWIC case study)"

Similar presentations


Ads by Google