Front-end Android Primer (5) –MVC mode (bottom)

Source: Internet
Author: User

We separate the View class and complete the design and writing. This time we will complete the Model class and connect the two through the Controller to complete the calculator program.

A model is an object that encapsulates data in a program and defines the logic to manipulate and manipulate the data. In the case of the calculator, the operands and operators of the input are processed and the returned results are computed. Let ' s Go
(Note: In the example, the double type is used directly to process the data, but strictly speaking, the floating-point calculation for many languages is imprecise)

One, the interface of the design model

At the beginning of the program construction, we should first consider the encapsulation and expansion between the modules, design the interface of the module, then implement the details of the module, and finally combine the modules together to form the whole program. This is the concept of interface-oriented programming.

Consider the Model class, which mainly implements three functions, 1, accepts the operand input, 2, accepts the operator input and returns the result of the calculation; 3, resets. These features are mostly externally used, so you can design an interface based on this, and for other classes, just know what method the interface defines and then use it, without needing to know the implementation details.

Create a com.test.interfaces package, and right-click New----Interface to create an interface named ICalculator (generic interface naming begins with the capital letter I)

Create the Com.test.model package and create the Calmodel class, while implementing the ICalculator interface.

Second, the calculator algorithm is realized by stack and recursive function.

The calculator's calculation rules are simple, calculated from left to right, regardless of operator precedence, such as: 2 + 1 * 2–3, equivalent to: (((2 + 1) * 2) –3) = 3; This guarantees the simplicity of writing.

We know that the stack is a last-in-first-out data structure, and we record the input operands and operators through a stack datastack. :

The so-called recursive function, is to accept a finite number of natural numbers, by repeating the call itself, the final result of a natural number of the function. The core of the algorithm is to design such a recursive function popopoffstack to solve the datastack, the process

Third, write the procedure

According to the above thinking, Popopoffstack can be written separately, do not need to be instantiated to use, so the popopoffstack is set to Calmodel static method can be,

Once you have finished writing, you can build a stack to test it, because the double type is returned, so the result is 3.0,

After the completion of the core algorithm, the processing of operands and operator input is completed and other details are added. So our calmodel is done.

Four, the model and view are connected via the controller

In the Android documentation, Activity is recommended as the Controller's role, responsible for the coordination and communication of views and models. And from the perspective of the functions that the Activity has, it is also the most suitable choice. At this point, the code structure is clear. Mainactivity will act as a Controller to coordinate views and models, and to handle some data and details. The code is as follows:

After connecting the model and the view through Mainactivity, our calculator is finally finished. You can run the test a bit.

Five, end and answer

This point:
1) through the MVC design, so that the calculator program has a good extension and maintenance capabilities, if you use another calculator algorithm, just create another implementation of the ICalculator interface Model class, and replace the original Calmode can be. When you need to add functionality, you can add code to the corresponding module without having to write it together.

2) This time only introduces the most basic MVC knowledge, and the more advanced use of the MVC pattern and the communication between modules will be explained later. From the writing of the Code, you may find that mainactivity is actually very difficult to abstract, because it is a number of connected logic and fine-grained code. That's why the MVC concept has been so long since the package technology for Model and View has made a big leap, and Controller is one of the reasons why. As a front-end, you can compare the existing MVC library to handle the Controller role. And Apple in the Cocoa of the Controller package is also very unique, there is a chance to introduce a comparison.

3) The details of the example can also be handled better, left to everyone to modify. In addition, if you have time, you can try to use the classic double-stack algorithm to implement this calculator, replacing the Calmodel class.

In the last few articles, the questions you have mentioned are summarized here:
1) When to use callbacks and MVC design why can improve maintenance and extensibility: For the former, when we design a module, the module in addition to provide methods, the thing itself is to be known to the outside world, or the outside world of this module is "eager" to know, this time need to do callback processing. For the latter, in this article is more clear.

2) When creating a project, why do you have more support-v4, support-v7 and other such packages, which is some of the advanced Android API or components to be implemented in the lower version of the backward-compatible implementation and automatically loaded into the project, its own program has no impact.

Thank you again for your support!

Front-end Android Primer (5) –MVC mode (bottom)

Related Article

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.