Controller, is a part of MVC, is also the mobile phone software development in the most need to pay attention to its unclear understanding will lead to business layer code structure confusion, high coupling. Often see a few years of old project code, if not often iterative refactoring, maintenance will be very painful. Today, let's focus on how to solve this problem. In fact, each platform has a very clear description and definition of controller, but we are easy to use in their own chaos, and eventually lead to their own discomfort. Like activity in Android, Viewcontroller in Fragment,ios, define a clear life cycle self-management approach for developers to use. We just need to take these life cycle methods as portals and drive our own code. In the process of using the following a few notes a lot of attention, your code will be very pure.
1, controller absolutely no external exposure method, do not let other C can be called by the function of the way they do. This is a serious violation of the controller life cycle self-management behavior, must be banned. For a C, accept the startup parameters of the external C and start, after adding to the system's C manager, you can not accept any other C driver. If you do need communication, use the message.
2, message communication. This should be considered to be the only means of communication between C, because only it is not interfering with the operation of C, by the target C itself decide whether to receive the message, after receiving the message how to handle. One CA launches another CB, or during the CB run, the CA sends messages to the CB. Activity, fragment in the Intent,viewcontroller in the notification, these are how many natural combinations.
3. When there is commonality between controllers, do we get swollen? This is actually a has-a, or is-a problem. I used nearly 10 years of coding experience to tell everyone, must choose Has-a, this based on several, one is the business of many changes, today's commonality tomorrow may not exist, today there are 3, tomorrow may become 2, choose is-a This strong association, will be very painful. In addition, the development of the business layer is often a few guns, the task of separate each dry, has-a is the most appropriate, the key public small module, other free play to it. Don't keep your eyes on those repetitive lines of code.
4, too many pages, controller jumps between chaos, how to break? This is actually a representation. First of all, we do the above 3 points, the problem of coupling between c no longer exists, and then we standardize the transfer between C to jump when the rules, the parameter is the target C to define the data format that you need to receive. To jump to which page, find its source code, look at its definition of the parameters of the rules, according to its requirements passed, and then the generated new C to the system's C manager is good. In Android it is presentviewcontroller in Startactivity,commitfragment,ios.
5, about the page open. Do you use Pagelink? This is to avoid going to extremes. If your page needs to be open to third-party applications, Pagelink is no doubt the best choice. Because these pages are generally the core page, the stability is high, the quantity is few, the parameter is few, a URI can express clearly. Like the electronic side of the baby details, SNS category of personal page, log details and so on. What about the jumps between the internal pages? This should be used with caution. The number of internal pages, parameters changeable, data structure is complex and diverse, with the business changes will continue to update. The data structure is compressed into a URI before each jump, which is passed to target C by the distribution center and then deserialized to verify the validity of the parameters. Every time the parameters have changed and adjusted, you tired not tired?
Well, today we talk so much, you have any better ideas or different views welcome exchanges, together to improve.
Talk about the controller in MVC in mobile development