Internal classes, design patterns

Local inner class

It is equivalent to a local variable.
Note: When a local internal class accesses a local variable in an external method, final modification is required;
final can be omitted from 1.8, but the function of final still exists.
Local internal classes cannot have static declarations;
if it is The local inner class in the member method can access all the properties of the outer class;
if it is the local inner class in the static method, it cannot directly access the member properties of the outer class;

Anonymous inner class

	指在方法调用时,实参需要传入某个接口对象的时候,不传入对象,传入一个匿名的实现类;
	如果方法定义形参 一般是个接口,那么调用的时候,就需要传入其实现类的对象
	但是可以不是实现这个接口,不传递实现类对象,传递一个匿名内部类,这个类没有名字,不能被复用;
	默认的匿名内部类类名:外部类类名$1 以此类推
	那么既然是没有名字的类,那么自然无法创建对象,但是传入匿名内部类的时候会自动创建一个对象;

Design Patterns

Overview: Represents the best implementation method;
we directly avoid many bugs based on the experience and lessons summarized by the predecessors;
a set of implementation schemes to improve code efficiency and maintainability;
there are currently 23 mature design patterns ;

Singleton mode

	目的:是某个类仅创建一个对象;

Implementation idea:
First, we must create an object. The object is created by the construction method. If we want to control the power of creating the object in our hands, then we cannot allow the user to directly access our construction method.
Then we create an object to implement it.

Implementation steps:
①Privatization of the construction method
First of all, we must create objects (control should be in our hands) to control the number of objects created; the
construction method is used to create objects, so we need to control the construction method; do not allow users to access the construction method;
how achieve? The construction method is privatized, and the user has no access rights, and naturally cannot create the object.
②Create a public static method to return the current class object and ensure that it is instantiated only once (create an object).
We should create an object to the user. How to pass it? Provide a public method, call this method to return an object;
then this method should be a static method or a member method?
If it is a member method, you need to create an object to call the method, and what we need now is to use this method to call the object, an endless loop;
so it should be a static method to get the object, and the static method is called with a class to ensure that it is instantiated once;
③Create a privatized static variable to store the object of the current class (the type of the variable is the type of the current class).
At this time, a variable to store the object is required, a static variable (related to the life cycle of the class);
if it is a local variable , Each call is initialized, that is, the object is instantiated;
if it is a member variable, the member variable cannot be directly manipulated in a static method;
so it can only be a static variable, and it should be a privatized static variable;
application scenario:
example: class In the class, if there is only one lecturer, the rest are students;
then the lecturer can create a singleton class;

According to the different creation timing of the object:
1. Lazy
man mode When the object is obtained for the first time, the object is created.
2. Hungry man mode
Static variables create static variables in the class loading stage. This static variable saves the object, which is equivalent to creating the object.

Factory mode

Separately encapsulate the object creation statement into a class, and when the object is needed, go to the factory to obtain it;
weaken the strong dependency relationship between the classes and have higher flexibility;
when creating the object, the creation logic will not be exposed to the client ; Shield the specific implementation of the product, the caller only cares about the interface of the product;
Decoupling: reduce the degree of coupling, the smaller the impact between the two, the better;

High cohesion and low coupling: usually refers to the relationship between classes, which is a criterion for judging the quality of software;
enhanced portability;

Hard code: The source code needs to be changed for each compilation;

Guess you like

Origin blog.csdn.net/MIRACLE_Ying/article/details/112855058