Experiment two Java object-oriented programming
( because the speed is not good, the experiment building cursor reflects too slow, so the code adjustment and operation are done in the computer Eclipse , not in the laboratory building)
Experimental content
- Initial mastery of unit testing and TDD
- Understanding and mastering object-oriented three elements: encapsulation, inheritance, polymorphism
- Initial mastery of UML modeling
- Familiar with S.O.L.I.D
- Understanding Design Patterns
set up a directory with your own number in the ~/code directory in the lab building for storing code and UML diagrams
Experimental steps
(a) Unit testing
(1) Three kinds of code
If you want to use the program to solve the problem, you will write three kinds of code
L Pseudo-code
L Product Code
L Test Code
Example:
Requirements: We want to MyUtil solve a percentile in a class. Turn into "excellent, good, medium, pass, fail" five Grade system performance function
L pseudo-code (pseudo-code independent of the specific programming language, do not write the specific programming language grammar-related statements, 伪代码 from the intention level to solve the problem, ultimately, 伪代码 is the 产品代码 most natural, the best comment)
L Product Code (translate pseudo-code in Java language)
L test the code (write 产品代码 , we have to write 测试代码 , prove your code is not a problem.) In Java programming, the programmer tests the class implementation 单元测试 . Class XXXX , 单元测试 generally write a XXXXTest class, MyUtil write a test module for the class, the MyUtilTest.java code is as follows)
Here we have 测试用例(Test Case) designed a 测试用例 set of test inputs, execution conditions, and expected results for a particular goal to test a program path or verify that a specific requirement is met
Test the general situation:
Test for abnormal conditions, such as a score of negative or greater than 100, the code is as follows:
Run the program found that negative ticks and expectations are inconsistent, found a bug, because the judgment failed when the results are not required to more than 0. Modify MyUtil.java , increase the judgment of the negative score, the code is as follows:
in order to ensure the unit measure is sufficient, the general requirement is 测试代码 产品代码 more . The method proposed in unit testing Right-BICEP can be used to write tests
(2) TDD (test driven devlopment, testing-driven development)
test-driven development (TDD): write First 测试代码 , then write 产品代码 , and the code to write is correct.
The steps are as follows:
L CLEAR the current function to be completed and record it as a test list
L quickly completed writing test cases for this function
L Test Code compilation does not pass (no product code?)
L Write Product Code
L refactor the code and ensure the test passes (refactoring the next lab session)
L Cycle complete development of all functions
Based on TDD, we don't have an over-design situation, and the requirements are expressed through test cases, and we just 产品代码 have to let the test pass.
There is a unit test tool JUnit in Java to help with TDD
Use TDD to rewrite the previous example
EclipseFile->New->Java Project-A new TDDDemo Java project
Tdddemo-> Right-click to New->Source Folder Create a new test directorytest
TestRight-click New->JUnit Test Case New test Case classMyUtilTest
We add the first test case testNormal (there must be an annotation before the test case@Test)
在TDDDemosrc New MyUtil class in the directory, and implement the Percentage2fivegrade method
The coding rhythm of TDD is:
L Add test Code, JUnit appears red bar
L Modify the Product code
L JUnit appears green bar, mission accomplished
(ii) Object-oriented three elements
(1) Abstract
Abstract refers to the abandonment of the appearance of things and the extraction of essential factors in the cognitive thinking activities.
Abstraction is a matter of extracting the essential features of things and not considering their details for the time being. For the complex system problem, we can solve the problem by the method of hierarchical abstraction.
At the highest level of abstraction, you can use the language of the problem environment to describe the solution of the problem in a generalized way.
In the lower layers of abstraction, they are described in a procedural way.
When describing a problem solution, use a problem-oriented and implementation-oriented terminology. In program design, abstraction consists of two aspects, one is process abstraction and the other is data abstraction .
Example:
Can print out "1,2,3,4", want to hit "the" How to do? Most of the practice is to copy the above code and add one more line:
Want to print out "1..100" what to do? These two pieces of code have three lines of duplicate code, violating a common programming principle dry (Don ' t Repeat yourself), the solution is to process the abstraction , write a function printn:
The previous two-segment code can be used
(2) encapsulation, inheritance and polymorphism
Object-oriented three elements: encapsulation, inheritance, polymorphism
Object-oriented thinking involves all aspects of software development, such as object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP). The OOA decomposes the system according to the abstract key problem domain. Ood is an object-oriented implementation of the symbolic design system, which constructs the system into a "real-world" object using a method very close to the problem domain term, and implements the functional specification through the model.
OOP is coded in a programming language (such as Java) on a design basis. The main line that runs through OOA, Ood, and OOP is abstract
The result of a process abstraction is a function, and the result of the data abstraction is an abstract data Type,adt, which can be used as an ADT with inherited and polymorphic mechanisms. Data Abstraction is the core and origin of OOP
Encapsulation : Encapsulation is the wrapping of data with related behaviors to implement information hiding
Like what:
Encapsulation actually uses methods to hide the data of the class, controlling the user's modifications to the class and the extent to which the data is accessed, resulting in modularity (modularity) and information hiding (information hiding) Interface (interface) is an accurate description of the package
Describe a class of dog with a class diagram in UML
Shell->umbrello-> class icon->class Diagram->dog
dog-> Right button->properties->display-> remove public only
Dog-> Right-click and New->Attribute Fill, Type Name and selectVisibility
Dog-> Right-click and New->Operation Fill, Type Name and selectVisibility
In UML, the property of a class can display its name, type, initialization value, and attributes can also display private,public,protected. The methods of the class can display their method names, parameters, return types, and the Private,public,protected property of the method.
The representation of inheritance in UML class diagrams is to refer to the parent class with a triangular line, through inheritance, we eliminate the Dog Cat duplication of code in classes and classes and meet DRY the requirements
Inheritance is the foundation of software reusability, and it is the main way to improve the expansibility and maintainability of software system. On the basis of encapsulation, inheritance can implement code reuse , and inheritance is more important to implement polymorphism . Object-oriented objects allow different classes of objects to respond to the same message, that is, the same message can be used in different ways depending on the sending object, we call this behavior polymorphism
Polymorphism is the basis of the flexibility and extensibility of object-oriented programming
(iii) Preliminary design pattern
(1) S.o.l.i.d
Object-oriented three elements are "encapsulation, inheritance, polymorphism", and any object-oriented programming language will support these three elements syntactically. It is very difficult to use the three elements, especially polymorphism, in the abstract, s.o.l.i.d design principle is a good guide
Based on the OCP use of object-oriented polymorphism (polymorphic), more flexibility in handling change-embracing changes OCP can be achieved by: (1) abstraction and inheritance, (2) interface-oriented programming
LSPThe core idea is that the parent type object can be overridden by the quilt type Object
(2) mode and design mode
Pattern is the usual solution to a particular problem in an external environment, the most important of which is the design pattern
(3) design pattern Real Example
Design patterns provide a subsystem or component for refining a software system, or a diagram between them, which describes the public rendition structure of a communication component, which can solve a design problem in a particular context
(iv) Practice
Design and implement the plural class complex using TDD method
(1) Pseudo-code
Design a complex number class complex, respectively, the real and imaginary parts of the complex as attributes
Define two member methods to calculate the sum and difference of two complex numbers
Defines a print () method that outputs a complex number and does not output imaginary parts when the imaginary part is 0
Defines a song class that uses the complex class to create two complex objects in the main method of the class, calculating and outputting
(2) Product Code
Class Complex {
Double Re,im;
Complex () {
this.re=0;
this.im=0;}
Complex (double re) {
This.re=re;
this.im=0;}
Complex (double re,double im) {
This.re=re;
This.im=im;
}
Complex Add (complex P1,complex p2) {
Complex p=new complex (p1.re+p2.re,p1.im+p2.im);
return p;
}
Complex minus (complex P1,complex p2) {
Complex p=new complex (p1.re-p2.re,p1.im-p2.im);
return p;
}
void print () {
System.out.println ("The value of the plural is:");
if (this.im!=0)
System.out.println (this.re+ "+" +this.im+ "I");
Else
System.out.println (this.re);
}
}
(3) test code
public class song{
public static void Main (string[] args) {
Complex c=new complex ();
Complex c1=new complex (2,7);
Complex c2=new complex (5,2);
C1.print ();
C2.print ();
System.out.println ("The sum of these two complex numbers is:");
System.out.println ((C.add (C1,C2) re+ "+" +c.add (c1, C2). im+ "I"). ToString ());
System.out.println ("The difference between these two complex numbers is:");
System.out.println ((C.minus (C1,C2) re+ "+" +c.minus (c1, C2). im+ "I"). ToString ());
}
}
|
Steps |
Take |
Percentage |
|
Demand analysis |
20min |
20% |
|
Design |
25min |
25% |
|
Code implementation |
40min |
60g |
|
Test |
2min |
15% |
|
Analysis Summary |
10min |
The benefits of unit testing:
Ensuring the correctness of the unit's own functions
Unit testing is naturally evolving with code and will not expire because of code changes
Unit tests provide each unit with a strict and written contract that must be followed.
The second experiment of laboratory building