Today to share with you a small edition of their own Java design pattern in-depth research PDF, everyone to take a closer look!
There are 12 chapters: Interface and Abstract Classes, reflection, Factory pattern, Generator pattern, Observer pattern, Bridge pattern, Proxy pattern, state pattern, Visitor pattern, command pattern, decorator pattern, composite pattern.
Interfaces and abstract classes
Simple semantic description
Interface and abstract class are two important concepts in object-oriented thought. An interface is only a collection of method definitions and constant value definitions. Methods have no function body. Abstract classes can theoretically define much more than what is in an interface. They can define everything that a normal class contains, as well as abstract methods, which is why they are called abstract classes. Interfaces and abstract classes themselves cannot be exemplified. Abstract methods must be implemented in the corresponding subclasses to obtain applications.
So how do you better understand interfaces and abstract classes? Why abstract classes when an interface can define abstract methods? Abstract methods have no function body and cannot be exemplified, indicating that interfaces and abstract classes themselves have no purpose. Is this definition meaningful? How do interfaces relate to abstract classes?
The best way to get answers to these questions is through life experience. For example, when writing a composition, we must think about what to write first and what to write after; Do geometric problems, to think clearly how to lead the auxiliary line, which axioms, theorems, etc.; When doing research work, think about what key problems must be solved; Before the factory produces the product, must make the perfect production plan. That is to say, before people do anything, they generally think well and then implement it. This pattern is commonplace in life. The Java language, therefore, must reflect the “think-implement” process, implemented with different keywords, namely interfaces. Abstract classes reflect the thinking stage, and subclasses reflect the implementation stage.
It is easy to conclude from the above that “think-implement” is the simple semantics of interfaces, abstract classes. From this point of view, combined with the reality of life, it is convenient to answer many questions to be answered, as follows.
Why can’t interfaces, abstract classes, be exemplified? Because interface, abstract class is the result of thinking, just put forward what problems need to be solved, do not need the concrete content of the function body, of course, can not be exemplified.
Why must interfaces, abstract classes, be implemented by subclasses? When a problem is presented, there must be a solution. The Java language is implemented through subclasses, of course, which must solve all the problems raised during the “thinking” process.
What is the difference between interfaces and abstract classes? It can be considered that there are generally two kinds of questions raised by human beings after thinking: one is the “order” problem, and the other is the “order + sharing” problem. The former is described by interfaces, while the latter is described by abstract classes.
Figure 1-1 shows an example of a specific interface for producing a car.
As shown in Figure 1-1 (a), car production can be composed of four processes: steel plate cutting, molding, assembly and painting. These operations are sequential, so it is most appropriate to convert them into interfaces, as shown in Figure 1-1 (b).
Figure 1-1 Example of interface for producing a car
reflection
The concept of reflection:
Java reflection (Java reflection) is a mechanism to obtain information about a class or an existing object with a known name when the program is running, including the method, attribute, parent class and other information of the class, as well as the creation of an example and the determination of the type of an example. In normal programming, we call class objects and their associated methods explicitly. For example,
public class A{Copy the code
vold func() {}Copy the code
public statie vold main(String []args){Copy the code
A obj= new A();Copy the code
obj. func();Copy the code
}Copy the code
}Copy the code
Can the class name A,① list the properties and methods of this class, ② call any methods of any object? This is also the meaning of “reverse mapping – reflection”.
In the JDK, Java reflection is implemented primarily by the following classes, which are in the Jav.LangReflect package.
●Class: Represents a Class.
●Constructor class: Represents a class Constructor.
●Field class: Represents the member variables of the class (also known as attributes of the class).
●Method class: A Method that represents a class.
Because there are too many chapters, I will not give you a introduction, you can take their own hands and then go to see!
Obtaining method:
1. Forwarding attention to xiaobian;
2. Private letter xiaobian “learn” to take away ~