JavaSE custom annotation + Design Patterns

Built-in annotation:

(1) @SuppressWarnings then be removed program preceded by a warning javac compilation - phase is the SOURCE
the SOURCE (2) @Deprecated marked packet, the method, which field specifies obsolete ---- stage
(3) @Overricle Applying this marker shows that the method is a method to rewrite the parent class - stage SOURCE

Custom annotation:

Meta-annotation role is responsible for other annotation notes. Java5.0 standard defines four meta-annotation type, which are used to provide other types of annotation specified. Java5.0 element annotation definition:
@Target
@Target illustrates the modified target range Annotation: Annotation may be used for packages, types (classes, interfaces, enumerations, Annotation type), a member type (method, constructor, members variables enumerated value), method arguments and local variables (such as a loop variable, the catch parameter). Use a target in the Annotation type declaration can be more clear that it modifies the target.
1.CONSTRUCTOR: constructor is used to describe
2.FIELD: for describing fields
3.LOCAL_VARIABLE: local variables used to describe
4.METHOD: a method for describing
5.PACKAGE: Package for describing
6.PARAMETER: parameter used to describe the
7.TYPE: used to describe the class, interface (including annotation type), or enum declaration
2. @ Retention
expressed the need to save the annotation information on what level, is used to describe the life cycle of notes (ie: what annotations are described in the range of effective)
3. @ of Documented
@Documented annotation indicates that the notes should be recorded javadoc tool. by default, javadoc does not include annotations. However, if the statement notes specified @Documented, it will be treated like javadoc tool, so annotation type information will also be included in the generated document, it is a marker annotation, no members.
4. @ Inherited
Indicating annotation type is automatically inherited. If there is succession in the annotation type declaration yuan notes, annotation types and user queries on the class declaration, the class and the class declaration no comment, then the class superclass will automatically be queried annotation types. This process is repeated until you find this type of comment, or reach the top of the class hierarchy (object).

Example:

Notes categories:

@Retention(RetentionPolicy.RUNTIME)
@Target(value= {ElementType.TYPE})
@interface Table{
String name();
}

@Retention(RetentionPolicy.RUNTIME)
@interface Property{
String name() default “”;
int lenght() default 0;
}

Entity classes:

@Table(name=“Student”)
public class NoteCode {
@Property(name=“student_name”,lenght=12)
private String studentName;
@Property(name=“student_age”,lenght=12)
private Integer studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}

}

Test category

static PreparedStatement stmt = null;
static ResultSet rs = null;
static Connection connection=null;
public static void main(String[] args) throws Exception {
	try {
		Class<?> forName = Class.forName("com.hm.NoteCode");//拿到类对象
		Field[] declaredFields = forName.getDeclaredFields();//获取属性
		StringBuffer strbuf=new StringBuffer();//拼接容器
		strbuf.append("select ");
		for (int i = 0; i < declaredFields.length; i++) {
			Field field = declaredFields[i];
			  Property declaredAnnotation = field.getDeclaredAnnotation(Property.class);
			  String name = declaredAnnotation.name();//获取属性值
			strbuf.append(name);
			if(i<declaredFields.length-1) {
				strbuf.append(",");
			}
		}
		Table table = forName.getDeclaredAnnotation(Table.class);
		String name = table.name();//获取表对象
		strbuf.append(" from "+name);
		stmt= connection.prepareStatement(strbuf.toString());
		rs=stmt.executeQuery();
		if (!rs.next()) {
			throw new Exception("查询无结果!");
		}
		System.out.println(rs.toString());
	}catch (Exception e) {
		if(rs!=null) {
			rs.close();
		}
		if(stmt!=null) {
			stmt.close();
		}
		if(connection!=null) {
			connection.close();
		}
	}	

}

Design Patterns

1, singleton

2, factory pattern
3, proxy mode

Six principles of design patterns

1, the opening and closing principle (Open Close Principle)
opening and closing principle that is open for extension but closed for modification. When the program needs to expand, not to modify the existing code to achieve the effect of a hot-swappable. So summarized in one sentence: In order to make the program better scalability, ease of maintenance and upgrades. Want to achieve this effect, we need to use interfaces and abstract classes, follow the detailed design, we will mention this point.
2, Richter substitution principle (Liskov Substitution Principle)
Richter substitution principle (Liskov Substitution Principle LSP) One of the basic principles of object-oriented design. Richter substitution principle in that place any base class can appear, sub-class will be able to appear. LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, the software unit of functionality is not affected, the base class can really be reused, derived class can add new behavior on the basis of the base class on . Richter substitution principle is - to add "on-off" principle. To achieve "open - closed" principle is a key step in abstraction. The inheritance relationship with a base class is a subclass of the abstract concrete realization, so Richter substitution principle is the specification for concrete steps to achieve the abstract. - From Baidu Encyclopedia
3, Dependency Inversion Principle (Dependence Inversion Principle)
Based on this principle of opening and closing, the specific content: true on programming interfaces, dependent on the abstract does not depend on the specific.
4, the interface segregation principle (Interface Segregation Principle)
the meaning of this principle is: the use of a plurality of interface isolation, is better than using a single interface. Or a lower degree of coupling between classes mean, from here we see that, in fact, a design pattern is the design of software, from large-scale software architecture, in order to upgrade and easy maintenance. Therefore, the above multiple times: reduce dependence, reduce coupling.
5, the Law of Demeter (the least known principle) (Demeter Principle)
Why call least known principles, that is to say: an entity should minimize the interaction between the entity and the other, such that the independent function modules.
6, the synthesis of multiplexing principles (Composite Reuse Principle)
principle is to use the synthetic polymeric info / instead of inheritance

Published 26 original articles · won praise 0 · Views 715

Guess you like

Origin blog.csdn.net/YHM_MM/article/details/102999727