-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/main/java/com/el/jichu/designpattern/composite/test/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/el/jichu/designpattern/composite/test/College.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/el/jichu/designpattern/composite/test/Department.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/el/jichu/designpattern/composite/test/OrganizationComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/el/jichu/designpattern/composite/test/University.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/java/com/el/jichu/designpattern/composite/test/composite.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便 |
Binary file added
BIN
+96.9 KB
src/main/java/com/el/jichu/designpattern/composite/test/学校院系专业组合关系图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions
33
src/main/java/com/el/jichu/designpattern/flyweight/test/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/el/jichu/designpattern/flyweight/test/ConcreteWebSite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/el/jichu/designpattern/flyweight/test/ConnectionPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/java/com/el/jichu/designpattern/flyweight/test/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/el/jichu/designpattern/flyweight/test/WebSite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);//抽象方法 | ||
} |
Oops, something went wrong.