The application of MVC pattern in J2ME project (II.)

Source: Internet
Author: User
Tags exit functions include interface modify reference domain
Application of Project MVC pattern in J2ME project (II.)
Author: Favoyang email:favoyang@yahoo.com Welcome to Exchange
KEYWORDS:MVC Case Mode

Content Summary:
If you want to test your own knowledge of a sort of mastery, the best way is to practice her. Combine a project called "note" (Notepad) and use the MVC pattern to build and experience the power and flexibility of MVC. Note that the term in this article about no longer uses MVC, but instead uses a salutation such as a boundary class, a control class, an entity class, a lifecycle class. On the various changes of MVC in J2ME please see this article (i), especially look at my practice, otherwise communication difficulties.
This article attaches importance to the overall design, the details are not too much talk.
I want a good sleep.

Copyright Notice:
This article at the same time published in Www.j2medev.com and my blog (blog.csdn.net/alikeboy), if necessary reprint, there are three ways: 1 contact me and by my consent; 2) and www.j2medev.com have reproduced article cooperation agreement 3 to aggregate my blog via RSS. In addition, the online reprint needs full text forwarding (including the statement of the head of the article), not taken out of context.

Body:

Note Project description

Mobile phone is a truly portable digital terminal, in addition to using mobile phone phone, send information, often let him help us to record textual information. The PDA has a large screen and handwriting input function, and the mobile phone notes on the limited, record the content is very simple. It can be said that the use of mobile phone recording information is considered a well-deserved function, but also created a series of applications-the mobile phone as a information collector anytime, anywhere. It is not difficult to imagine this kind of application will use a certain market, we choose today's note Notepad project, it is this kind of application of stand-alone version of the prototype.

Note is a lot of mobile phone built-in applications, commonly known as Notepad or notes, that this is a very common service. Perhaps friends may think that developing an existing program is not challenging. Yes, in the development of the most taboo is to redo the wheel, but for teaching can be said two. After all, a simple prototyping program won't get us into too much detail, and my main purpose is to introduce you to the application of the MVC pattern.

Note For mobile phone users to record a number of short information functions, users can add records, open browsing records, and can modify the records have been saved on the phone, of course, can also delete them. Just like the Notepad under Windows, just a lot of management functions.

Use case analysis

First imagine who is using this program: mobile phone users. OK, we'll call this participant the user in the future.
Then imagine that users are using our note (Chinese, we call it records) What do we do? Obviously, the user can add a new record, browse the record he added, modify the record he added, and he can also delete the record. A record should simply include the user's name for the record, the time the record was created or modified, and the contents of the most important record.

In general, developers are very disgusted with this textual description, often because developers are accustomed to stiff pc, rather than facing customers, to collect this demand. In fact, the text of things, is not only a summary of the system, but also we found that the development of the elements of the soil. Imagine that if your software is going to be released, you can't organize the language so that users can properly understand the function of the software and use objects, how annoying.

Refining the needs of users (in fact, my teaching needs haha). It is obvious that adding records and modifying records is the same as editing a record, which is called an edit record use case (Noteedit). Ok. The browsing record is also an obvious use case, called a browse use case (Notepad). Deletion is a management of records, called the Management record use case (Notemanager). Here, we have found and refined three main use cases, but not bad, the system is gradually becoming clear. Here to remind you that this stage is to stand on the customer's point of view (here is the user's point of view) to think about the problem, your job is to find and systematize the customer's ideas, do not have to stand in the developer's perspective to consider any details.

So,let ' Go.
Edit a record use case (Noteedit)
Event Flow 1:
1 Display the contents of the user note
2 User Edit Content
3 Users discard changes, note the content unchanged, normal exit

Event Flow 2:
1 Display the contents of the user note
2 User Edit Content
3 user Save, return main Menu

Event Flow 3:
1 Display the contents of the user note
2 User Edit Content
3 user Save As, prompting the user to enter a new filename
4) Save, return main Menu

Browse use Cases (Notepad)
Event Flow 1:
1 Display the user's note title, Creation time, content
2 the user chooses to exit, returns the main menu

Event Flow 2:
1 Display the user's note title, Creation time, content
2 User display edit, turn to edit use case

Management record Use cases (Notemanager)
Event Flow 1:
1) Display the user's note list
2 The user opens the selected note and turns to the browse use case

Event Flow 2:
1) Display the user's note list
2 The user edits the selected note, turns to the edit use case

Event Flow 3:
1) Display the user's note list
2 user creates a new note and turns to edit use case

