Beijing Institute of Electronic Technology (BESTI)
Real Inspection report
Course: Java Programming Class: 1353 Name: Chen Chaojan No.: 20135310
Score: Instructor: Lou Jia Peng Experimental Date: 2015.5.6
Experiment level: Preview degree: Experiment time: 22:30-1:30
Instrument Group: 10 Compulsory/Elective: Experiment Number: 2
Experiment Name: Java Object-oriented programming
Experimental purposes and requirements:1. Initial mastery of unit testing and TDD
2. Understanding and mastering 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 instrument:
Name |
Model |
Number |
Pc |
MacBook (Win7) |
1 |
Virtual Linux Systems |
Lab Building Virtual Machine |
1 |
First, the experimental Steps (a) unit test (1) Three kinds of code: pseudo Code, product code, test code
Example: Need to MyUtil
solve a percentile in a class to turn into "excellent, good, medium, pass, fail" five grade system performance.
A. Pseudo-code
Percentile five-point system:
If the score is less than 60, turn into "fail"
If the score is between 60 and 70, turn into "pass"
If the score is between 70 and 80, turn into "medium"
If the score is between 80 and 90, turn into "good"
If the score is between 90 and 100, turn into "good"
Other, turn into "error
B. Product Code
C. Test code
Run display success, description product code there is no error here
Test the general situation:
Run display success, general product Code no problem
To test for abnormal conditions:
Run display error, resulting in the product code to increase the judgment of negative points, and then run the display successfully
To test the boundary value:
To run the display without considering the score 100, modify the product code:
Since this code is complete
(2) TDD (test driven devlopment, testing-driven development): First write the test code, then write the product Code development method
General steps:
- 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
Rewrite the example of percentile to five by TDD, and feel the benefits of having a test code
A. In
Eclipse中
Click
File->New->Java Project】
Create a new
TDDDemo
Java Project B. In the project name
TDDDemo上
Right-click on the popup menu and select "
New->Source Folder】
Create a new test directory
test
C. In
test
Right-click on the directory and select "
New->JUnit Test Case】
Create a new test case class
MyUtilTest
D. In the test case class
MyUtilTest中
, enter the test code, such as
E. Red Fork Description Code has a syntax error because
MyUtil
Class is not yet established, and the Percentage2fivegrade method in the class does not exist;
TDDDemo
Of
src
In the directory, create a new
MyUtil
class, creating a Percentage2fivegrade method, the test code has no syntax errors;
MyUtilTest.java上
Right-click and select
Run as->JUnit Test】,效果
F. The test results appear red bar, indicating that the test did not pass; Modify the product code
MyUtil.Java如,再如上一步运行测试代码,显示绿条
The coding rhythm of the **TDD is:
- Add test code, JUnit appears red bar
- Modify the Product Code
- JUnit appears green bar, Task complete
G. Increasing the use cases for testing anomalies
testException和
Use cases for testing boundary conditions
testBoundary,运行测试代码出现红条;
Modify the product code, and then run the test code to appear green bar
(ii) Object-oriented three elements 1, abstract
(1) The abandonment of the appearance factors of things and the extraction of essential factors
(2) Theabstract ability of "refine, simplifying, Youbiaojili and seeking for the same" determines Programmer's Program design ability
(3) taking out the essential features of things and not considering their details for the time being
(4) Abstraction includes process abstraction and data abstraction
2. Encapsulation, inheritance and polymorphism
(1) Object-oriented three elements: encapsulation, inheritance, polymorphism.
(2) Object-oriented thinking involves all aspects of software development, such as object-oriented analysis (OOA), Object-oriented design (OOD), and object-oriented programming (OOP). OOA decomposes The system according to the abstract key problem domain and focuses on what. The main line that runs through OOA, Ood, and OOP is abstraction.
(3) Ood modeling will be modeled using the Graphical Modeling Language UML (Unified Modeling Language), UML is a common modeling language, experiment using Umbrello to model
(4) The result of Process abstraction is function, the result of data abstraction is abstract data type, data abstraction is the core and origin of OOP
(5) Encapsulation actually uses the method to hide the data of the class, control the user's modification to the class and the degree of accessing the data, which brings the advantages of modularization and information hiding, and the interface is the accurate description means of encapsulation.
To illustrate:
A.java uses classes for encapsulation, such as a dog class:
B. Dog
Classes hide properties by using class and access control, color
Open Interfaces setColor()
, getColor()
bark()
and toString
. Dog
class is a module that we can use with the following code to test the code and run the results as follows
C. You can use class diagrams in UML to describe classes Dog:
open the shell, enter in the command line umbrello
, open the UML modeling softwareumbrello;
Click the class icon on the toolbar, and then class diagram(类图)
click in the box to enter the class nameDog;
Put the mouse Dog
on the class, right-click, select Properties
, in the popup dialog box to Display
remove the Public Only
option;
Put the mouse Dog
on the class, right-click, select New->Attribute
, fill in the Pop-up dialog box, and select a Type
Name
goodVisibility,得到如Dog类
* 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. Where: + represents public; #表示 protected;-represents private. Using UML allows us to not focus on the details
D. Creating cat classes and Animaltest classes in the Dog class
The E.uml class diagram shows the static relationship between classes, AnimalTest
class dependent Dog
classes and Classes Cat
, and UML relies on a straight line with arrows
Corresponding Code:
Both the F.dog class and the cat class have the color attribute and the corresponding set and get methods, which violate the dry principle mentioned earlier, and we can solve this problem by inheriting the color attribute and the corresponding method into the parent class animal, as shown in the following UML figure:
(Note that the representation of inheritance in UML class diagrams is to refer to the parent class with a triangular line)
G. Further abstraction, the abstraction of classes Dog
bark()
and classes Cat
meow()
into an abstract method shout()
that Dog
Cat
overrides this method in classes and classes, as shown in the following UML diagram:
(Note that a method in a class in a UML class diagram Animal
shout()
is an abstract method, is italic, the Animal
class is abstract and italic)
Corresponding Code:
(iii) design pattern Preliminary 1, s.o.l.i.d principle
SRP (single Responsibility Principle, sole responsibility principle)
OCP (open-closed Principle, open-closed principle)
LSP (Liskov substitusion Principle,liskov substitution principle)
ISP (Interface segregation Principle, interface separation principle)
Dip (Dependency inversion Principle, dependency inversion principle)
(1) OCP is the most important principle, the content of which is:
- Software entities (class, modules, function, etc) should open for extension,but closed for modification.
- Software entities (classes, modules, functions, etc.) should be open to expansion and closed to modifications.
Based on the OCP
use of object-oriented polymorphism (polymorphic), more flexibility in handling change-embracing changes OCP
can be achieved with abstraction and inheritance, interface-oriented programming means
(2) SRP
the contents are:
- There should never be + than one reason for a class to change
- Never have more than one reason to modify a class
(3) LSP
the contents are:
- Subtypes must is substitutable for their base types
- Functions that use pointers or references to base classes must is able to use objects of derived classes without knowing I T
- The subclass must be able to be used by its base class
- Using a pointer to a base class or a referenced function, you must be able to use it without knowing the specific derived class object type
(4) ISP
the contents are:
- Clients should not being forced to depend upon interfaces the They do
- Customers should not rely on interfaces that they do not use
(5) DIP
the contents are:
- The modules should not depend upon low level modules. Both should depend upon abstractions
- Abstractions should not depend upon details. Details should depend upon abstractions
- High-level modules should not be dependent on low-layer modules. Both should be dependent on the abstract
- Abstraction should not depend on detail. Details should depend on abstraction
2. Mode and design mode
A pattern is a customary solution (solution) for a particular problem (problem) under an external environment (context). The pattern must make the problem clear, explain why it is used to solve the problem, and under what circumstances it will not work. Each pattern can be reused because of its repeatability, has its own name, can be imparted, and can be transplanted into different scenarios. Patterns can be seen as expert solutions to a problem that can be reused.
There are many patterns in computer science:
grasp Mode
• Analysis Mode
• Software Architecture Patterns
• Design Patterns: Created, structured, behavioral
• Management mode: The manager Pool implementation mode
• Interface Design Interaction mode
• ...
One of the most important is design patterns
3. Design Pattern Real Example
(1) Design pattern provides 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. Behind the design pattern is the principle of abstraction and solid.
There are four basic elements of the design pattern:
Pattern Name: Description mode, easy to communicate, archive
problem: Describes where to apply the pattern
Solution: Describes a design's constituent elements, not for special cases
consequence: The results and tradeoffs of applying this pattern (trade-offs)
(2) Learning the design pattern is to understand the design patterns may exist over-design problems and how to avoid it.
(3) There are many other object-oriented principles besides the solid principle. Such as:
- "Combinatorial substitution Inheritance": this is to say that relative to inheritance, more inclined to use the combination;
- "The Law of the Piper": This means "the less you know about other classes, the better";
- "Common closure principle": This means that "related classes should be packaged together";
- "Stable abstract principle": This means that "the more stable the class, the more it should be composed of abstract classes";
These principles are not isolated, but closely linked, followed by one principle and one or more principles, whereas a violation of one of these principles is likely to violate one or more of the other principles. Design patterns are the result of the application of these principles in some specific scenarios. Therefore, the design pattern can be regarded as "framework" and the Ood principle as "norm". In the process of learning the design pattern, we should constantly reflect on it, which embodies the principle of object-oriented design.
(iv) Exercise 1. Design off with TDD to implement plural class complex
(1) Pseudo-code:
Plural class Complex
The value of the plural =a+bi;
Plus = (A1+A2) + (B1+B2) I;
minus = (A1-A2) + (B1-B2) I;
(2) Product Code:
(3) Test code:
2.PSP Time Statistics
Steps |
Take |
Percentage |
Demand analysis |
20 |
18% |
Design |
10 |
9% |
Code implementation |
40 |
37% |
Test |
30 |
27% |
Analysis Summary |
10 |
9% |
3. Summarize the benefits of unit testing
(1) After analyzing the requirements, write the pseudo-code, determine the idea, then write the product Code and test code, avoid directly open the editor code will appear confusion
(2) test code and product code separate, can rest assured that the test code debugging and temporarily do not affect the use of product code
(3) using TDD to write the test code first, you can avoid many errors in the product code, easy to write the correct code directly
(4) test code for product Code guidance, can be used as a description of the user, but also convenient for users to modify
Experiment two Java object-oriented programming