首页 > 代码库 > Decorator Pattern(装饰器模式)Demo1

Decorator Pattern(装饰器模式)Demo1

一个简单的装饰器模式例子,用来体现直接查询,缓存查询,和过滤敏感词汇

  1 import java.sql.Connection;
  2 import java.sql.PreparedStatement;
  3 import java.sql.ResultSet;
  4 import java.sql.SQLException;
  5 import java.util.ArrayList;
  6 import java.util.HashMap;
  7 import java.util.List;
  8 import java.util.Map;
  9 import util.DBConnection;
 10 
 11 /**
 12  *
 13  * @author NevinYang
 14  */
 15 class Student { // 查询结果类,相当于一个JavaBean
 16 
 17     private String Stu_id;
 18     private String Stu_name;
 19     private String Stu_sex;
 20     private String Stu_grade;
 21     private int Col_id;
 22     private int Pro_id;
 23 
 24     public String getStu_id() {
 25         return Stu_id;
 26     }
 27 
 28     public void setStu_id(String Stu_id) {
 29         this.Stu_id = Stu_id;
 30     }
 31 
 32     public String getStu_name() {
 33         return Stu_name;
 34     }
 35 
 36     public void setStu_name(String Stu_name) {
 37         this.Stu_name = Stu_name;
 38     }
 39 
 40     public String getStu_sex() {
 41         return Stu_sex;
 42     }
 43 
 44     public void setStu_sex(String Stu_sex) {
 45         this.Stu_sex = Stu_sex;
 46     }
 47 
 48     public String getStu_grade() {
 49         return Stu_grade;
 50     }
 51 
 52     public void setStu_grade(String Stu_grade) {
 53         this.Stu_grade = Stu_grade;
 54     }
 55 
 56     public int getCol_id() {
 57         return Col_id;
 58     }
 59 
 60     public void setCol_id(int Col_id) {
 61         this.Col_id = Col_id;
 62     }
 63 
 64     public int getPro_id() {
 65         return Pro_id;
 66     }
 67 
 68     public void setPro_id(int Pro_id) {
 69         this.Pro_id = Pro_id;
 70     }
 71 
 72     @Override
 73     public String toString() {
 74         return "Student{" + "Stu_id=" + Stu_id + ", Stu_name=" + Stu_name + ", Stu_sex=" + Stu_sex + ", Stu_grade=" + Stu_grade + ", Col_id=" + Col_id + ", Pro_id=" + Pro_id + ‘}‘;
 75     }
 76 }
 77 
 78 interface Query { // 查询接口,有简单查询和缓存查询
 79 
 80     public Student query(String sql);
 81 }
 82 
 83 class SimpleQuery implements Query { // 简单查询,相当于直接查询数据库,这里直接返回Result,相当于是数据库查询的结果
 84 
 85     @Override
 86     public Student query(String sql) {
 87         Student student = new Student();
 88         Connection conn = DBConnection.getConnection();
 89         PreparedStatement pstmt = null;
 90         ResultSet rs = null;
 91         try {
 92             pstmt = conn.prepareStatement(sql);
 93             rs = pstmt.executeQuery();
 94             if (rs.next()) {
 95                 student.setStu_id(rs.getString(1));
 96                 student.setStu_name(rs.getString(2));
 97                 student.setStu_sex(rs.getString(3));
 98                 student.setStu_grade(rs.getString(4));
 99                 student.setCol_id(rs.getInt(5));
100                 student.setPro_id(rs.getInt(6));
101             }
102 
103         } catch (SQLException e) {
104             System.out.println("操作中出现错误:" + e.getMessage());
105         } finally {
106             DBConnection.close(conn, pstmt, rs);
107         }
108         return student;
109     }
110 }
111 
112 class CacheQuery implements Query { // 缓存查询,如果查询相同的sql,不直接查询数据库,而是返回map中存在的Result
113 
114     private Query query;
115     private Map<String, Student> cache = new HashMap<>();
116 
117     public CacheQuery(Query query) {
118         //此时这个query就为SimpQuery
119         this.query = query;
120     }
121 
122     @Override
123     public Student query(String sql) {
124         if (cache.containsKey(sql)) {
125             return cache.get(sql);
126         }
127         Student student = query.query(sql);
128         System.out.println("执行了推入操作");
129         cache.put(sql, student);
130         return student;
131     }
132 }
133 
134 class FilterQuery implements Query {
135     //过滤敏感词汇
136     private Query query;
137     private List<String> words = new ArrayList<>();
138 
139     public FilterQuery(Query query) {
140         this.query = query;
141         words.add("杨晓龙");
142         words.add("杨晓亮");
143     }
144 
145     @Override
146     public Student query(String sql) {
147         for (String word : words) {
148             if (sql.contains(word)) {
149                 return null;
150             }
151         }
152         return query.query(sql);
153     }
154 }
155 
156 public class Test {
157 
158     public static void main(String args[]) {
159 
160         Query simpleQuery = new SimpleQuery();
161         Student student1 = simpleQuery.query("SELECT * FROM student WHERE Stu_id=1");
162         Student student2 = simpleQuery.query("SELECT * FROM student WHERE Stu_id=2");
163         System.out.println(student1);
164         System.out.println(student2);
165         System.out.println(student1 == student2);
166         System.out.println("---------------------------------------");
167 
168         Query cacheQuery = new CacheQuery(simpleQuery);
169         Student student3 = cacheQuery.query("SELECT * FROM student WHERE Stu_id=1");
170         Student student4 = cacheQuery.query("SELECT * FROM student WHERE Stu_id=1");
171         System.out.println(student3);
172         System.out.println(student4);
173         System.out.println(student3 == student4);
174         System.out.println("---------------------------------------");
175 
176         Query filterQuery = new FilterQuery(simpleQuery);
177         Student filter1 = filterQuery.query("SELECT * FROM student WHERE Stu_name=‘杨晓龙‘");  // null
178         System.out.println(filter1);
179         Student filter2 = filterQuery.query("SELECT * FROM student WHERE Stu_name=‘杨晓亮‘");  // null
180         System.out.println(filter2);
181 
182     }
183 }

 

