If you are involved in java-related development, it is inevitable to use abstraction and encapsulation, which is a feature of java, but also every developer must master, Java is so, and so is Android. And the design pattern is to tell us how to write efficient and more applicable and extensibility of the code, recently also learned a few kinds of more commonly used design patterns, listed below, although not fine, but should know that I will do a summary.
# # # # #单例设计模式 #####
A generic definition of a singleton design pattern: only one instance is allowed in a class.
Implementation Idea: Let the Class's construction method be privatized, and provide a static method to instantiate the class.
Code:
Static classSingleTon {Private StaticSingleTon instance; PrivateSingleTon () {}; public StaticSingleTon newinstance () {if(instance==NULL) { synchronized(SingleTon.class) { if(instance==NULL) {ins and tance=NewSingleTon (); } } } returninstance;}
This is a more standard singleton mode, for the sake of security I put a lock on him, but this is not the best way to Write. The singleton pattern has two ways of writing, lazy writing and a hungry man writing.
Lazy: Initialize in a static method. Time to change space. (not recommended, Time is Important)
A hungry man: initializes the object as it is Declared. Space Change Time. (recommended, space is not a Problem)
therefore, in the actual development used more is the A hungry man notation, you can lock the class, when the variable declaration is Initialized. Specifically how to achieve here I do not introduce, you can Achieve.
# # # # #简单工厂设计模式 #####
A general definition of a simple factory design pattern: A simple factory is also called a static factory, and a factory object determines which product object to Create.
Realization Idea: Write a class, Let him create the object that we Want.
Code:
public classStudent Factory { public StaticStudent Getstudent (intType) { Switch(type) { casestudent Type. Learn god:return NewLearn God (); caseStudent Type. Genius:return NewGenius (); casetype of Student. weak learning:return NewLearning Weak (); caseStudent Type. School Slag:return NewLearning Slag (); default: break; } return NULL; }}
Through this factory, we can easily produce the objects we want. Here I use pseudo-code to express, in order to more image, the actual wording of this is absolutely not allowed!
# # # # #适配器设计模式 #####
The general definition of the adapter Pattern: a class inherits this adapter to implement the method we need to Implement.
Implementation Ideas: by writing an adapter class, which writes all the abstract methods, but these methods are empty, and the adapter class to be defined as abstract, if the adapter class can be implemented by itself is meaningless. The function of the adapter, inheriting the adaptor, re-requires a re-approach on it, simplifying the Operation.
Advantages * The client does not need to be responsible for the creation of objects, thus clarifying the responsibilities of each class, if there are new objects added, only need to add a specific class and the specific factory class, do not affect the existing code, late maintenance easy, enhance the system Extensibility. Disadvantages * Additional writing code required, increased workload
Code:
public class Adapter {public static void main (string[] Args) {a=new Alam (); a. Pop ();} interface fall in love {void to eat (); void to see a movie ( ); void Shopping (); void POPs (); } abstract class Love Sister implements Love {@Overridepublic void meal () {//TODO auto-generated method stub} @Overridepublic void Watch movie () {//todo auto-generated method stub} @Overridepublic void Shopping () {//todo auto-generated method stub} @Overridepublic Vo ID POPs () {//todo auto-generated method stub}} class Alam extends in love sister {public void PA () {//todo auto-generated met Hod stubSystem.out.println ("alam: asian-american father, Good shy");} }}
Here I have written an interface that defines several methods that need to be implemented when a class implements this Interface. When the adapter does not allow itself to implement these methods, it needs to be handed over to his subclass to inherit, let the subclass choose what code to implement, Android adapter is using the adapter Mode.
# # # # #模板设计模式 #####
The general definition of template design pattern: define an algorithm skeleton to give the implementation to Subclasses.
Implementation Method: define an abstract method in the class, the distance implementation by the subclass to implement
Code:
public class A { publicvoid run { ... void Howrun (); ........ public Abstract void Howrun ();} public class publicvoid howrun () () { ... }}
Advantages * Using the template method mode, in defining the algorithm skeleton, can be very flexible implementation of the specific algorithm, to meet the User's flexible needs of the shortcomings * if the algorithm skeleton is modified, you need to modify the abstract class
well, first introduced so much, I will slowly add, I hope this blog post to help You.
Several design patterns in Java