A Preliminary Study on the factory model of Android development and a preliminary study on the android Factory

Source: Internet
Author: User

A Preliminary Study on the factory model of Android development and a preliminary study on the android Factory

Factory is a common mode. I think it is also a very easy-to-understand mode. Let's give a simple example. We often take tests when we were young, what you are most afraid of each time is the score of your parents asking for the exam. Our mood is different for different scores. Then we will write a Java program in factory mode based on this situation.

Public class Factory {public static Student_test creator (String rank) {if (rank. equals ("excellent") {return new Student_Laugh ();} else {return new Student_Cry ();}}}
Here we set up a factory class to generate a class of reaction to the student's different emotions based on the student's different scores. Many people think that it is not good for me to directly set the value of the new Student () class. Yes, there is no problem with this logic, however, if the initialization work we do when creating a Student instance is not as simple as assigning values, we all know that our hard-earned scores are played by our teachers and have gone through a long marking process, the conversion may be a long piece of code. If it is also written into the constructor, our code will be ugly (Refactor reconstruction is required ).
Why is the code really ugly? Most people may not have this feeling at the beginning, as I thought at the beginning. However, after analyzing the initialization work, if it is a long piece of code, it means a lot of work to be done. loading a lot of work into a method is equivalent to putting a lot of eggs in a basket, it is very dangerous. This is also based on the Java object-oriented principle. The object-oriented Encapsulation (Encapsulation) and distributed (Delegation) tell us that, try to "cut" long codes into each segment, and then "encapsulate" each segment (to reduce the coupling between segments), so that risks are dispersed, if you need to modify each segment in the future, nothing will happen again.
In this example, we need to separate the instance creation and use, that is, this removes the large amount of initialization work required to create an instance from the Student constructor.
When building the Factory class, besides the Factory class above, there is also the abstract class Student
public abstract class Student {}
The Student subclass laugh and the cry class inherit the abstract class Student.

Next we will introduce the abstract factory model. The difference between the two models lies in the complexity of object creation. If the method for creating an object becomes complicated, for example, the factory method above creates an object Student_test. What if students still have the normal status Student_normal! Let's continue with the above example. A family is like a factory. There are two children in the family, and both of them are still going to take the test. They will all have the test scores, but even though they were born by a parent, the scores are also different. According to the above scenario, we are writing an abstract factory model.

public abstract class Factory{public abstract Student_normal creator();public abstract Student_test creator(String name);}public class SimpleFactory extends Factory{public Student_normal creator(){ ......... return new StudentA }public Student_test creator(String name){.........return new StudentA_laugh|cry  }}public class BombFactory extends Factory{public Student_normal creator(){...... return new StudentB}public Student_test creator(String name){...... return new StudentB_laugh|cry }}}

From the code analysis above, we have two abstract creator classes, Student_normal and Student_test, which represent the two statuses of Normal Students, exam and no exam, in each abstract class model, StudentA, StudentB, and so on. So why don't we just build two factories directly? Because they have links and relationships with each other, we encapsulate the common part in the abstract class, and implement different parts using subclasses.

Abstract Factory models have more roles than common factory models [Abstract Factory classes], which must be implemented by ordinary factory classes inherited from abstract factory classes, abstract Factory classes encapsulate the sharing of production products, while general factory classes are responsible for the production of a single type of specific products.





What is the difference between the Android mobile phone factory model and the engineering model?

The engineering mode is used to upgrade the system and change key system parameters.
The factory mode is used for reboot. (Similar to restoring factory settings)

Android phone factory Model

Restore production? How much is your system? Enter the settings and find the reset option. If the option is not reset, find the privacy option. Production can be restored. What is your factory model? Engineering model? Or the coal mining mode?

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.