[go: nahoru, domu]

Skip to content

Commit

Permalink
享元模式,组合模式
Browse files Browse the repository at this point in the history
  • Loading branch information
rmzf9192 committed Jan 2, 2020
1 parent 9c02214 commit 57d611a
Show file tree
Hide file tree
Showing 16 changed files with 408 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.el.jichu.designpattern.composite.test;

public class Client {

public static void main(String[] args) {
//从大到小创建对象 学校
OrganizationComponent university = new University("清华大学", " 中国顶级大学 ");

//创建 学院
OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 ");
OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 ");


//创建各个学院下面的系(专业)
computerCollege.add(new Department("软件工程", " 软件工程不错 "));
computerCollege.add(new Department("网络工程", " 网络工程不错 "));
computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 "));

//
infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 "));
infoEngineercollege.add(new Department("信息工程", " 信息工程好学 "));

//将学院加入到 学校
university.add(computerCollege);
university.add(infoEngineercollege);

//university.print();
infoEngineercollege.print();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.el.jichu.designpattern.composite.test;

import java.util.ArrayList;
import java.util.List;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:36
* @Version V1.0
*/
public class College extends OrganizationComponent {

//List 中 存放的Department
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

// 构造器
public College(String name, String des) {
super(name, des);
}

// 重写add
@Override
protected void add(OrganizationComponent organizationComponent) {
// 将来实际业务中,Colleage 的 add 和 University add 不一定完全一样
organizationComponents.add(organizationComponent);
}

// 重写remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}

@Override
public String getName() {
return super.getName();
}

@Override
public String getDes() {
return super.getDes();
}

// print方法,就是输出University 包含的学院
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
//遍历 organizationComponents
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.el.jichu.designpattern.composite.test;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:38
* @Version V1.0
*/
public class Department extends OrganizationComponent {
//没有集合

public Department(String name, String des) {
super(name, des);
}


//add , remove 就不用写了,因为他是叶子节点

@Override
public String getName() {
return super.getName();
}

@Override
public String getDes() {
return super.getDes();
}

@Override
protected void print() {
System.out.println(getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.el.jichu.designpattern.composite.test;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:35
* @Version V1.0
*/
public abstract class OrganizationComponent {
private String name; // 名字
private String des; // 说明

protected void add(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}

protected void remove(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}

//构造器
public OrganizationComponent(String name, String des) {
super();
this.name = name;
this.des = des;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDes() {
return des;
}

public void setDes(String des) {
this.des = des;
}

//方法print, 做成抽象的, 子类都需要实现
protected abstract void print();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.el.jichu.designpattern.composite.test;

import java.util.ArrayList;
import java.util.List;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:39
* @Version V1.0
*/
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

// 构造器
public University(String name, String des) {
super(name, des);
}

// 重写add
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}

// 重写remove
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}

@Override
public String getName() {
return super.getName();
}

@Override
public String getDes() {
return super.getDes();
}

// print方法,就是输出University 包含的学院
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
//遍历 organizationComponents
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.el.jichu.designpattern.flyweight.test;

public class Client {

public static void main(String[] args) {
// 创建一个工厂类
WebSiteFactory factory = new WebSiteFactory();

// 客户要一个以新闻形式发布的网站
WebSite webSite1 = factory.getWebSiteCategory("新闻");


webSite1.use(new User("tom"));

// 客户要一个以博客形式发布的网站
WebSite webSite2 = factory.getWebSiteCategory("博客");

webSite2.use(new User("jack"));

// 客户要一个以博客形式发布的网站
WebSite webSite3 = factory.getWebSiteCategory("博客");

webSite3.use(new User("smith"));

// 客户要一个以博客形式发布的网站
WebSite webSite4 = factory.getWebSiteCategory("博客");

webSite4.use(new User("king"));

System.out.println("网站的分类共=" + factory.getWebSiteCount());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.el.jichu.designpattern.flyweight.test;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:59
* @Version V1.0
*/
public class ConcreteWebSite extends WebSite {
//共享的部分,内部状态
private String type = ""; //网站发布的形式(类型)

//构造器
public ConcreteWebSite(String type) {

this.type = type;
}
@Override
public void use(User user) {
System.out.println("网站的发布形式为:" + type + " 在使用中 .. 使用者是" + user.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.el.jichu.designpattern.flyweight.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

/**
* @Auther: roman.zhang
* @Date: 2018/12/28 10:39
* @Version:V1.0
* @Description:ConnectionPool:数据库连接池代码 通过连接池的管理,实现了数据库连接的共享,不需要每一次都重新创建连接,
* 节省了数据 库重新创建的开销,提升了系统的性能!
*/
public class ConnectionPool {

private Vector<Connection> pool;

//公有属性
private String url = "jdbc:mysql://localhost:3306/test";

private String username = "root";

private String password = "root";
private String driverClassName = "com.mysql.jdbc.Driver";

private int poolSize = 100;

private static ConnectionPool instance = null;

Connection connection = null;

//构造方法,做一些初始化工作
private ConnectionPool() {
pool = new Vector<Connection>(poolSize);

for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
connection = DriverManager.getConnection(url, username, password);
pool.add(connection);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

//返回连接到数据库连接池
public synchronized void release() {
pool.add(connection);
}

//返回连接到一个数据库连接池
public synchronized Connection getConnection() {
if (pool.size() > 0) {
Connection connection = pool.get(0);
pool.remove(connection);
return connection;
} else {
return null;
}
}


}
23 changes: 23 additions & 0 deletions src/main/java/com/el/jichu/designpattern/flyweight/test/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.el.jichu.designpattern.flyweight.test;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:58
* @Version V1.0
*/
public class User {
private String name;

public User(String name) {
super();
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.el.jichu.designpattern.flyweight.test;

/**
* @author roman zhangfei
* @Date 2019/12/18 16:58
* @Version V1.0
*/
public abstract class WebSite {
public abstract void use(User user);//抽象方法
}
Loading

0 comments on commit 57d611a

Please sign in to comment.