470 likes | 486 Views
Teaching slides Chapter 7. Chapter 7: The business logic layer design and implementation. Contents Software engineering methodology considerations - Programming concepts Variables Operators Decision trees Continuous condition checking Methods Class Objects
E N D
Teaching slides Chapter 7
Chapter 7: The business logic layer design and implementation Contents • Software engineering methodology considerations - Programming concepts • Variables • Operators • Decision trees • Continuous condition checking • Methods • Class • Objects • Functional design and programming • Database programming • Programming for the web • Refactoring • Debugging
Chapter 7: The business logic layer design and implementation Software engineering methodology considerations On Waterfall projects, all work pertaining to a software engineering phase is carried out and completed and only then work for the next phase can be started. After software requirements are gathered the next phase of software designing starts. All the software design work including designing and implementing business logic for the entire software product is carried out here. This means a large amount of work will be involved in designing and implementing business logic. A dedicated team of business logic designing and implementing specialists will be deployed on the project to take and complete this task.
Chapter 7: The business logic layer design and implementation Software engineering methodology considerations
Chapter 7: The business logic layer design and implementation Software engineering methodology considerations On Agile projects, software products are built incrementally. Customer provides only a bunch of user stories to the project team at a time (at start of a Sprint). The project team creates and implements all the software designs including business logic only for these user stories. So amount of work involved in designing and implementing business logic will be far less. On Agile projects, since no specialists roles are defined; the business logic design and implementation work can be done by any team member. Once all the work including business logic design and implementation is completed on a Sprint, the project team moves on to work on the next Sprint.
Chapter 7: The business logic layer design and implementation Software engineering methodology considerations
Chapter 7: The business logic layer design and implementation Programming concepts Designing and implementing business logic involves writing a lot of programming code. Most programming languages use concepts of variables, operators, methods, decision trees etc. A method is the top level entity which wraps a piece of code inside. In object oriented programming languages, there is one more concept defined which is known as a class. A class works as a wrapper to wrap even methods and variables inside. Any software program consists of many classes. Any computation involves calling one method from any other method. In object oriented programming, since methods are wrapped inside classes, you will need to mention the class name as well when calling a method.
Chapter 7: The business logic layer design and implementation Programming concepts
Chapter 7: The business logic layer design and implementation Variables Variables are defined in programming languages to hold some values which are used for computation. Computations may involve doing many things like summing some numbers or manipulation of a string or finding out time related computations. So in most programming languages many types of variables are defined. Some examples of variables types include integers, strings, dates etc.
Chapter 7: The business logic layer design and implementation Variables In object oriented programming, a variable can be declared inside a method or at class level. When variables are declared inside a method then they can be used only inside that method. But when a variable is declared at class level then it can be accessed even from outside that class. When we declare a variable, we need to specify the scope, the variable type and the variable name [modifier] [data type] [variable name] The modifier in the definition of a variable specifies its scope. If declared private then the variable can be accessed from only within the class. If it is declared public then a variable can be accessed even from outside the class. The data type specifies what type of data will be stored in the variable (numbers, strings etc.). Finally the variable name specifies the name with which the variable is called. Sometimes we also need to perform computations where defining a single variable with just one value does not help. In such cases, group variables are defined which store many values. To access individual value from the group of values, indexes are used. In the next slide, some variables are shown which are defined at class and method levels.
Chapter 7: The business logic layer design and implementation Variable declaration
Chapter 7: The business logic layer design and implementation Operators For mathematical calculations we use addition, subtraction, multiplication, division etc. operators. For software programming we use operators to perform many types of computations. With numbers, we can perform addition, subtraction etc. operations very similar to the ones performed in mathematics. With strings we can perform concatenation, join, character search and replace etc. operations. Example of a piece of computation using operators is given on the next slide.
Chapter 7: The business logic layer design and implementation Operators Integer x; Integer y; Integer a; Integer b; x = 5; y = 6; a = 304; b = 5; if (x * y) > a/b then print “result of multiplication of x and y is greater than division of a by b” else print “result of multiplication of x and y is less than division of a by b” In the program given above, we have defined 4 variables viz. x, y, a and b. You can observe that all these variables have been declared as Integer type. Then using an “if statement” we are checking if result of multiplication of x to y is greater than the result of dividing a by b. If so then using a print command we are displaying a message that result of multiplication of x and y is greater than division of a by b. If this is not the case then using an else clause in our “if statement”, we are displaying a message that result of multiplication of x and y is less than division of a by b.
Chapter 7: The business logic layer design and implementation Decision trees We implement business logic to perform some computations. In many pieces of computations, some kind of decision making is involved. For example, we may need to find out various ways to pay less taxes by finding out various tax credits available. By analyzing various scenarios we can find out how to pay least tax. In this case, we will be making some decisions to arrive at the least tax computation. A decision tree can be created using if-else-else if statements. If <condition1> true then Execute statement1 Else if <condition2> true then Execute statement2 Else if <condition3> true then Execute statement3 Else if <condition4> true then Execute statement4 When a decision tree is created then one or several statements are evaluated to know which condition is true. If a condition is true then some computation is performed and the program execution terminates.
Chapter 7: The business logic layer design and implementation Decision trees
Chapter 7: The business logic layer design and implementation Decision trees Here is an example to evaluate a condition to find out applicable tax at the rate of 9% on goods sold worth $20,000. integer x; x = 20000, float y; y = 0.09 (9% after conversion into number becomes 0.09) float z; z = x*y; Now suppose we need to find out if the tax collected is above USD 1000. If tax collected is above USD 1000 then we need to give a tax credit of USD 50. We can do it using an “if statement”. Let us define a variable to store value for tax credit and then do the require computation. integer a; if z > 1000 then a= 50 else a = 0;
Chapter 7: The business logic layer design and implementation Continuous condition checking In programming, we sometime need to check a condition continuously until the condition becomes invalid. As long as the condition is true, we do some computation. This kind of computation is needed in operations where we need to organize things or select a particular thing from among several things. For example if we need to search for a specific file in our computer then we may need to search all files and find the suitable files needed.
Chapter 7: The business logic layer design and implementation Continuous condition checking
Chapter 7: The business logic layer design and implementation Continuous condition checking Here is an example of a continuous condition checking to match a color code with all possible shades. Integer x; x= 45601 while x < 45678 match color shade; x = x + 1; if color shades match then break; The program mentioned above will start executing with a value of x at 45601. The original color will be tried to be matched with the shade with color code 45601 using the function “match color shade”. If they do not match then program execution will come to the statement “x = x + 1”. Thus value of x becomes 45602. In the next iteration, value of x becomes 45603. The loop will keep executing and increasing the value of x till the 2 colors match. Once colors match then the program execution will end using the break command “if color shades match then break”. If the program execution finds that the colors never match till encountering the last number 45678 then program execution will again terminate as we have specified that x < 45678.
Chapter 7: The business logic layer design and implementation Methods Methods are the wrapper to contain pieces of code. A method can do some computation and may or may not return any value. If a method returns any value then the method can be called from another method and the return value will be used for some further computation. In object oriented programming, a method is always wrapped inside a class. Here is definition of a method. [modifier] [return type] [method name] [parameter list] [method body] A method can be private or public. If a method is declared private then the method can be called only from inside the class where it is defined. If a method is declared public then it can be called even from outside the class. The modifier keyword here is used to denote the scope of a method (Private or Public). If a method is returning a value then the return type must be specified. A method must have a name so that it can be easily called. A method can be passed some values using the parameter list. The method body contains the pieces of code which do the computation.
Chapter 7: The business logic layer design and implementation Methods
Chapter 7: The business logic layer design and implementation Methods Suppose we need to find out grades of students based on the marks they obtained in an examination. If marks obtained were more than 75% then it will be an A grade. If marks are below 75% but above 60% then it is a B grade. If marks are above 45% but below 60% then it is C grade. Anything below 45% is a D grade. Let us first define the method which will do the computation using parameters so the method gets its inputs. Private Character ComputeGrade (integer marks) { Private Character Grade; If marks > 75 { Grade = A}; Else if marks < 75 and marks > 60 { Grade = B }; Else if marks < 60 and marks > 45 { Grade = C }; Else if marks < 45 { Grade = D }; Return Grade; } Now let us create a method which will print the grade. Private void PrintGrade (){ Private Character Grade; Grade = ComputeGrade(34); Print Grade; }
Chapter 7: The business logic layer design and implementation Class A class is the most important building block in object oriented programming languages. Classes are the blueprints on which all objects are created during runtime. Most of the benefits of object oriented programming are derived from the class concept. Data protection on object oriented programming languages is derived from the data encapsulation provided by the classes. Inheritance concept provides code reuse and better software product quality as many child classes can be created from a parent class. These child classes inherit all the properties and behavior of their parent class. Polymorphism is achieved by loading a different method at runtime and thus changing behavior of an object.
Chapter 7: The business logic layer design and implementation Class - encapsulation
Chapter 7: The business logic layer design and implementation Class - encapsulation In object oriented programming, we define all class variables as private so that they can not be accessed from outside. We use getter and setter methods to access these class variables. Let us take an example to understand encapsulation better. Suppose we want to give grades to students. Public class AssignGrade { Private String name; Private character grade; Public String getName() { Return name; } Public String getGrade() { Return grade; } Public void setName(String studentName { name = studentName; } Public void setGrade(character studentGrade { grade = studentGrade; } }
Chapter 7: The business logic layer design and implementation Class - encapsulation Now we can create a class to get and set values for name and grade. Public class getgrade { assignGrade setGrade = new assignGrade(); setGrade.setName (“William”); setGrade.setGrade (“B”); print “Student Name : “ + setGrade.getName() ; print “Student Grade :” + setGrade.getGrade(); } In the above piece of code please notice the keyword new. When an object is created from a class then it is done by creating an instance of the class using the keyword new. Also notice that we had not created a method assignGrade() but when we create the object from class assginGrade this method has come automatically. When a class is created in any object oriented programming language, a method with same name is automatically created. This default method with same name as its class is known as constructor.
Chapter 7: The business logic layer design and implementation Class - inheritance
Chapter 7: The business logic layer design and implementation Class - inheritance Suppose we are building a software application for a university. This university has many colleges and each college has many academic departments. We need to keep information about names, addresses, courses offered at the colleges and departments. When we need to model the university along with its colleges and departments, we can have a class for the university. The colleges can be represented by child classes of this class. The departments can be represented by child classes of each college. Public class university{ Private String name; Private String address; Public String getName() { Return name; } Public String getAddress() { Return address; } Public void setName(universityName) { universityName = name; } Public void setAddress(universityAddress) { universityAddress = address; } } Public class college extends university { } Public class department extends college { }
Chapter 7: The business logic layer design and implementation Class - inheritance Public class test { Public static void testInherit (){ University univ = new university(); College college1 = new college(); College college2 = new college(); Department department1 = new department(); Department department2 = new department(); Univ.setName ( “London University”); Univ.setAddress(“12 East University Street”); College1.setName(“Charles College”); College2.setName(“Royal College”); department1.setName(“Computer Science”); department2.setname(“History”); } } Now when you run your code, names and addresses of university will be set. Similarly names of college1, college2, department1 and department2 will also be set. Even though we have not defined anything inside college and department classes but due to inheritance, names and addresses attributes are passed to these child classes from their parent class university.
Chapter 7: The business logic layer design and implementation Class - polymorphism Let us take an example. Public class class1 { String name; Public String name(){ Name = class1; Return name; } } Public class class2 extends class1 { Public String name(){ Name = class2; Return name; } }
Chapter 7: The business logic layer design and implementation Class - polymorphism Now to test polymorphism, here is the class. Public class test { Public static void test{ Class1 test1 = new class1(); Class2 test2 = new class2(); Class1 test3 = new class2(); String name1 = test1.name(); String name2 = test2.name(); String name3 = test3.name(); Print name1 + name2 + name3; } } We have created 2 classes, namely class1 and class2 where class2 is a child of class1. We have defined a method name() in class1 which returns a value. Similarly we have again defined a method name() in its child class class2. To test it, we have created 3 objects test1, test2 and test3. While test1 object is based on class1; test2 object is based on class2. When it comes to test3, things become tricky. This object is based on class2 but it has been declared as type class1. This is acceptable since class2 is a child of class1 class. Next we are calling the method name() with objects test1, test2 and test3 and storing the return values from these methods in variables name1, name2 and name3. When you print name1, name2 and name3 then what you will expect? Name1 will return “class1”, name2 will return “class2” but what about name3? It will return “class1”. At compile time, the method name() in object test3 uses the method name() of class2 class. However at runtime, it picks the method name() from class1 class as this object is of type class1 and not class2 type.
Chapter 7: The business logic layer design and implementation Class diagram In object oriented software design, a software product is modeled using many classes. When a software product design is modeled this way then the diagram depicting all the classes with their class members and the relationships among those classes is known as a class diagram. A class diagram depicts the business logic design and implementation. This software design can be created referring the user stories provided by the customer. From the user stories, you need to find out which entities need to be modeled. These entities will become classes in the class diagram. The properties and behavior of the entities will become class members of these classes.
Chapter 7: The business logic layer design and implementation Class diagram Suppose we need to create the software product features for the following user stories in a Sprint: • A student can enroll for a course • A student can appear for an examination in which he/she is enrolled How we will design a class diagram for these 2 user stories? We can easily see that there are 3 main entities: student, course and examination. When we need to create classes, we need to take into account following things: • Which entities may become bigger in future (more information about them is likely to be added in future) • Which entities may remain the same size? • Which entities will not grow and are very small? The 3 entities look to grow in future and so all of them should be classes. The next task is to find out class members for each class. Assuming a student has properties including name, age, and roll number; we can model this class with the 2 behavior mentioned in the user stories (enrolling for a course and appearing for an examination). The other 2 classes will have just properties (course name, course description, exam name, exam time etc.). The resulting class diagram is provided on next slide.
Chapter 7: The business logic layer design and implementation Class diagram
Chapter 7: The business logic layer design and implementation Objects Object oriented programming languages use objects to perform all computations. These objects are created at runtime from classes. Each object carries its own data in its data structure. We already have seen objects as data structures in the examples given for encapsulation, inheritance and polymorphism in the section on classes. In all those examples, we created a class and then created objects from those classes. We created some variables and assigned them some values stored in the data structure of objects. In the next slide you can see a class is first created. Then an object is created from this class and then it is used like a variable in another class. There are many benefits of using objects. One benefit is that objects keep all data safe as the data inside an object is difficult to be accessed. The other benefit is that code reuse increases substantially as we can create different objects from the same code base using child objects.
Chapter 7: The business logic layer design and implementation Objects as data structures
Chapter 7: The business logic layer design and implementation Object diagram Object diagrams are used to model business logic of a software product. Class diagrams show static model of business logic of a software product. Only during runtime, the actual behavior and state of objects are exposed. Object diagrams show what is the state of objects and how objects are interacting with each other during runtime. Object diagrams are snapshots of a software product at any given point of time. In the next slide, object diagram for the student course and examination enrollment system is depicted. The instance of the software product is being used by 2 students at the same time. So 2 threads of objects are being depicted. One student is enrolling for a course while another student is registering for an examination.
Chapter 7: The business logic layer design and implementation Object diagram
Chapter 7: The business logic layer design and implementation Sequence diagram Sequence diagrams are another way of modeling business logic of a software product at runtime. During runtime, when a user invokes a transaction then objects interact with each other to complete that transaction. This interaction among objects takes place by call among methods belonging to these objects. The sequence of method calls for a transaction is depicted in a sequence diagram. For the student course and examination enrollment system, a sequence diagram is shown on the next slide. When a student invokes a transaction for enrollment for a course then the student object will call the course object with its enrollment for course() method. Since for the enrollment transaction, no other calls are needed, the transaction will be complete.
Chapter 7: The business logic layer design and implementation Sequence diagram
Chapter 7: The business logic layer design and implementation Statechart diagram During runtime of a software product, objects are created for fulfilling some transactions. After creation, these objects may participate in some transactions. Finally when an object is no longer needed for a transaction then it is destroyed. This is the lifecycle of all objects. A sequence diagram depicts this lifecycle of objects. In the next slide, a statechart diagram is shown for the student object of the student course and examination enrollment system. The student object is created when a user (student) wants to enroll for a course. The student object is created for this transaction. After successful enrollment, there may not be any other transaction left for this student object. So the student object will be destroyed.
Chapter 7: The business logic layer design and implementation Statechart diagram
Chapter 7: The business logic layer design and implementation Functional programming Functional programming is required even when all business logic is implemented using object oriented design. For access to user interface objects, functional programming is used. Similarly for access to databases, functional programming is again used. For example, JSP (which is a script programming language) is used for gluing the user interface with the business logic layer. Similarly it is used for gluing the database layer with the business logic layer.
Chapter 7: The business logic layer design and implementation Database programming Most software products need to create and store permanent data for their many transaction. This permanent data is created and stored in a database. Databases are a separate software programs themselves. So from the software product, a connection needs to created to the database for all transactions which need to manipulate permanent data. After connection is established with the database, permanent data manipulation can be done using programming. As most relational databases allow access to permanent data only through Structured Query Language (SQL); SQL must be embedded in the programming language for all permanent data manipulation. Suppose we need to create some pieces of data in a database table named “mall” then here is the pseudo code Connect to the database; Create record in table mall (name, address, email address, phone number); Close database connection;
Chapter 7: The business logic layer design and implementation Web programming Web programming is very important as most software products these days are being developed to be deployed on the world wide web (WWW). However the communication over the www takes place using Hyper Text Transfer Protocol (HTTP). HTTP is a stateless protocol where every time a set of data is transmitted, the connection is reset and no trace is left as to what data was transmitted. If a user is browsing a website then when the user navigates from one web page to another then no trace remains as to what data was transmitted when the first web page was accessed by the user. If a website has secured content and can be accessed only after a user logins on the website then the login information will be lost after the user logins on the login web page. Similarly if a user is using a shopping cart to put goods in the cart and navigates on another web page to select and put more goods in the shopping cart then shopping cart information from the previous web page will be lost. To overcome this problem, session management objects need to be used in web programming. These session management objects keep track of user information so that browsing history can be tracked. The preferred way of managing session information is to use cookie objects in web programming.
Chapter 7: The business logic layer design and implementation Refactoring On Agile projects, complete software design is never done before software development starts. For each Sprint, software designs including business logic design is created for the user stories provided by the customer. This way of developing a software product incrementally poses some problems to the business logic design. Some methods created in previous Sprints may need to be changed or moved to accommodate the need to create new methods to implement the new batch of user stories. Similar can be the case for classes. This essentially means that the business logic may need to be redesigned. This redesigning the business logic layer is known as refactoring. If refactoring is done manually then it will be problematic and error prone. It is not possible for a programmer to know how many times a method or class is called in the entire source code. So at some places, the changes required in the code may not take place. This may cause compilation problems for the source code. Thankfully some refactoring tools have been developed which allow to do refactoring automatically. These tools track all the source code to find where changes need to be done and they do it automatically.
Chapter 7: The business logic layer design and implementation Debugging When a programmer writes pieces of code to implement a business logic design, some errors can happen in the code due to humanly mistakes or unfamiliarity with the programming syntax. If an error exists in a piece of code then the code will not compile. The programmer then has to find the error and correct it. The process of finding errors in a piece of code and correcting it is known as debugging. There are many tools available in most programming languages which help in debugging. For programming languages which are interpreted or are in form of scripts, no compiler is available which can find errors in a piece of code. In such cases, the piece of code must be run to find out errors in code.