Self-understanding design patterns follow the principles:
1) A single clear function, design a class of intent to be clear, can not embracing what functions are inherited in
2) for extensions to be open, modify to close. Software usually has a change in demand, the change process through the expansion of the way to achieve changes in demand, rather than by modifying the original method, because the original method can be modified to cause the original methods of the caller will have problems, so that the layer calls the problem.
3) The changes are carried out in an abstract, unchanging and concrete. Design code in the process will face very variable things, such as in the implementation of a function, can be implemented in different ways, this time can be each concrete implementation of the abstract, the real constant is the purpose of this method to achieve
Single-Case mode:
The singleton mode is too many, only one in the entire application, the standard single-case pattern is written:
Public classsingleton{PrivateVolatitleStaticInstance//use volatile to indicate that each time you go to the memory to read the latest value; static indicates that the build cycle is the same as the current application PublicSingleton () {} Public StaticSingleton getinstance () {//also use static methods to obtain if(Instance = =NULL) {synchronized (singleton.classs) {//getting synchronization in multiple threads or causing synchronization problems if(Instance = =NULL) Instance=NewSingleton (); } } returninstance; }}
Builder mode
When you need to create an object that requires a combination of variables, consider separating the object's creation process from the object's representation, so that you can construct a complex object very succinctly.
The Common builder pattern has the process of creating dialog.
Alertdialog.builer builder=NewAlertdialog.builder (context); Builder.seticon (R.drawable.icon). Settitle ("title"). Setmessage ("message"). Setpositivebutton ("Button1", NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intWhichbutton) {Settitle ("Click"); }}). Create (). Show ();
In this case, when creating a dialog, it is not necessary to write a separate constructor for an object individually, and the creation process is at a glance.
Policy Method mode
Definition: There is a series of algorithms that encapsulate each algorithm (each of which can be encapsulated into different classes), which can be substituted between the algorithms, and the policy pattern allows the algorithm to be independent of the customers who use it.
For example, when registering a user information, may eventually register to a different server, but for the upper code does not care about the end user data to where, only need to have a place to store it, so you can abstract the storage function, the real implementation let its implementation of the subclass to do.
Abstract the Registration user information function:
Public InterfaceIusermanager {/** * @Decription Registration **/ voidRegister (final Userdalex user, final icallbackobject<userdalex>callBack); /** * @Decription Login **/ voidLogin (Final context context, final string name, final string psw, final icallbackobject<userdalex>callBack); /** * @Decription query user based on input **/ voidQueryusers (String username,intLimit,final icallbackobject<list<userdalex>>callBack); ..}
Define a different implementation method
public class Bmobusermanager implements Iusermanager {@Override public void Register (userdalex user, Final icallbackobject< Userdalex> CallBack) {... @Override public void Login ( Context context, String name, final string psw, final Icallbackobject<userdalex> CallBack) {...} @Override public voi D queryusers (String username, int limit, final icallbackobject<list <userdalex>> CallBack) {...}
Of course, it is certainly possible to implement it with other object methods. Set which method to use during initialization of the app.
// initializing the Cloud database New Bmobusermanager (); Bmob.initialize (this, Ncappcontext.applicationid); Cloudmanager.getinstance (). Setusermanager (Usermanager); }
Factory method Mode
In the activity or fragment that we create, when we define the base class, we can abstract each interface in different places and let the truly implemented subclass implement the unified method.
Methods of the base class
Public Interface Ibase<p> { /* * * function Description: Bind data to view * */ void Oninitview (Bundle savedinstancestate); /* * function Description: Get the layout file ID ** /int getlayoutresource ();}
Implement this method in different implementations of sub-classes activity or fragment
Public class Loginactivity implements ibaseview{ @Override publicvoid Oninitview (Bundle savedinstancestate) { //.... } @Override publicint getlayoutresource () { return r.layout.activity_login;} }
Observer pattern
The Notifydatasetchanged () method is used in more places in the viewer mode of Android when the database changes or the ListView content changes.
Public contactlistadapter Netadapter;mnetdatalist.addall (mlist); netadapter.notifydatasetchanged ();
notifydatasetchanged () The definition of the function in adapter
Public void notifydatasetchanged () { mobservable.notifychanged ();}
Notify all observers to update.
Combination mode
This mode is most common in Android, the view in Android is a tree structure, each viewgroup contains some columns of view, and each viewgroup itself can be regarded as a view, so the end of the mobile phone display interface, is a combination of a view.
Adaptation mode
In fact, the adapter mode is very easy to understand, we are often used in Android development. More typical are the ListView and Recyclerview. Why does the ListView need to use an adapter? The main thing is that the ListView only cares about each of its itemview, and does not care what this itemview specifically shows. Our data source holds the content to be displayed, and it saves every itemview to be displayed. There is no relationship between the ListView and the data source, which requires the adapter to provide the GetView method to the ListView, each time the ListView simply provides the location information to the GetView function, The GetView function then obtains the corresponding data from the data source according to the location information, and returns the different view according to the data.
Template method Mode
Definition: Define an algorithmic framework in an operation, and defer some steps into subclasses so that subclasses can redefine some specific steps of the algorithm without altering the structure of an algorithm.
This pattern is also quite common, such as our activity or fragment, which defines the periodic functions of some columns in base, basically defines the entire activity framework, so we only need the activity of the process base class or the Fragment method, Then we can do what we need in a defined periodic function, without worrying about the entire activity's start-up process.
Proxy mode
Definition: Provides a proxy for other classes to control access to this object.
Communication in the wirelessly process is a common thing, and if you need to process communication you can get the proxy for the remote service by using Aidl, and then we can perform the response operation through this proxy.
Private New serviceconnection () { @Override publicvoid onserviceconnected ( ComponentName name, IBinder service) { = ITransactionManager.Stub.asInterface (service); .... } }
The above is the app code is very common, and we design code is often used, of course, there are many other design patterns are not introduced, here is not introduced.
Common design Patterns in Android