A brief talk on application Factory mode and single case to realize business isolation in Android

Source: Internet
Author: User

Android write applications, the same need to consider the problem of reducing coupling, there are some other issues, such as the incremental update of the app, business changes, such as the ease of implementation, there will be Factory mode and a singleton figure.

Factory mode is the most commonly used instantiation object pattern, and it is a pattern that replaces the new operation with a factory method. Because the factory pattern is equivalent to creating the instance object's new, we often have to build the instance object according to class classes, such as the A A=new a () factory pattern is also used to create instance objects, so later new will be more than the mind, whether you can consider using Factory mode, although doing so, may do more work, But it will give you greater scalability and minimal modification.


Here is a small example to illustrate.


Suppose we now write a login business function, according to the Factory mode of thinking, we will not directly to write a login class, and then new out to call login method, but will first write an interface, the interface has a method name login, and then write an interface implementation class, The advantage of implementing the login method and then adding it to new is that if the implementation class changes, the interface does not need to be moved and does not affect the upper interface. Suppose our classes here are: User class, Iuserengine login Business interface class and Userengineimpl login Service Interface implementation class.


Here, it looks like the factory model is almost there? We write a testcase, you can directly implement the login function? As follows:

public void Testlogin () {User user = new User (); User.setusername ("Alex"); USER.SETPASSWD ("123456"); Iuserengine engine = New Userengineimpl (); engine.login (user);}

There seems to be no problem, but there is a big problem hidden in it.


Imagine, if you have just finished writing this hundreds of-line login method, test also passed, is complacent, the boss said, the landing business to big change ... What to do? Delete the hundreds of lines of code you just wrote? Waiting for you to pay the boss to change, through, the boss and said, well, or before the good ...


So the best way is not to move the original code, a lot of people must want to, that does not directly re-write an interface implementation class is called:UserEngineImpl2 not on the line? Indeed , keeping the original Userengineimpl is not moving, just don't use it. Seems to be better than before, but still not satisfied, if the previous userengineimpl in hundreds of places were called, it should not be changed one after another? So it's still trouble ...


So here's the best status quo, what's constant, that's the interface iuserengine! We might as well match the name of the iuserengine with its corresponding implementation class name, stored in a configuration file, each time we go to the configuration file to read exactly which implementation class to load, so if the business changes, need to modify the implementation class, only need to update an implementation class, and change one line of code in the configuration file to do the following:


Beanfactory: Used to load the properties configuration file, and the implementation gets an instance of the corresponding class. The beanfactory itself only needs an instance, so using singleton mode, here is a single example of registration:


Package Com.example.factorymode.util;import Java.io.ioexception;import Java.util.hashmap;import java.util.Map; Import Java.util.properties;import Com.example.factorymode.bean.user;import Com.example.factorymode.engine.iuserengine;public class Beanfactory {private static Properties properties;// For registered singleton mode storage Beanfactory unique instance private static map<string, beanfactory> Map = new hashmap<string, beanfactory> () ;p rivate static String beanfactoryname = BeanFactory.class.getName (), static {//Singleton mode initialization beanfactory bfactory = new BEANFAC Tory (); Map.put (BeanFactory.class.getName (), bfactory);//Initialize the configuration file to load only once to Properties = new properties (); try { Properties.load (BeanFactory.class.getClassLoader (). getResourceAsStream ("config.properties")); catch (IOException e) {e.printstacktrace ();}} /** * Singleton mode ① provides a private constructor, which makes it impossible for the outside world to New② provide a getinstance method for the outside world to provide the only instance of this class itself * * @return */private beanfactory () {}//provides an example to the outside world, Register singleton mode public static Beanfactory getinstance () {if (Map.get (beanfactoryname) = = NULL{try {map.put (Beanfactoryname, (beanfactory) Class.forName (beanfactoryname). newinstance ());} catch ( Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Return Map.get (beanfactoryname);} /** * Load required to implement class * * @param Clazz Interface class * @return */public <T> T getimpl (class<t> clazz) {String Simplename = cl Azz.getsimplename (); String implclassname = Properties.getproperty (simplename); try {return (T) Class.forName (implclassname). newinstance () ;} catch (Exception e) {e.printstacktrace ();} return null;}}


Our properties file is placed in the SRC directory, which makes it easy to load with the ClassLoader.


Config.properties content:


Iuserengine=com.example.factorymode.engine.impl.userengineimpl



Then we can write it in Testcast:


public void TestLogin2 () {User user = new User (); User.setusername ("Alex"); USER.SETPASSWD ("123456"); Iuserengine engine = Beanfactory.getinstance (). Getimpl (Iuserengine.class); engine.login (user);


Through the above transformation, encountered business changes, it can be more leisurely, but also can be applied to the incremental update of the app, such as only when the login business changes, update the app only need to update a configuration file, and a new login implementation class can be loaded to load the new implementation class to produce the corresponding instance , thus saving the user's traffic.




A brief talk on application Factory mode and single case to realize business isolation in Android

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.