1 / 71

Teaching slides Chapter 7

Teaching slides Chapter 7. Chapter 7 Software middle layer design & construction. Contents Introduction Design and implementation & software engineering methodology Procedural programming: a brief introduction Object-oriented programming Basics of programming languages

Download Presentation

Teaching slides Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Teaching slides Chapter 7

  2. Chapter 7Software middle layer design & construction Contents • Introduction • Design and implementation & software engineering methodology • Procedural programming: a brief introduction • Object-oriented programming • Basics of programming languages • Variables and variable types • Operators • Decision trees & loops • Methods • Classes • Class variables, Inheritance, Interfaces, Encapsulation, Method over riding, Import statement, class diagram • Objects & object-oriented programming • Objects & data structures, Object state & behavior, Object state management in web applications, Object diagram, Sequence diagrams, Statechart diagrams • Packages • Database programming • Model-view-controller revisited • Refactoring • Client-side & server-side scripts • Debugging

  3. Chapter 7Software middle layer design & construction A human can understand both natural and programming language. A computer understands only machine code

  4. Chapter 7Software middle layer design & construction Software products are pieces of machine codes which a computer can understand. Human beings can not understand this machine code. Software engineers use programming languages to write source code. This source code when compiled get converted into machine code. Software requirements are written using a natural language which human beings understand without any problems. A software product needs to be developed based on these software requirements. A software design needs to be developed based on these software requirements. Later source code needs to be written based on this software design. When this source code is compiled then the source code is converted into machine code. This machine code is the software product.

  5. Chapter 7Software middle layer design & construction Platform independent technology using byte code

  6. Chapter 7Software middle layer design & construction When a piece of source code is compiled to create machine code, this machine code is platform dependent. This generated machine code can run only on computers which run the same operating system as the one on which the machine code was generated. On other operating systems, this machine code will not run at all. In this scenario, the software project team may need to write source code separately for each operating system on which the software product is targeted to run on. The source code written for a specific operating system uses specific operating system dependent functions. Thus the source code will be written differently for different operating systems. This is lot of work for the software project team. Platform independent programming languages like Java, C# etc. have evolved to solve this problem. For these programming languages, there is a compiler which converts the source code into what is known as byte code. This byte code is platform independent and can run on just any operating system. A virtual machine converts this byte code into machine code. A virtual machine is a language specific runtime environment which converts byte codes into machine code.

  7. Chapter 7Software middle layer design & construction Variables The most basic elements of any programming language are variables, methods, operators, decision trees etc. A variable is a placeholder to hold a piece of data which can be manipulated to do various kinds of computations. Most computations involve numbers, characters, yes / no decisions etc. To take care of these kinds of computations, various types of variables have been defined in programming languages. Essentially they are data types of variables. For example in Java there are variable types as “int” for Integer type variables, “char” for character variable types, “boolean” for Boolean decision types, “float” for decimal / fraction variable types etc.

  8. Chapter 7Software middle layer design & construction A small program to understand variables: Create variable a; Create variable b; Assign a = 5; Assign b = 6; Compute a multiplied by b; Display a multiplied by b on the user screen; This program will first create 2 variables. Some numerical values are later assigned to these 2 variables. These 2 variables are then multiplied and the program asks the computer to display the result of this multiplication on the computer screen.

  9. Chapter 7Software middle layer design & construction Scope for variables in procedural and object oriented programming

  10. Chapter 7Software middle layer design & construction Any variable can be defined as a private or a public type. A private variable can be visible and called only inside the programming structure within which it is defined. Public variables on the other hand are visible through out the module in which it is defined. Variables used in a procedural programming language have a limitation of scope as compared to the ones used in object oriented programming languages. In procedural programming, variables can only be defined inside a method. Variables which are to be used only for that method are declared as private variables. But variables which need to be used outside that method need to be declared as public. Declaring a variable as public poses serious problems. A public variable can be seen and accessed from any module of a software product. A public variable thus can be used and even reassigned some other value than a value it was assigned previously. This leads to software defects which are difficult to trace and fix. Variables used in an object oriented programming language do not have this limitation of scope. A variable in an object oriented programming language can be defined inside a method and also inside a class but outside any method. A variable when defined at class level can be visible outside that class and can be accessed if declared public. But all variables which are defined inside a method are always private.

  11. Chapter 7Software middle layer design & construction Operators Operators are used in programming languages to do many types of computations. Some of the operators include assignment, equality, less than, more than, count etc. Assignment operator is used to assign some value to variables. Other operators are used to check conditions in decision trees, loops and other programming constructs.

  12. Chapter 7Software middle layer design & construction The if – then and the if-then-else decision trees

  13. Chapter 7Software middle layer design & construction Software programming is mostly about evaluation of some conditions. Based on evaluation results some computing task is performed. Decision trees are one of the most commonly used programming constructs used in software programming. In a decision tree, a condition is evaluated. If the condition is found true then some computing task is performed. If the condition is found to be incorrect then some other computing task is performed.

  14. Chapter 7Software middle layer design & construction Here is one example of decision tree: Create variable a; Create variable b; Create variable c; Assign a = 5; Assign b = 6; Assign c = 34; If c >= 30 then Compute a multiplied by b; Display a multiplied by b on user screen; Else Display a message “age of the leader is below 30 and computation is not possible”; End if;

  15. Chapter 7Software middle layer design & construction In the example we can see that a condition (if c >= 30 then) is tested. If the condition is true then one set of computation will be done by this computer program. If this condition is not true then another set of computation will be done.

  16. Chapter 7Software middle layer design & construction The while loop

  17. Chapter 7Software middle layer design & construction Loops are another programming constructs which are used to do computations based on evaluating some condition. As long as the condition holds true, some computation will be repeated. Once the condition turns out to be false then the evaluation of the condition terminates. The execution then comes out of the loop and computation ends. One important aspect about loops is that in the condition statement the condition should be incremented or decremented after execution of each loop. If this increment or decrement in the condition leads to a situation where the exit condition is never met (the condition is always true after a loop execution) then it will become an endless loop. This is a software defect and must be avoided.

  18. Chapter 7Software middle layer design & construction Methods Methods in software programming are used for naming the wrapper of a piece of source code. In procedural programming, a piece of source code is wrapped inside a method. When this piece of source code needs to be called from another method then the method name along with the complete method signature is used. Since there is no wrapper above a method in procedural programming, a method is always public; otherwise it can never be called by another method. In object oriented programming, a method is wrapped inside a class. When a method needs to be called from another method in another class then the class name along with the method signature is used. A method in object oriented programming can be both private or public. If a method is public then it will be visible even outside the class in which it is defined. If the method is private then it will be visible only to the methods which are defined inside the same class.

  19. Chapter 7Software middle layer design & construction Method definition: <Modifier> <data type> method name (<data type> parameter 1, <data type> parameter 2..) {method body Exception list Exception handling } A method has a modifier which defines its scope (private or public). The data type declaration defines what kind of a value a method will be returning (an integer / string / boolean etc.). The method can have many parameters. Each parameter should be declared with its data type. Data is passed to a method through these parameters. The method body contains business logic which does some computation. The exception list contains exceptions which can arise during computation. These exceptions can be handled by writing exception handling logic.

  20. Chapter 7Software middle layer design & construction Method calling

  21. Chapter 7Software middle layer design & construction Example of a method being called from another method: Pass_candies (a, b) Create variable x; Assign x = a multiplied by b; Return x; Candy_computation() Create variable a; Create variable b; Create variable y; Create variable c; Assign a = 5; Assign b = 6; Assign c = 35; Assign y = Pass_candies (a, b);

  22. Chapter 7Software middle layer design & construction If c >= 30 then Display y (a multiplied by b) on user screen; Else Display a message “age of the leader is below 30 and computation is not possible”; End if; In this example a method is defined which takes 2 parameters. This method is called by another method with passing values stored in 2 variables to the parameters of this method. This method does computations and returns the computed value. This value is then used in the other method.

  23. Chapter 7Software middle layer design & construction In object oriented programming, there is type of method which is known as constructor. A constructor has the same name as the name of its class. When the class is called without specifying a method name then this constructor method is called by default. Constructors are used for the following purposes: • Identify the name of the objects that need to be created from classes (object initialization) • Method overloading • Copying or cloning an object • Assigning the value of one object to another For example we can initialize an object called “person” using the statement getset person = new getset (“Natasha”); In the above statement the keyword “new” is used to create an object. “getset” is the name of the class. In many cases if the constructor is not specifically defined inside the class then when an object is created during runtime; a constructor is automatically created for that object.

  24. Chapter 7Software middle layer design & construction A method overloading example is given below. public class rectangle { private integer length; private integer height; private integer x; private integer y; public rectangle() { this.x = 15; this.y = 20; this.length = 20; this.height = 25; }

  25. Chapter 7Software middle layer design & construction public rectangle (integer length1, integer height1) { this.length = length1; this.height = height1; this.x = 1; this.y = 2; } public rectangle (integer length2, integer height2, integer x, integer y) { this.length = length2; this.height = height2; this.x = x; this.y = y; } } In the above example, method rectangle() is overloaded many times with a new method signature.

  26. Chapter 7Software middle layer design & construction Many different types of objects can be created from the rectangle class. It is due to the fact that the rectangle() method can be overloaded to produce these different objects. Rectangle rectangle1 = new rectangle(); Rectangle rectangle2 = new rectangle(30,35); Rectangle rectangle3 = new rectangle(40, 50, 5, 10); When you test your code, you will see that three objects will be created: rectangle1 will be created with length 20, height 25, and with the center at (15, 20); rectangle2 will be created with length 30, height 35, and with the center at (1, 2); rectangle3 will be created with length 40, height 50, and center of the rectangle at (5, 10). Method overloading is a good way to reuse source code. Just one method can be used to create many types of objects. One drawback of method overloading is that it leads to difficult to understand code.

  27. Chapter 7Software middle layer design & construction Class Classes are the blueprints for objects which are used to build software products in any object oriented programming language. A class can be declared with a modifier, class name and class body. If the class is a child class then keyword “extends” is used with the parent class name. If a class implements an interface then keyword “implements” is used with the interface name. Classes serve many useful purposes in object oriented programming. They are used as a wrapper to manage source code of a software product. Classes help object oriented programming languages to implement powerful techniques like encapsulation, polymorphism, method overriding, method overloading, inheritance etc.

  28. Chapter 7Software middle layer design & construction Example of a class: <modifier> class my_class { Pass_candies (a, b) Create variable x; Assign x = a multiplied by b; Return x; Candy_computation() Create variable a; Create variable b; Create variable y; Create variable c; Assign a = 5; Assign b = 6; Assign c = 34; Assign y = Pass_candies (a, b); If c >= 30 then Display y (a multiplied by b) on user screen; Else Display a message “age of the leader is below 30 and computation is not possible”; End if; }

  29. Chapter 7Software middle layer design & construction Classes & Scope for variables and methods in object oriented programming

  30. Chapter 7Software middle layer design & construction Accessor methods Variables can be declared inside methods and also at class level. Variables when declared at class level can be both public and private. These variables are known as class variables. If a variable is declared as public then it can be accessed and manipulated from anywhere within the software product. This will lead to same problem as encountered in procedural programming. To overcome this problem, a technique is used which is known as encapsulation. Encapsulation ensures that any data (stored in a variable) contained in a class is not visible from outside. To do this, all class variables are declared as private so that they are not visible from outside the class. These private class variables however can still be called from outside using what is known as accessor methods. Accessor methods are used to both set value of a class variable as well as to get value from a class variable.

  31. Chapter 7Software middle layer design & construction Here is an example of encapsulation: public class my_encapsulate{ private String name; private integer age; public integer getAge(){ return age; } public String getName(){ return name; } public void setAge (integer newAge){ age = newAge; } public void setName (String newName){ name = newName; } }

  32. Chapter 7Software middle layer design & construction The my_encapsulate class has 2 private class variables. 2 accessor methods “getAge()” and “setAge()” have been created for the private class variable “age”. Similar accessor methods have also been defined for the private class variable “name”. These accessor methods are defined as public. So these methods are visible from outside this class. Using these accessor methods it is easy to read value stored in a private class variable as well as set a value to a private class variable. public class test{ public static void main() { my_encapsulate encap = new my_encapsulate(); encap.setName ("Rebecca"); encap.setAge (20); print to user screen ("Name : " + encap.getName() + " Age : " + encap.getAge()); } }

  33. Chapter 7Software middle layer design & construction Inheritance Public class parent { public integer age; public string language; age = 44; language = “English” } Public class child extends parent { age = 20; language = “French”; } Inheritance is one property which is used to reuse source code. In the example here, a child class “child” has been created which inherits the class variables “age” and “language” from its parent class “parent”.

  34. Chapter 7Software middle layer design & construction Interfaces Interface is used to define a software component without any concrete details. It just has a structure. Later many concrete classes can be created from this interface which will implement concrete details. Public interface my_interface { Public void yellow(); } Now you can implement a class based on this interface as follows: Public class my_class implements my_interface { Public void yellow(){ String color = “light yellow”; Print to user screen (color); } }

  35. Chapter 7Software middle layer design & construction Method overriding Method overriding is used to differentiate behavior of a child class from its parent class. The child class implements a method differently from its parent class with the same name. When an object is created from the child class then it will behave differently than an object created from the parent class. Here is an example of method overriding: class Animal{ void eat(){ System.out.println("Animal is eating");} } class Cat extends Animal{ void eat(){ System.out.println("Cat is eating mouse");} public static void main(){ Cat obj = new Cat(); obj.eat(); }

  36. Chapter 7Software middle layer design & construction Import statements To manage writing source code, import statements are used. An import statement will make source code written in any other class available in a class. If you have some piece of source code in a class and you want to import it in the new class you are creating then you can use import statement like below: Import <package>.<sub-package>.class_name; After the import statement, all the classes and their class members (Methods & class variables) will become available in your new class. You can use them as if they are written in the newly created class. Import statements are different from creating objects from a class. When an object is created, then actually an instance of the class is created.

  37. Chapter 7Software middle layer design & construction Class diagram A class diagram is used to design a complete software product using classes and their relationship with each other. Internal structure of a class shows all its class variables and its methods. Class diagram is also known as static diagram because it shows classes and their relationships with each other of the software product when it is not running (is in static state). A class diagram accurately shows design of a software product. All the source code when written is entirely based on the software design in the form of a class diagram for the software product. The class names, class variable names and method names as well as their declarations defined in a class diagram will be used when the source code is written.

  38. Chapter 7Software middle layer design & construction A class diagram can be made for a software product by using mental model. In the software requirement specification document; all nouns, verbs and adjectives can be identified. Each noun can become a class, each verb can become a method of a class and each adjective can become a class variable. This way the complete software product can be designed as a class diagram. However there is a limitation to this approach. The problem is that there will be too many places in the software design where repetition of almost same design components or their parts can happen. For example, if you built two classes representing a credit card and an ATM machine then many attributes and behavior such as customer information, bank information, and data related to a transaction, will be common to both of these. So, you end up creating two classes that have many things in common. This will result in unnecessary duplication of the source code. This is a violation of the principles of code reuse. To counter this challenge, the mental model approach tries to remodel the classes by using refactoring. The common behavior and attributes are then shifted from many classes to new classes that will be built.

  39. Chapter 7Software middle layer design & construction There is another way to create a class diagram. In this approach the component diagram is taken as input to create the class diagram. One important factor here is that component diagrams do not contain all the information which is required in creating class diagrams. You can get information about orders, customers, sales representative and you can create a class diagram from the component diagram for restaurant management system described in Chapter 5. But if you compare this component diagram with the software requirement specifications (SRS) given in Chapter 4, you will see that one piece of information is missing in the component diagram. This piece of information is about order types. Presently this piece of information is not covered in the component design. The order types have not been covered in component design because the component design depicts objects or components at aggregate level. The order type information is actually at detail level. This information should be covered at detail design. the class diagram is the detail design of a software product and thus it can cover detail aspect about order types. This information is still at the SRS and has to be covered now. We can see that components diagrams can not cover all design details about a software product. But class diagrams must cover all aspects of the software design.

  40. Chapter 7Software middle layer design & construction A class diagram showing internal structure of classes as well as relationship of classes with each other

  41. Chapter 7Software middle layer design & construction Class diagram for the restaurant order management system includes the classes sales representative, customer and order. Two classes Take out order and Dine in order are also included in the class diagram. These 2 classes are child classes for the order class. They have been made child classes because even though they have some common elements like close(), confirm(), date, number (order number) etc. but at the same time they have differences. While Take out order type has elements like receive() which is not part of the Dine in order type.

  42. Chapter 7Software middle layer design & construction Objects Objects are the building blocks for all software products which are built using object oriented design. When we design classes then actually we are designing objects. All the objects created during runtime are actually instances of classes. An object can be created by using the “new” keyword. If we have a class named “my_class” then we can use following statement to created objects: My_object = new my_class; Once an object is created then it can be assigned to any variable or any other object. There is one difference though. While a variable is assigned by value; an object is always assigned by reference. This is important.

  43. Chapter 7Software middle layer design & construction class Echo { integer count = 0; } Now, let us create an object of our class Echo and call it in another class. public class my_class { public static void main() { Echo e1 = new Echo(); Echo e2 = new Echo(); Integer x = 4; e1.count = x; e2.count = x; Print on user screen (e1.count + e2.count); } } In this example we have created 2 objects and assigned values to these objects. After summing the values stored in the objects the result will show 8.

  44. Chapter 7Software middle layer design & construction Let us take a look at this assignment example. Here we are assigning one object to another object. public class my_class { public static void main() { Echo e1 = new Echo(); Echo e2 = new Echo(); Integer x = 4; e1.count = x; e2 = e1; e2.count = e2.count + 1; Print on user screen (e1.count + e2.count); } } In this example, one object is assigned to another object. Suppose instead of object, the assignment was only for a variable then what could have been the result? Here the result would have been 9 as one variable is incremented by 1 so it became 5. But another variable kept a value of 4. so the result would have been 9. But since both e1 and e2 are objects and not variables. Once we assigned e1 to e2 then both objects started pointing to the same memory location. So if one object was incremented by 1 then the other object also incremented by 1. Thus now both objects are storing a value of 5. Thus their sum will give a value of 10.

  45. Chapter 7Software middle layer design & construction Variable and object declaration in a method

  46. Chapter 7Software middle layer design & construction In procedural programming, we can use variables to hold some data which will be used for computation. In contrast, we can use objects in object oriented programming to hold some data which will be used for computation. What is the difference? When we use variables, we can have just one piece of data stored at one place. With array variables, we can store more than one piece of at one place. But data type of all these pieces of data will be the same. For example we can have an integer array and we can hold data like [2,7,10] etc. in this array.

  47. Chapter 7Software middle layer design & construction Comparison of arrays & objects

  48. Chapter 7Software middle layer design & construction In the figure given here, we have 2 arrays. One array holds numeric data while the other array holds string data. An object is also depicted which can hold many pieces of data having different data types. We can see that at the most an array can hold many pieces of data but all these pieces of data belong to only one data type. What if we need many pieces of data belonging to many data types. After all in real life, most data structures hold data belonging to many data types. For example, if we have a user registration form on a website and we need to capture all the pieces of data provided by the user on this form then we will end up having pieces of data belonging to user name, age, sex, date of birth etc. All these pieces of data belong to different data types. Creating an object which can hold all these different pieces of data in one structure (the object data structure) is possible. When you save data in a database when the user clicks a button on the user interface to register the user then you are taking all this data entered on the user interface to the database. If the data structure used to collect the data from user interface is same as the data structure of the database then software design can be used to design the database as well. In fact the database design and software design will be the same and so effort required for creating database design can be avoided. At the same time software programming will become much easier to do as it will become a breeze to pick all data from the user interface and save them in the database without doing much computation work.

  49. Chapter 7Software middle layer design & construction The 3 layers of a software product with transformation of entities in each layer

  50. Chapter 7Software middle layer design & construction In the diagram given above you can see there is a user interface, a business logic layer and a database. The user interface contains fields user name, user address, product ID of a product and product description. The business logic takes data provided by a user in these fields and assign them to some variables. There are 2 database tables in the database, one each for keeping user information and product information. In the business logic, the data stored in variables need to be assigned to the database entities so that all the data provided by the user can be stored in the database. Once the user clicks on some button on the user interface which is used for saving user provided data then all user provided data will be saved in the database. Instead of using variables, if a single object is created in the business logic with a data structure which is same as that of the database structure then amount of source code to be written for saving all this data will be less. At the same time, it will be very easy to write the source code as well.

More Related