Java Lab Report II: Java Object-oriented programming
20135315 Song Ning
Experimental requirements:
1. Initial mastery of unit testing and TDD2. Understand and master the object-oriented three elements: encapsulation, inheritance, polymorphism 3. Initial mastery of UML modeling 4. Familiarity with S.O.L.I.D principles 5. Understanding Design Patterns
Experimental content
(i) Unit testing (ii) object-oriented three elements (iii) design pattern preliminary (iv) exercise
Experimental process
(a) Unit test (1) three codes
Writing a program requires the use of pseudo-code, product code, test code.
Pseudo-code is independent of the specific programming language, which 伪代码
solves the problem from the intent level and, ultimately, 伪代码
is the 产品代码
most natural and best comment.
The Product code is:
The Test code is:
(2) TDD (test driven devlopment, testing-driven development)
Definition: Write first 测试代码
, then write 产品代码
the development method
The general steps are as follows:
- Clear the current functionality to be completed and record it as a test list
- Quick completion of writing test cases for this feature
- Test code compilation does not pass (no product code)
- Write the Product Code
- Test Pass
- Refactor the code and ensure the test passes (refactoring the next lab session)
- Cycle through the development of all functions
Writing and debugging results of TDD mode test code
(ii) Object-oriented three elements (1) abstract
Abstract ability refers to the ability of "refine, simplifying, Youbiaojili, and seeking for the same in different".
In program design, abstraction consists of two aspects, one is process abstraction and the other is data abstraction.
(2) encapsulation, inheritance and polymorphism
The three elements of object-oriented (object-oriented) include: encapsulation, inheritance, polymorphism. Includes object-oriented analysis (OOA), Object-oriented design (OOD), object-oriented programming implementation (OOP).
OOA focuses on whatto do, and Ood focuses on how to doit, and OOP encodes it in a programming language (such as Java) on a design basis.
1. Encapsulation
Definition: Wrapping data together with related behaviors to implement information is hidden.
Encapsulation actually uses methods to hide the data of the class, controlling the user's modification of the class and the degree of access to the data, resulting in the benefits of modularity (modularity) and information hiding (information hiding) ; interface (interface) is an accurate description of the package. Dog
classes use classes and access control (Private,public) to hide Properties color
, open interfaces setColor()
, getColor()
bark()
and toString
.
Use UML software to design the dog, cat, and animal classes and implement them in eclipse.
2. Modeling Language UML
UML is a common modeling language, we use Umbrello to model in our experiments, we recommend that you use STARUML in Windows.
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 inheritance and polymorphic mechanisms. Data Abstraction is the core and origin of OOP.
Use UML software to model the animal test program and implement it through eclipse.
(iii) design pattern preliminary (1) S.O.L.I.D principle
1. SRP (single Responsibility Principle, sole responsibility principle)
Content: Never have more than one reason to modify a class. objects provide a high degree of encapsulation of a single responsibility, and object changes depend only on the change of a single responsibility.
2. OCP (open-closed Principle, open-closed principle)
Content: Software entities (classes, modules, functions, etc.) should be open to expansion, closed to modifications.
For the expansion and opening, the behavior of the software module must be extensible, when the application needs change or needs to meet the new application requirements, we should let the module work in different ways;
To modify the closed, requires the module source code is not modifiable, no one is allowed to modify the existing module source.
OCP
Implementation means : (1) abstraction and inheritance, (2) interface-oriented programming.
3. LSP (Liskov substitusion Principle,liskov substitution principle)
Content: Subclasses must be able to be replaced by their base classes; Using a pointer to a base class or a referenced function must be able to use it without knowing the specific derived class object type.
The core idea is that the parent type object can be overridden by the quilt type object.
4, ISP (Interface segregation Principle, interface separation principle)
Content: Customers should not rely on interfaces that they do not use (not too many functions of the interface)
5, DIP (Dependency inversion Principle, dependency inversion principle)
Content: High-level modules should not be dependent on the lower layers, both should be dependent on abstraction; abstractions should not be dependent on detail, and detail should be dependent on abstraction.
(2) mode and design mode
pattern is an external environment, the usual solution to a specific problem, can be seen as a problem can be reused expert solution .
(3) design pattern Real Example
1. Four basic elements:
Mode Name: Description mode, easy to communicate, archive
Problem: Describes where to apply the pattern
Workaround: Describe the constituent elements of a design, not for special cases
Result: The results and tradeoffs of applying this pattern
2. Abstract Factory mode
Prone to over-design problems
Just for the plastic data.
The method of presenting the data is abstracted and encapsulated into the database class.
Encapsulates the method that creates the object of the new data type into the factory class
(iv) Exercise 1 using TDD to design a complex that implements a complex number class. 2. Report the time of your PSP (Personal software Process)
Steps |
Time Consuming |
percentage |
Demand analysis |
10 minutes |
10% |
Design |
20 minutes |
20% |
Code implementation |
20 minutes |
20% |
Test |
40 minutes |
60g |
Analysis Summary |
10 minutes |
The pseudo-code is:
1) Properties of the plural class ComplexNumber
Realpart: Real part, representing the real portion of a complex number
Imaginpart: Imaginary part, representing the imaginary part of a complex number
2) method of complexnumber of plural class
ComplexNumber () constructor, set the real part, the imaginary part to 0
ComplexNumber (double Realpart, double Imaginpart) constructor to create complex objects while completing real parts of complex numbers, initialization of imaginary parts
Getrealpart () get the real part
Getimaginarypart () Get imaginary part
Getrealpart (double realpart) set the real part
Getimaginarypart (double imaginpart) Set imaginary part
Add (ComplexNumber c) Complex sum
Add (double realPart2) complex sum
Minus (ComplexNumber c) Complex subtraction
Minus (double realPart2) plural subtraction
Complexmulti (ComplexNumber c) Multiply plural
Complexmulti (double realPart2) Multiply plural
ToString () combines the real part of the current complex object with the imaginary part into a a+bi string form
The Product code is:
The Test code is:
4. Summarize the benefits of unit testing
Through the TDD mode programming, the product code can be tested and perfected by the result, which can make the product code more rigorous and perfect.
Can improve the feedback speed, reduce duplication of work, improve development efficiency.
If a bug or new feature is found, you can run unit tests immediately to verify that the previously completed code is correct and easy.
Java Lab Report II: Java Object-oriented programming