##### Single case design mode #####
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 class SingleTon { private static SingleTon instance; Private SingleTon () {}; public static SingleTon newinstance () { if (instance==null) { synchronized (singleton.class) { if (instance ==null) { ins and tance=new SingleTon (); }}} return instance;}
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.
##### Simple Factory design mode #####
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 class student Factory {public static student getstudent (int type) { switch (type) {case student type. Learning God: return to new studies God (); Case student type. Genius: return new Genius (); Case student type. Learning Weak: return new learning Weak (); Case student type. Learning slag: return new 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!
##### Adapter design mode #####
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's extensibility. Disadvantages* Additional writing code required, increased workload
Code:
| 123456789Ten One A - - the - - - + - + A at - - - - - in - to + - the * $Panax Notoginseng - the + A the + - |
public class Adapter { public static void main(String[] args) { 阿乐 a=new 阿乐(); a.啪啪啪(); } interface 谈恋爱 { void 吃饭(); void 看电影(); void 逛街(); void 啪啪啪(); } abstract class 恋爱中的妹子 implements 谈恋爱{ @Override public void 吃饭() { // TODO Auto-generated method stub } @Override public void 看电影() { // TODO Auto-generated method stub } @Override public void 逛街() { // TODO Auto-generated method stub } @Override public void 啪啪啪() { // TODO Auto-generated method stub } } class 阿乐 extends 恋爱中的妹子{ public void 啪啪啪() { // TODO Auto-generated method stub System.out.println("阿乐:亚美爹,好害羞"); } }} |
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.
##### Template design 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 {public final void run { ... void Howrun (); ........ } public abstract void howrun ();} public class B extends A {public void Howrun () { ... }}
Advantages* Using the template method mode, in defining the algorithm skeleton, can be very flexible to achieve the specific algorithm to meet the user's flexible needsDisadvantages* If the algorithm skeleton is modified, you need to modify the abstract class
Several design patterns of Java