对数据库的操作类:

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 /**
 8  *
 9  * @author NevinYang
10  */
11 public class DBConnection {
12 
13     public static Connection getConnection() {
14         Connection conn = null;
15         try {
16             Class.forName("com.mysql.jdbc.Driver");//帮助建立连接
17             String username = "root";
18             String password = "123456";
19             String url = "jdbc:mysql://localhost:3306/student?"
20                     + "useUnicode=true&characterEncoding=UTF-8";
21             conn = DriverManager.getConnection(url, username, password);
22         } catch (ClassNotFoundException e) {
23             System.out.println(e.getMessage());
24         } catch (SQLException e) {
25             System.out.println(e.getMessage());
26         }
27         return conn;
28     }
29 
30     public static void close(Connection conn,
31             PreparedStatement pstmt, ResultSet rs) {
32         try {
33             //判断rs 等是否为空?
34             //如果为空会出现空指针异常
35             if (rs != null) {
36                 rs.close();
37             }
38             if (pstmt != null) {
39                 pstmt.close();
40             }
41             if (conn != null) {
42                 conn.close();
43             }
44 
45         } catch (Exception e) {
46             e.printStackTrace();
47         }
48     }
49 }

数据库创建语句:

create table student(
    Stu_id varchar(32) NOT NULL,
    Stu_name varchar(32) DEFAULT NULL,
    Stu_sex varchar(32) DEFAULT NULL,
    Stu_grade varchar(32) DEFAULT NULL,
    Col_id int NOT NULL,
    Pro_id int NOT NULL,
    PRIMARY KEY (Stu_id)
) DEFAULT CHARSET=utf8

 

Decorator Pattern(装饰器模式)Demo1