This article analyzes the design mode from the perspective of junior programmers. 1. What is the design model?
Simply put, it is a solution to specific problems.
2. What can design patterns help us?
We have a paradigm theory when designing databases. Do you still remember? I don't remember. I will review it later. The paradigm theory guides us to design database tables. It is a guiding principle for us to design databases, and its goal is to reduce redundancy. To put it simply, as a Java programmer, we write a variety of classes every day. These classes are not isolated, and they are connected as a whole, collaboration between different classes forms a huge program. So how to design these classes? Design Patterns are some principles that guide us to design these classes. You have to review what is high cohesion and low coupling.
3,
What design patterns have you used?
This guy was lucky because he just saw it yesterday.Singleton Mode. So the following dialog appears?
M indicates facial recognition, and Z indicatesZhang xiao'er: The problem is rough.
M: Since you will
Singleton ModeYou can write it out for me. (Let's see if I can't help you) Z:
PackageSingleton Mode; Class singleton2 {Private Static singleton2 instance = new singleton2 (); Private singleton2 () {system. out. println ("object created");} public static singleton2 getinstance () {return instance;} public class test6 {public static void main (string ARGs []) {singleton2 S1 = singleton2.getinstance (); singleton2 S2 = singleton2.getinstance ();
} }
Run the getinstace constructor twice and call it only once.
M; How do you ensure that each class only generates an object once?
1. Static variables of the class are initialized only once.
M: Yes, but you must have other methods. This method is flawed, because class initialization does not necessarily appear only when getinstace is called.
Z: Yes. Even if we do not call getinstacne, the object may be created. Therefore, we cannot create on demand.
For example:
Class. forname ("Singleton Mode. Singleton2 ");In simple terms, this is the loading class, which is to load the class file to the virtual machine. This has a lot to do with reflection, both in dynamic proxy and factory mode, and will be reviewed later.
Call other static methods at the same time
M: Good. Your skills are good. You can write a thread safe and create it as needed.
Singleton ModeRight
Z:Zhang xiao'er, Pen writing,
PackageSingleton Mode; Class singleton2 {private singleton2 () {system. out. println ("object created");} Private Static class sigleton2holder {Private Static singleton2 instance = new singleton2 ();} public static singleton2 getinstance () {return sigleton2holder. instance ;}} public class test6 {public static void main (string ARGs []) {singleton2 S1 = singleton2.getinstance (); singleton2 S2 = singleton2.getinstance (); If (S1 = S2) {system. out. println ("the same object");} else {system. out. println ("different objects ");}}}
M: You are awesome. I asked you how you solve this problem. One class only creates one instance.
Z: I use a static internal class to obtain objects. For an internal class, its static variables are only initialized once, but I ensure that they are initialized only when getinstace is called.
(Here we need to review the internal class)
NextSingleton Mode, Mainly combined with multi-thread explanation, profound and profound
Http://www.cnblogs.com/coffee/archive/2011/12/05/inside-java-singleton.html