(十一)合成模式 Composite
合成模式是一组对象的组合,这些对象可以是容器对象,也可以是单对象。组对象允许包含单对象,也可以包含其他组对象,要为组合对象和单对象定义共同的行为。合成模式的意义是 保证客户端调用单对象与组合对象的一致性。
class TreeNode{
private String name;
private TreeNode parent;
private Vector<TreeNode> children = new Vector<TreeNode>();
public TreeNode(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TreeNode getParent() {
return parent;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public void setChildren(Vector<TreeNode> children) {
this.children = children;
}
//添加孩子节点
public void add(TreeNode node){
children.add(node);
}
//删除孩子节点
public void remove(TreeNode node){
children.remove(node);
}
//取得孩子节点
public Enumeration<TreeNode> getChildren(){
return children.elements();
}
}
public class Composite {
TreeNode root = null;
public Composite(String name){
root = new TreeNode(name);
}
public static void main(String[] args){
Composite com = new Composite("A");
TreeNode nodeb = new TreeNode("B");
TreeNode nodec = new TreeNode("C");
nodeb.add(nodec);
com.root.add(nodeb);
}
}
(十二) 享元模式 Flyweight
享元模式的主要目的是实现对象的共享,当系统中存在大量对象的时候减少内存开销,通常与工厂模式一同使用。一个客户端请求时候,工厂检查当前对象池是否存在可用的对象,有就返回已经存在的对象。没有就创建一个新对象。数据库连接池就是最典型的运用享元模式的例子。
public class ConnectionPool implements IConnectionPool{
private DBbean dbBean; //连接池配置属性
private boolean isActive = false; //连接池活动状态
private int contActive = 0; //记录创建的总连接数
private List<Connection> freeConnection = new Vector<Connection>();
private List<Connection> activeConnection = new Vector<>();
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
public ConnectionPool(DBbean dbBean){
super();
this.dbBean = dbBean;
init();
cheackPool();
}
public void init(){
try {
Class.forName(dbBean.getDriverName());
for(int i = 0;i < dbBean.getInitConnections();i++){
Connection conn;
conn = newConnection();
if(conn != null){
freeConnection.add(conn);
contActive++;
}
}
isActive = true;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
//获得当前连接数
public Connection getCurrentConnection(){
Connection conn = threadLocal.get();
if(!isValid(conn)){
conn = getConnection();
}
return conn;
}
// 获得连接
public synchronized Connection getConnection() {
Connection conn = null;
try {
// 判断是否超过最大连接数限制
if(contActive < this.dbBean.getMaxActiveConnections()){
if (freeConnection.size() > 0) {
conn = freeConnection.get(0);
if (conn != null) {
threadLocal.set(conn);
}
freeConnection.remove(0);
} else {
conn = newConnection();
}
}else{
// 继续获得连接,直到从新获得连接
wait(this.dbBean.getConnTimeOut());
conn = getConnection();
}
if (isValid(conn)) {
activeConnection.add(conn);
contActive ++;
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return conn;
}
// 获得新连接
private synchronized Connection newConnection()
throws ClassNotFoundException, SQLException {
Connection conn = null;
if (dbBean != null) {
Class.forName(dbBean.getDriverName());
conn = DriverManager.getConnection(dbBean.getUrl(),
dbBean.getUserName(), dbBean.getPassword());
}
return conn;
}
// 释放连接
public synchronized void releaseConn(Connection conn) throws SQLException {
if (isValid(conn)&& !(freeConnection.size() > dbBean.getMaxConnections())) {
freeConnection.add(conn);
activeConnection.remove(conn);
contActive --;
threadLocal.remove();
// 唤醒所有正待等待的线程,去抢连接
notifyAll();
}
}
// 判断连接是否可用
private boolean isValid(Connection conn) {
try {
if (conn == null || conn.isClosed()) {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
// 销毁连接池
public synchronized void destroy() {
for (Connection conn : freeConnection) {
try {
if (isValid(conn)) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
for (Connection conn : activeConnection) {
try {
if (isValid(conn)) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
isActive = false;
contActive = 0;
}
// 连接池状态
@Override
public boolean isActive() {
return isActive;
}
// 定时检查连接池情况
@Override
public void cheackPool() {
if(dbBean.isCheakPool()){
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// 1.对线程里面的连接状态
// 2.连接池最小 最大连接数
// 3.其他状态进行检查,因为这里还需要写几个线程管理的类,暂时就不添加了
System.out.println("空线池连接数:"+freeConnection.size());
System.out.println("活动连接数::"+activeConnection.size());
System.out.println("总的连接数:"+contActive);
}
},dbBean.getLazyCheck(),dbBean.getPeriodCheck());
}
}
}