JavaSE自定义注解+设计模式

内置注解:

(1) @SuppressWarnings 再程序前面加上可以在javac编译中去除警告–阶段是SOURCE
(2) @Deprecated 带有标记的包,方法,字段说明其过时----阶段是SOURCE
(3)@Overricle 打上这个标记说明该方法是将父类的方法重写–阶段是SOURCE

自定义注解:

元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
@Target
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.@Retention
表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
3.@Documented
@Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员。
4.@Inherited
指示批注类型是自动继承的。如果在注释类型声明中存在继承的元注释,并且用户在类声明上查询注释类型,并且类声明对该类没有注释,那么该类的超类将自动被查询到注释类型。这个过程将被重复,直到找到这个类型的注释,或者到达类层次结构(对象)的顶端。

示例:

注解类:

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

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

实体类:

@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;
}

}

测试类

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();
		}
	}	

}

设计模式

1、单例模式

2、工厂模式
3、代理模式

设计模式的六大原则

1、开闭原则(Open Close Principle)
开闭原则就是说对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。所以一句话概括就是:为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。
2、里氏代换原则(Liskov Substitution Principle)
里氏代换原则(Liskov Substitution Principle LSP)面向对象设计的基本原则之一。 里氏代换原则中说,任何基类可以出现的地方,子类一定可以出现。 LSP是继承复用的基石,只有当衍生类可以替换掉基类,软件单位的功能不受到影响时,基类才能真正被复用,而衍生类也能够在基类的基础上增加新的行为。里氏代换原则是对“开-闭”原则的补充。实现“开-闭”原则的关键步骤就是抽象化。而基类与子类的继承关系就是抽象化的具体实现,所以里氏代换原则是对实现抽象化的具体步骤的规范。—— From Baidu 百科
3、依赖倒转原则(Dependence Inversion Principle)
这个是开闭原则的基础,具体内容:真对接口编程,依赖于抽象而不依赖于具体。
4、接口隔离原则(Interface Segregation Principle)
这个原则的意思是:使用多个隔离的接口,比使用单个接口要好。还是一个降低类之间的耦合度的意思,从这儿我们看出,其实设计模式就是一个软件的设计思想,从大型软件架构出发,为了升级和维护方便。所以上文中多次出现:降低依赖,降低耦合。
5、迪米特法则(最少知道原则)(Demeter Principle)
为什么叫最少知道原则,就是说:一个实体应当尽量少的与其他实体之间发生相互作用,使得系统功能模块相对独立。
6、合成复用原则(Composite Reuse Principle)
原则是尽量使用合成/聚合的方式,而不是使用继承

发布了26 篇原创文章 · 获赞 0 · 访问量 715

猜你喜欢

转载自blog.csdn.net/YHM_MM/article/details/102999727