Event Flow 4:
1) Display the user's note list
2 The user deletes the selected note
3 The confirmation prompt appears
4 User Confirmation, delete note
5) Update display, return to the note list

Once the event flow of three use cases is parsed, it is clear that the first iteration should be completed in its entirety.

Looking for classes (OO analysis)
The first is the entity class (Entity), which only needs to be extracted from the event stream to narrow the scope.
Note, obviously an object.
Content, Time (datetime), title (title), Hmm, should be the element of the note.
The method of the note includes Set/get the operation of the domain member. Because you want to save, you need to serialize the deserialization method.

General entity classes are used by a corresponding lifecycle Class (lifecycle) for his production, storage, extinction, and so on operations, the general operation of such operations independently of the use of the entity class reuse. But there is no need to analyze him at this stage, and when he draws a sequence diagram, he will find it naturally.

In order to efficiently draw the order graph, the boundary class and the control class method need to be listed. However, we save, we only know that each use case corresponds to a boundary class is good.

Design Practice
I present a detailed design process in three use cases as part of the management record use case (Notemanager) and as part of the Browse use case (Notepad). Here, we try to transform from a customer perspective to a developer's perspective. There are a number of challenges that may include some detail. You should learn to think about the whole system from the perspective of separation. The essence of MVC is right here.

Record use case (Notemanager) event Flow 1:

Notemanagerui does not know the specific form of the note list, which getnotetitlelist to the control class Notemanagerworkflow the desired data through a predetermined interface, and the control class returns a string[] array.
Again, Notemanagerworkflow needs to notelocator the data to the lifecycle class, but Notemanagerworkflow knows the details of the data. To be able to recognize the data, in addition to returning the title of the record, a uniquely identified ID is also returned as a way of recognizing the note within the entire system. So Notemanagerworkflow has two domains, one is titlelist, the other is idlist.
Here are a few details:
1 Notemanagerui, Notemanagerworkflow How to communicate, this is not a problem, we have reason to believe that they are closely related.
2 Notemanagerworkflow How to find notelocator, under normal circumstances, Noteloator is a single piece of Singlton.
3 when Notemanagerui's Shownote (index) is invoked, he will invoke Notemanager's Shownotedispose (index), and Shownotedispose will be based on the internal implementation, Convert this index to ID to identify the note

Browse use Case (Notepad) event Flow 1:

Notepadui to the control class to the title, the control class has domain note, and Noteid, but the control class through the GetNode, it is obvious that the lazy initialization technology can be used to the life cycle class to the note. After you get a reference to a Record object, you can easily request data like an entity class.

Experience sharing

Of course, you can start coding when you're done with the skeleton, and the benefit of drawing is forcing you to do the interface design between the parts in the design phase. This can effectively reduce your chances of rework, but often we think too much about detail in the design phase, such as how Notelocator communicates with RMS, and so on. It's a bad habit, but it's not easy to correct. Because no matter is the school, or accompanies the training and so on all is trains, repeatedly trains us to coding's sensitivity. We rely too much on the code to think about problems, which hinders us from thinking about the big picture and discovering more general patterns.

If you want to start coding, also do not start from the full spread, the general entity class has a strong independence. Can be developed independently, while other classes can be developed from the boundary class, if you want to test while development side, the control class, life cycle class methods to temporarily use the fade data (pseudo support), which is a great way to reduce complexity.

If you have the conditions, please do unit test, otherwise the test efficiency is really low. (Ps, someone wrote an article J2ME the use of unit test articles, okay, I want to make the test automation)

Lazy initialization is the most frequent technique I use, and I think it can greatly reduce the level of code clutter.
UML of the whole system

Some people say that can not understand the UML, but if you give me such a large code, I certainly can not understand. UML, it is possible to read:)

Screen shot
After the start of the screen: (Management use cases)

New screen After: (Edit use case)

Press Save to enter title name

Update Display:

Choose your favorite browsing, such as the title is easy to record: (Browse use case)

Screen navigation:

All kinds of code
Some friends don't want to expose their code, but for Java, decompile is too easy. You have no secret to speak of. The code is a finely grained thing compared to the design. If someone is willing to modify the code for you, the common perfect words, it is playing a lantern can not find Ah, I use Eclipse development, is in the use of open source of the crystal AH. Of course this is the additional code for the article. Real projects if open source must be under the GPL. No one wants to appear in DivX again. Reprint of a friend, say again free not only equal to not copyright.

Csdn friend, because I do not know how to spread the image outside the attachment, to code for reference start to Www.j2medev.com bar.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.