Design and Test Approach for given scenario
This is not an homework. But an practice to understand what are the best practices to design , implement and unit test
a particular scenario and so discussion explaining why a particular approach was taken
as compared to other would be really helpful from understanding point of view to gain better understanding of how to approach and deal with similar situations.
How to approach following scenario from design, implementation开发者_如何学编程 and unit testing perspective.
Scenario
A speaker and a listener communicate with each other. The speaker can present emotions to the listener: smile,anger, courtesy, joke, fury, etc.
The listener gives a proper response for each communication (say something, attack, defense, ignore, etc.).
Questions
- What will be the listener objects's reaction rules ?
- How would this be designed using UML and implemented by simulating it using text outputs ?
- What would be the JUnit test case for the checking proper implementation of this scenario ?
Following steps are extremely important for any development.
Requirement gathering
Freezing and documenting exact requirements. Drafting exact use cases help a lot.
Design
Designing the solution. Here different approaches can be chosen depending upon the nature of project.
- Incremental pattern : For a long term project with short spurts of deliverables and reviews, an incremental pattern helps a lot.
- Concrete pattern : Here you design your application in thorough. The releases cycles are longer. Every feature is completed first and then delivered.
Using UML following diagrams are important.
- Class diagrams : Should list all classes in detail. Use of Interface, Abstract classes, helper classes, Third Party APIs can be detailed here.
- Sequence diagrams : Should list the flow of actions for all listed use cases in project.
Use of appropriate design patterns should be included here. Test cases should be drafted matching the use cases, generally in relation like 1 or more test cases per use case.
There actually is a lot to describe here. What I have provided is the approach that should be taken while implementing the project.
Considering your application, breaking it down to different pieces can help design it simpler. On a broad level I can think of following pieces.
- Entity - Speaker, Listener
- Expressions - List of expressions
- Rules - Rules in the form of reaction expression for speaker expression.
- Communication - Communication layer which will be used by speaker to listener (something like a broadcast) and from listener to speaker(something like a point to point queue)
To answer your last question (re. JUnit), I would implement the following:
- a test for each valid input, confirming the corresponding output
- a test giving an invalid input and confirming that this scenario is handled appropriately (your interface may not permit invalid inputs if well-designed)
I wouldn't write one unit test checking each emotion/response. Why not ? If one test fails, then the whole unit test fails, and you won't know which of the further tests could be failing. When debugging determining the complete set of responses could be important, and a prematurely exiting test removes this capability.
Here's an introduction to JUnit (perhaps a little old, but still relevant)
精彩评论