├── .gitignore
├── AbstractFactoryPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── bean
│ │ ├── Product.java
│ │ ├── Role.java
│ │ └── User.java
│ │ ├── client
│ │ └── Client.java
│ │ ├── dao
│ │ ├── product
│ │ │ ├── IProductDao.java
│ │ │ ├── MySQLProductDao.java
│ │ │ ├── OracleProductDao.java
│ │ │ └── PostgreSQLProductDao.java
│ │ ├── role
│ │ │ ├── IRoleDao.java
│ │ │ ├── MySQLRoleDao.java
│ │ │ ├── OracleRoleDao.java
│ │ │ └── PostgreSQLRoleDao.java
│ │ └── user
│ │ │ ├── IUserDao.java
│ │ │ ├── MySQLUserDao.java
│ │ │ ├── OracleUserDao.java
│ │ │ └── PostgreSQLUserDao.java
│ │ └── factory
│ │ ├── IDaoFactory.java
│ │ ├── MySQLDaoFactory.java
│ │ ├── OracleDaoFactory.java
│ │ └── PostgreSQLDaoFactory.java
│ └── resources
│ └── log4j2.xml
├── AdapterPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── adaptee
│ │ └── Adaptee.java
│ │ ├── client
│ │ └── AdapterClient.java
│ │ └── target
│ │ ├── Adapter.java
│ │ ├── ConcreteTarget.java
│ │ └── ITarget.java
│ └── resources
│ └── log4j2.xml
├── BridgePattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── brand
│ │ ├── AbstractCar.java
│ │ ├── BMWCar.java
│ │ ├── BenZCar.java
│ │ └── LandRoverCar.java
│ │ ├── client
│ │ └── BridgeClient.java
│ │ └── transmission
│ │ ├── Auto.java
│ │ ├── Manual.java
│ │ └── Transmission.java
│ └── resources
│ └── log4j2.xml
├── CompositePattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── client
│ │ └── Client1.java
│ │ └── organization
│ │ ├── Company.java
│ │ ├── Department.java
│ │ └── Organization.java
│ └── resources
│ └── log4j2.xml
├── DynamicProxy
├── build.gradle
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── client
│ │ ├── CgLibProxyClient.java
│ │ ├── DynamicProxyPerfClient.java
│ │ ├── JDKDynamicProxyClient.java
│ │ └── StaticProxyClient.java
│ │ ├── proxy
│ │ ├── cglibproxy
│ │ │ └── SubjectInterceptor.java
│ │ ├── jdkproxy
│ │ │ ├── MultiProxyHandler.java
│ │ │ └── SubjectProxyHandler.java
│ │ └── staticproxy
│ │ │ ├── $Proxy17.java
│ │ │ └── ProxySubject.java
│ │ └── subject
│ │ ├── ConcreteSubject.java
│ │ └── ISubject.java
│ └── resources
│ └── log4j2.xml
├── FactoryMethodPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── bean
│ │ └── User.java
│ │ ├── client
│ │ └── Client.java
│ │ ├── dao
│ │ ├── IUserDao.java
│ │ ├── MySQLUserDao.java
│ │ ├── OracleUserDao.java
│ │ └── PostgreSQLUserDao.java
│ │ └── factory
│ │ ├── IDaoFactory.java
│ │ ├── MySQLDaoFactory.java
│ │ ├── OracleDaoFactory.java
│ │ └── PostgreSQLDaoFactory.java
│ └── resources
│ └── log4j2.xml
├── FlyweightPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── client
│ │ └── FlyWeightClient.java
│ │ ├── factory
│ │ └── FlyWeightFactory.java
│ │ └── flyweight
│ │ ├── ConcreteFlyWeight.java
│ │ ├── FlyWeight.java
│ │ └── UnSharedFlyWeight.java
│ └── resources
│ └── log4j2.xml
├── LICENSE
├── ObserverPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── client
│ │ └── Client1.java
│ │ ├── observer
│ │ ├── Architect.java
│ │ ├── ITalent.java
│ │ ├── JuniorEngineer.java
│ │ └── SeniorEngineer.java
│ │ └── subject
│ │ ├── AbstractHR.java
│ │ └── HeadHunter.java
│ └── resources
│ └── log4j2.xml
├── ProxyAndDecoratorPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── client
│ │ ├── DecoratorClient.java
│ │ └── StaticProxyClient.java
│ │ ├── decorator
│ │ ├── SubjectPostDecorator.java
│ │ └── SubjectPreDecorator.java
│ │ ├── proxy
│ │ └── ProxySubject.java
│ │ └── subject
│ │ ├── ConcreteSubject.java
│ │ └── ISubject.java
│ └── resources
│ └── log4j2.xml
├── README.md
├── SimpleFactoryPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── annotation
│ │ └── Vehicle.java
│ │ ├── client
│ │ ├── Driver1.java
│ │ ├── Driver2.java
│ │ ├── Driver3.java
│ │ ├── Driver4.java
│ │ ├── Driver5.java
│ │ └── JDBC.java
│ │ ├── factory
│ │ ├── CarFactory1.java
│ │ ├── CarFactory2.java
│ │ └── CarFactory3.java
│ │ └── product
│ │ ├── BMWCar.java
│ │ ├── BenzCar.java
│ │ ├── Car.java
│ │ └── LandRoverCar.java
│ └── resources
│ ├── car.xml
│ └── log4j2.xml
├── SingletonPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── singleton1
│ │ └── Singleton.java
│ │ ├── singleton2
│ │ └── Singleton.java
│ │ ├── singleton3
│ │ └── Singleton.java
│ │ ├── singleton4
│ │ └── Singleton.java
│ │ ├── singleton5
│ │ └── Singleton.java
│ │ ├── singleton6
│ │ └── Singleton.java
│ │ ├── singleton7
│ │ └── Singleton.java
│ │ ├── singleton8
│ │ └── Singleton.java
│ │ └── singleton9
│ │ └── Singleton.java
│ └── resources
│ └── log4j2.xml
├── StrategyPattern
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── jasongj
│ │ ├── annotation
│ │ └── Strategy.java
│ │ ├── client
│ │ ├── SimpleClient.java
│ │ └── SimpleFactoryClient.java
│ │ ├── context
│ │ ├── SimpleContext.java
│ │ └── SimpleFactoryContext.java
│ │ └── strategy
│ │ ├── ConcreteStrategyA.java
│ │ ├── ConcreteStrategyB.java
│ │ └── Strategy.java
│ └── resources
│ ├── log4j2.xml
│ └── strategy.xml
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | .settings
3 | .project
4 | .classpath
5 | build
6 | logs
7 | bin
8 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/bean/Product.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.bean;
2 |
3 | public class Product {
4 |
5 | private String productname;
6 | private double price;
7 |
8 | public String getProductname() {
9 | return productname;
10 | }
11 |
12 | public void setProductname(String productname) {
13 | this.productname = productname;
14 | }
15 |
16 | public double getPrice() {
17 | return price;
18 | }
19 |
20 | public void setPrice(double price) {
21 | this.price = price;
22 | }
23 |
24 | @Override
25 | public String toString() {
26 | return "{productname=" + productname + ", price=" + Double.toString(price) + "}";
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/bean/Role.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.bean;
2 |
3 | import org.apache.commons.lang.StringUtils;
4 |
5 | public class Role {
6 |
7 | private String rolename;
8 | private String[] permissions;
9 |
10 | public String getRolename() {
11 | return rolename;
12 | }
13 |
14 | public void setRolename(String rolename) {
15 | this.rolename = rolename;
16 | }
17 |
18 | public String[] getPermissions() {
19 | return permissions;
20 | }
21 |
22 | public void setPermissions(String[] permissions) {
23 | this.permissions = permissions;
24 | }
25 |
26 | @Override
27 | public String toString() {
28 | return "{rolename=" + rolename + ", permissions=" + StringUtils.join(permissions, ", ") + "}";
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/bean/User.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.bean;
2 |
3 | public class User {
4 |
5 | private String username;
6 | private char[] password;
7 |
8 | public String getUsername() {
9 | return username;
10 | }
11 |
12 | public void setUsername(String username) {
13 | this.username = username;
14 | }
15 |
16 | public char[] getPassword() {
17 | return password;
18 | }
19 |
20 | public void setPassword(char[] password) {
21 | this.password = password;
22 | }
23 |
24 | @Override
25 | public String toString() {
26 | return "{username=" + username + ", password=" + new String(password) + "}";
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/client/Client.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.bean.Product;
4 | import com.jasongj.bean.User;
5 | import com.jasongj.dao.product.IProductDao;
6 | import com.jasongj.dao.role.IRoleDao;
7 | import com.jasongj.dao.user.IUserDao;
8 | import com.jasongj.factory.IDaoFactory;
9 | import com.jasongj.factory.MySQLDaoFactory;
10 |
11 | public class Client {
12 |
13 | public static void main(String[] args) {
14 | IDaoFactory factory = new MySQLDaoFactory();
15 |
16 | IUserDao userDao = factory.createUserDao();
17 | User user = new User();
18 | user.setUsername("demo");
19 | user.setPassword("demo".toCharArray());
20 | userDao.addUser(user);
21 |
22 | IRoleDao roleDao = factory.createRoleDao();
23 | roleDao.getRole("admin");
24 |
25 | IProductDao productDao = factory.createProductDao();
26 | Product product = new Product();
27 | productDao.removeProduct(product);
28 |
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/product/IProductDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.product;
2 |
3 | import com.jasongj.bean.Product;
4 |
5 | public interface IProductDao {
6 |
7 | void addProduct(Product product);
8 | void removeProduct(Product product);
9 | Product getProduct(String productname);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/product/MySQLProductDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Product;
7 |
8 | public class MySQLProductDao implements IProductDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(MySQLProductDao.class);
11 |
12 | @Override
13 | public void addProduct(Product product) {
14 | LOG.info("MySQL added Product {}", product);
15 | }
16 |
17 | @Override
18 | public void removeProduct(Product product) {
19 | LOG.info("MySQL removed Product {}", product);
20 | }
21 |
22 | @Override
23 | public Product getProduct(String productname) {
24 | Product product = new Product();
25 | product.setProductname(productname);
26 | return product;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/product/OracleProductDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Product;
7 |
8 | public class OracleProductDao implements IProductDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(OracleProductDao.class);
11 |
12 | @Override
13 | public void addProduct(Product product) {
14 | LOG.info("Oracle added Product {}", product);
15 | }
16 |
17 | @Override
18 | public void removeProduct(Product product) {
19 | LOG.info("Oracle removed Product {}", product);
20 | }
21 |
22 | @Override
23 | public Product getProduct(String productname) {
24 | Product product = new Product();
25 | product.setProductname(productname);
26 | return product;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/product/PostgreSQLProductDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Product;
7 |
8 | public class PostgreSQLProductDao implements IProductDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(PostgreSQLProductDao.class);
11 |
12 | @Override
13 | public void addProduct(Product product) {
14 | LOG.info("PostgreSQL added Product {}", product);
15 | }
16 |
17 | @Override
18 | public void removeProduct(Product product) {
19 | LOG.info("PostgreSQL removed Product {}", product);
20 | }
21 |
22 | @Override
23 | public Product getProduct(String productname) {
24 | Product product = new Product();
25 | product.setProductname(productname);
26 | return product;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/role/IRoleDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.role;
2 |
3 | import com.jasongj.bean.Role;
4 |
5 | public interface IRoleDao {
6 |
7 | void addRole(Role role);
8 | void removeRole(Role role);
9 | Role getRole(String rolename);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/role/MySQLRoleDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.role;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Role;
7 |
8 | public class MySQLRoleDao implements IRoleDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(MySQLRoleDao.class);
11 |
12 | @Override
13 | public void addRole(Role role) {
14 | LOG.info("MySQL added Role {}", role);
15 | }
16 |
17 | @Override
18 | public void removeRole(Role role) {
19 | LOG.info("MySQL removed Role {}", role);
20 | }
21 |
22 | @Override
23 | public Role getRole(String rolename) {
24 | Role role = new Role();
25 | role.setRolename(rolename);
26 | return role;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/role/OracleRoleDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.role;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Role;
7 |
8 | public class OracleRoleDao implements IRoleDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(OracleRoleDao.class);
11 |
12 | @Override
13 | public void addRole(Role role) {
14 | LOG.info("Oracle added Role {}", role);
15 | }
16 |
17 | @Override
18 | public void removeRole(Role role) {
19 | LOG.info("Oracle removed Role {}", role);
20 | }
21 |
22 | @Override
23 | public Role getRole(String rolename) {
24 | Role role = new Role();
25 | role.setRolename(rolename);
26 | return role;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/role/PostgreSQLRoleDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.role;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.Role;
7 |
8 | public class PostgreSQLRoleDao implements IRoleDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(PostgreSQLRoleDao.class);
11 |
12 | @Override
13 | public void addRole(Role role) {
14 | LOG.info("PostgreSQL added Role {}", role);
15 | }
16 |
17 | @Override
18 | public void removeRole(Role role) {
19 | LOG.info("PostgreSQL removed Role {}", role);
20 | }
21 |
22 | @Override
23 | public Role getRole(String rolename) {
24 | Role role = new Role();
25 | role.setRolename(rolename);
26 | return role;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/user/IUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.user;
2 |
3 | import com.jasongj.bean.User;
4 |
5 | public interface IUserDao {
6 |
7 | void addUser(User user);
8 | void removeUser(User user);
9 | User getUser(String username);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/user/MySQLUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.user;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class MySQLUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(MySQLUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("MySQL added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("MySQL removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/user/OracleUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.user;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class OracleUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(OracleUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("Oracle added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("Oracle removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/dao/user/PostgreSQLUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao.user;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class PostgreSQLUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(PostgreSQLUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("PostgreSQL added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("PostgreSQL removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/factory/IDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.product.IProductDao;
4 | import com.jasongj.dao.role.IRoleDao;
5 | import com.jasongj.dao.user.IUserDao;
6 |
7 | public interface IDaoFactory {
8 |
9 | IUserDao createUserDao();
10 | IRoleDao createRoleDao();
11 | IProductDao createProductDao();
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/factory/MySQLDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.product.IProductDao;
4 | import com.jasongj.dao.product.MySQLProductDao;
5 | import com.jasongj.dao.role.IRoleDao;
6 | import com.jasongj.dao.role.MySQLRoleDao;
7 | import com.jasongj.dao.user.IUserDao;
8 | import com.jasongj.dao.user.MySQLUserDao;
9 |
10 | public class MySQLDaoFactory implements IDaoFactory {
11 |
12 | @Override
13 | public IUserDao createUserDao() {
14 | return new MySQLUserDao();
15 | }
16 |
17 | @Override
18 | public IRoleDao createRoleDao() {
19 | return new MySQLRoleDao();
20 | }
21 |
22 | @Override
23 | public IProductDao createProductDao() {
24 | return new MySQLProductDao();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/factory/OracleDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.product.IProductDao;
4 | import com.jasongj.dao.product.OracleProductDao;
5 | import com.jasongj.dao.role.IRoleDao;
6 | import com.jasongj.dao.role.OracleRoleDao;
7 | import com.jasongj.dao.user.IUserDao;
8 | import com.jasongj.dao.user.OracleUserDao;
9 |
10 | public class OracleDaoFactory implements IDaoFactory {
11 |
12 | @Override
13 | public IUserDao createUserDao() {
14 | return new OracleUserDao();
15 | }
16 |
17 | @Override
18 | public IRoleDao createRoleDao() {
19 | return new OracleRoleDao();
20 | }
21 |
22 | @Override
23 | public IProductDao createProductDao() {
24 | return new OracleProductDao();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/java/com/jasongj/factory/PostgreSQLDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.product.IProductDao;
4 | import com.jasongj.dao.product.PostgreSQLProductDao;
5 | import com.jasongj.dao.role.IRoleDao;
6 | import com.jasongj.dao.role.PostgreSQLRoleDao;
7 | import com.jasongj.dao.user.IUserDao;
8 | import com.jasongj.dao.user.PostgreSQLUserDao;
9 |
10 | public class PostgreSQLDaoFactory implements IDaoFactory {
11 |
12 | @Override
13 | public IUserDao createUserDao() {
14 | return new PostgreSQLUserDao();
15 | }
16 |
17 | @Override
18 | public IRoleDao createRoleDao() {
19 | return new PostgreSQLRoleDao();
20 | }
21 |
22 | @Override
23 | public IProductDao createProductDao() {
24 | return new PostgreSQLProductDao();
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/java/com/jasongj/adaptee/Adaptee.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.adaptee;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.target.ConcreteTarget;
7 |
8 | public class Adaptee {
9 |
10 | private static Logger LOGGER = LoggerFactory.getLogger(ConcreteTarget.class);
11 |
12 | public void onRequest() {
13 | LOGGER.info("Adaptee.onRequest()");
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/java/com/jasongj/client/AdapterClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.target.Adapter;
4 | import com.jasongj.target.ConcreteTarget;
5 | import com.jasongj.target.ITarget;
6 |
7 | public class AdapterClient {
8 |
9 | public static void main(String[] args) {
10 | ITarget adapter = new Adapter();
11 | adapter.request();
12 |
13 | ITarget target = new ConcreteTarget();
14 | target.request();
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/java/com/jasongj/target/Adapter.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.target;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.adaptee.Adaptee;
7 |
8 | public class Adapter implements ITarget {
9 |
10 | private static Logger LOG = LoggerFactory.getLogger(Adapter.class);
11 |
12 | private Adaptee adaptee = new Adaptee();
13 |
14 | @Override
15 | public void request() {
16 | LOG.info("Adapter.request");
17 | adaptee.onRequest();
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/java/com/jasongj/target/ConcreteTarget.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.target;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 |
7 | public class ConcreteTarget implements ITarget {
8 |
9 | private static Logger LOG = LoggerFactory.getLogger(ConcreteTarget.class);
10 |
11 | @Override
12 | public void request() {
13 | LOG.info("ConcreteTarget.request()");
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/java/com/jasongj/target/ITarget.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.target;
2 |
3 | public interface ITarget {
4 |
5 | void request();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/AdapterPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/brand/AbstractCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.brand;
2 |
3 | import com.jasongj.transmission.Transmission;
4 |
5 | public abstract class AbstractCar {
6 |
7 | protected Transmission gear;
8 |
9 | public abstract void run();
10 |
11 | public void setTransmission(Transmission gear) {
12 | this.gear = gear;
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/brand/BMWCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.brand;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class BMWCar extends AbstractCar{
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(BMWCar.class);
9 |
10 | @Override
11 | public void run() {
12 | gear.gear();
13 | LOG.info("BMW is running");
14 | };
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/brand/BenZCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.brand;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class BenZCar extends AbstractCar{
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(BenZCar.class);
9 |
10 | @Override
11 | public void run() {
12 | gear.gear();
13 | LOG.info("BenZCar is running");
14 | };
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/brand/LandRoverCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.brand;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class LandRoverCar extends AbstractCar{
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(LandRoverCar.class);
9 |
10 | @Override
11 | public void run() {
12 | gear.gear();
13 | LOG.info("LandRoverCar is running");
14 | };
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/client/BridgeClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.brand.AbstractCar;
4 | import com.jasongj.brand.BMWCar;
5 | import com.jasongj.brand.BenZCar;
6 | import com.jasongj.transmission.Auto;
7 | import com.jasongj.transmission.Manual;
8 | import com.jasongj.transmission.Transmission;
9 |
10 | public class BridgeClient {
11 |
12 | public static void main(String[] args) {
13 | Transmission auto = new Auto();
14 | AbstractCar bmw = new BMWCar();
15 | bmw.setTransmission(auto);
16 | bmw.run();
17 |
18 |
19 | Transmission manual = new Manual();
20 | AbstractCar benz = new BenZCar();
21 | benz.setTransmission(manual);
22 | benz.run();
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/transmission/Auto.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.transmission;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class Auto extends Transmission {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(Auto.class);
9 |
10 | @Override
11 | public void gear() {
12 | LOG.info("Auto transmission");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/transmission/Manual.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.transmission;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class Manual extends Transmission {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(Manual.class);
9 |
10 | @Override
11 | public void gear() {
12 | LOG.info("Manual transmission");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/java/com/jasongj/transmission/Transmission.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.transmission;
2 |
3 | public abstract class Transmission{
4 |
5 | public abstract void gear();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/BridgePattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/CompositePattern/src/main/java/com/jasongj/client/Client1.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.organization.Company;
4 | import com.jasongj.organization.Department;
5 | import com.jasongj.organization.Organization;
6 |
7 | public class Client1 {
8 |
9 | public static void main(String[] args) {
10 | Organization shCompany = new Company("shCompany");
11 |
12 | Organization shHR = new Department("shHR");
13 | shCompany.addOrg(shHR);
14 |
15 | Organization shAdmin = new Department("shAdmin");
16 | shCompany.addOrg(shAdmin);
17 |
18 | Organization shFinance = new Department("shFinance");
19 | shCompany.addOrg(shFinance);
20 |
21 |
22 | Organization bjCompany = new Company("bjCompany");
23 |
24 | Organization bjHR = new Department("bjHR");
25 | bjCompany.addOrg(bjHR);
26 |
27 | Organization bjAdmin = new Department("bjAdmin");
28 | bjCompany.addOrg(bjAdmin);
29 |
30 | Organization bjFinance = new Department("bjFinance");
31 | bjCompany.addOrg(bjFinance);
32 |
33 | Organization company = new Company("company");
34 | Organization hr = new Department("hr");
35 | company.addOrg(hr);
36 | Organization admin = new Department("admin");
37 | company.addOrg(admin);
38 | Organization finance = new Department("finance");
39 | company.addOrg(finance);
40 |
41 | company.addOrg(shCompany);
42 | company.addOrg(bjCompany);
43 |
44 | company.inform("Cheers");
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/CompositePattern/src/main/java/com/jasongj/organization/Company.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.organization;
2 |
3 | import java.util.List;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | public class Company extends Organization{
9 |
10 | private static Logger LOGGER = LoggerFactory.getLogger(Company.class);
11 |
12 | public Company(String name) {
13 | super(name);
14 | }
15 |
16 | public void inform(String info){
17 | LOGGER.info("{}-{}", info, getName());
18 | List allOrgs = getAllOrgs();
19 | allOrgs.forEach(org -> org.inform(info+"-"));
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/CompositePattern/src/main/java/com/jasongj/organization/Department.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.organization;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class Department extends Organization{
7 |
8 | public Department(String name) {
9 | super(name);
10 | }
11 |
12 | private static Logger LOGGER = LoggerFactory.getLogger(Department.class);
13 |
14 | public void inform(String info){
15 | LOGGER.info("{}-{}", info, getName());
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/CompositePattern/src/main/java/com/jasongj/organization/Organization.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.organization;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public abstract class Organization {
7 |
8 | private List childOrgs = new ArrayList();
9 |
10 | private String name;
11 |
12 | public Organization(String name) {
13 | this.name = name;
14 | }
15 |
16 | public String getName() {
17 | return name;
18 | }
19 |
20 | public void addOrg(Organization org) {
21 | childOrgs.add(org);
22 | }
23 |
24 | public void removeOrg(Organization org) {
25 | childOrgs.remove(org);
26 | }
27 |
28 | public List getAllOrgs() {
29 | return childOrgs;
30 | }
31 |
32 | public abstract void inform(String info);
33 |
34 | @Override
35 | public int hashCode(){
36 | return this.name.hashCode();
37 | }
38 |
39 | @Override
40 | public boolean equals(Object org){
41 | if(!(org instanceof Organization)) {
42 | return false;
43 | }
44 | return this.name.equals(((Organization) org).name);
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/CompositePattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/DynamicProxy/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | apply plugin: 'java'
3 |
4 |
5 | // In this section you declare the dependencies for your production and test code
6 | dependencies {
7 | compile 'cglib:cglib:3.2.2'
8 | compile 'commons-io:commons-io:2.5'
9 | }
10 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/client/CgLibProxyClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.proxy.cglibproxy.SubjectInterceptor;
4 | import com.jasongj.subject.ConcreteSubject;
5 | import com.jasongj.subject.ISubject;
6 |
7 | import net.sf.cglib.proxy.Enhancer;
8 | import net.sf.cglib.proxy.MethodInterceptor;
9 |
10 | public class CgLibProxyClient {
11 |
12 | public static void main(String[] args) {
13 | MethodInterceptor methodInterceptor = new SubjectInterceptor();
14 | Enhancer enhancer = new Enhancer();
15 | enhancer.setSuperclass(ConcreteSubject.class);
16 | enhancer.setCallback(methodInterceptor);
17 | ISubject subject = (ISubject)enhancer.create();
18 | subject.action();
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/client/DynamicProxyPerfClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import java.io.IOException;
4 | import java.lang.reflect.InvocationHandler;
5 | import java.lang.reflect.Proxy;
6 |
7 | import org.slf4j.Logger;
8 | import org.slf4j.LoggerFactory;
9 |
10 | import com.jasongj.proxy.cglibproxy.SubjectInterceptor;
11 | import com.jasongj.proxy.jdkproxy.SubjectProxyHandler;
12 | import com.jasongj.subject.ConcreteSubject;
13 | import com.jasongj.subject.ISubject;
14 |
15 | import net.sf.cglib.proxy.Enhancer;
16 | import net.sf.cglib.proxy.MethodInterceptor;
17 |
18 | public class DynamicProxyPerfClient {
19 |
20 | private static final Logger LOG = LoggerFactory.getLogger(DynamicProxyPerfClient.class);
21 | private static int creation = 100000000;
22 | private static int execution = 1000000000;
23 |
24 | public static void main(String[] args) throws IOException {
25 | testJDKDynamicCreation();
26 | testJDKDynamicExecution();
27 | testCglibCreation();
28 | testCglibExecution();
29 | }
30 |
31 | private static void testJDKDynamicCreation() {
32 | long start = System.currentTimeMillis();
33 | for (int i = 0; i < creation; i++) {
34 | InvocationHandler handler = new SubjectProxyHandler(ConcreteSubject.class);
35 | Proxy.newProxyInstance(DynamicProxyPerfClient.class.getClassLoader(),
36 | new Class[] {ISubject.class}, handler);
37 | }
38 | long stop = System.currentTimeMillis();
39 | LOG.info("JDK creation time : {} ms", stop - start);
40 | }
41 |
42 | private static void testJDKDynamicExecution() {
43 | long start = System.currentTimeMillis();
44 | InvocationHandler handler = new SubjectProxyHandler(ConcreteSubject.class);
45 | ISubject subject =
46 | (ISubject) Proxy.newProxyInstance(DynamicProxyPerfClient.class.getClassLoader(),
47 | new Class[] {ISubject.class}, handler);
48 | for (int i = 0; i < execution; i++) {
49 | subject.action();
50 | }
51 | long stop = System.currentTimeMillis();
52 | LOG.info("JDK execution time : {} ms", stop - start);
53 | }
54 |
55 | private static void testCglibCreation() {
56 | long start = System.currentTimeMillis();
57 | for (int i = 0; i < creation; i++) {
58 | MethodInterceptor methodInterceptor = new SubjectInterceptor();
59 | Enhancer enhancer = new Enhancer();
60 | enhancer.setSuperclass(ConcreteSubject.class);
61 | enhancer.setCallback(methodInterceptor);
62 | enhancer.create();
63 | }
64 | long stop = System.currentTimeMillis();
65 | LOG.info("cglib creation time : {} ms", stop - start);
66 | }
67 |
68 | private static void testCglibExecution() {
69 | MethodInterceptor methodInterceptor = new SubjectInterceptor();
70 | Enhancer enhancer = new Enhancer();
71 | enhancer.setSuperclass(ConcreteSubject.class);
72 | enhancer.setCallback(methodInterceptor);
73 | ISubject subject = (ISubject) enhancer.create();
74 | long start = System.currentTimeMillis();
75 | for (int i = 0; i < execution; i++) {
76 | subject.action();
77 | }
78 | long stop = System.currentTimeMillis();
79 | LOG.info("cglib execution time : {} ms", stop - start);
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/client/JDKDynamicProxyClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import java.io.FileOutputStream;
4 | import java.io.IOException;
5 | import java.io.OutputStream;
6 | import java.lang.reflect.InvocationHandler;
7 | import java.lang.reflect.Proxy;
8 |
9 | import org.apache.commons.io.IOUtils;
10 | import org.slf4j.Logger;
11 | import org.slf4j.LoggerFactory;
12 |
13 | import com.jasongj.proxy.jdkproxy.SubjectProxyHandler;
14 | import com.jasongj.subject.ConcreteSubject;
15 | import com.jasongj.subject.ISubject;
16 |
17 | import sun.misc.ProxyGenerator;
18 |
19 | public class JDKDynamicProxyClient {
20 |
21 | private static final Logger LOG = LoggerFactory.getLogger(JDKDynamicProxyClient.class);
22 |
23 | public static void main(String[] args) throws IOException {
24 | InvocationHandler handler = new SubjectProxyHandler(ConcreteSubject.class);
25 | ISubject subject =
26 | (ISubject) Proxy.newProxyInstance(JDKDynamicProxyClient.class.getClassLoader(),
27 | new Class[] {ISubject.class}, handler);
28 | subject.action();
29 | LOG.info("Proxy class name {}", subject.getClass().getName());
30 | byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy18", ConcreteSubject.class.getInterfaces());
31 | try(OutputStream outputStream = new FileOutputStream("$Proxy18.class")){
32 | IOUtils.write(classFile, outputStream);
33 | }
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/client/StaticProxyClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.proxy.staticproxy.ProxySubject;
4 | import com.jasongj.subject.ISubject;
5 |
6 | public class StaticProxyClient {
7 |
8 | public static void main(String[] args) {
9 | ISubject subject = new ProxySubject();
10 | subject.action();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/proxy/cglibproxy/SubjectInterceptor.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy.cglibproxy;
2 |
3 | import java.lang.reflect.Method;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import net.sf.cglib.proxy.MethodInterceptor;
9 | import net.sf.cglib.proxy.MethodProxy;
10 |
11 | public class SubjectInterceptor implements MethodInterceptor {
12 |
13 | private static final Logger LOG = LoggerFactory.getLogger(SubjectInterceptor.class);
14 |
15 | @Override
16 | public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
17 | throws Throwable {
18 | preAction();
19 | Object result = proxy.invokeSuper(obj, args);
20 | postAction();
21 | return result;
22 | }
23 |
24 | private void preAction() {
25 | // LOG.info("SubjectProxyHandler.preAction()");
26 | }
27 |
28 | private void postAction() {
29 | // LOG.info("SubjectProxyHandler.postAction()");
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/proxy/jdkproxy/MultiProxyHandler.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy.jdkproxy;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 |
9 | public class MultiProxyHandler implements InvocationHandler {
10 |
11 | private static final Logger LOG = LoggerFactory.getLogger(SubjectProxyHandler.class);
12 |
13 | private Object target;
14 |
15 | @SuppressWarnings("rawtypes")
16 | public MultiProxyHandler(Class clazz) {
17 | try {
18 | this.target = clazz.newInstance();
19 | } catch (InstantiationException | IllegalAccessException ex) {
20 | LOG.error("Create proxy for {} failed", clazz.getName());
21 | }
22 | }
23 |
24 | @Override
25 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
26 | switch(method.getName()) {
27 | case "hashCode":
28 | return ((int)method.invoke(target, args)) + 1;
29 | case "equals":
30 | return (boolean)method.invoke(target, args);
31 | case "toString":
32 | return method.invoke(target, args) + "";
33 | case "action":
34 | preAction();
35 | Object result = method.invoke(target, args);
36 | postAction();
37 | return result;
38 | default:
39 | return method.invoke(target, args);
40 | }
41 | }
42 |
43 | private void preAction() {
44 | LOG.info("SubjectProxyHandler.preAction()");
45 | }
46 |
47 | private void postAction() {
48 | LOG.info("SubjectProxyHandler.postAction()");
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/proxy/jdkproxy/SubjectProxyHandler.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy.jdkproxy;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 |
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 |
9 | public class SubjectProxyHandler implements InvocationHandler {
10 |
11 | private static final Logger LOG = LoggerFactory.getLogger(SubjectProxyHandler.class);
12 |
13 | private Object target;
14 |
15 | @SuppressWarnings("rawtypes")
16 | public SubjectProxyHandler(Class clazz) {
17 | try {
18 | this.target = clazz.newInstance();
19 | } catch (InstantiationException | IllegalAccessException ex) {
20 | LOG.error("Create proxy for {} failed", clazz.getName());
21 | }
22 | }
23 |
24 | @Override
25 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
26 | preAction();
27 | Object result = method.invoke(target, args);
28 | postAction();
29 | return result;
30 | }
31 |
32 | private void preAction() {
33 | // LOG.info("SubjectProxyHandler.preAction()");
34 | }
35 |
36 | private void postAction() {
37 | // LOG.info("SubjectProxyHandler.postAction()");
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/proxy/staticproxy/$Proxy17.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy.staticproxy;
2 |
3 | import com.jasongj.subject.ISubject;
4 | import java.lang.reflect.InvocationHandler;
5 | import java.lang.reflect.Method;
6 | import java.lang.reflect.Proxy;
7 | import java.lang.reflect.UndeclaredThrowableException;
8 |
9 | public final class $Proxy17 extends Proxy implements ISubject {
10 | private static Method m1;
11 | private static Method m2;
12 | private static Method m0;
13 | private static Method m3;
14 |
15 | public $Proxy17(InvocationHandler paramInvocationHandler) {
16 | super(paramInvocationHandler);
17 | }
18 |
19 | public final boolean equals(Object paramObject) {
20 | try {
21 | return ((Boolean) this.h.invoke(this, m1, new Object[] {paramObject})).booleanValue();
22 | } catch (Error | RuntimeException localError) {
23 | throw localError;
24 | } catch (Throwable localThrowable) {
25 | throw new UndeclaredThrowableException(localThrowable);
26 | }
27 | }
28 |
29 | public final String toString() {
30 | try {
31 | return (String) this.h.invoke(this, m2, null);
32 | } catch (Error | RuntimeException localError) {
33 | throw localError;
34 | } catch (Throwable localThrowable) {
35 | throw new UndeclaredThrowableException(localThrowable);
36 | }
37 | }
38 |
39 | public final int hashCode() {
40 | try {
41 | return ((Integer) this.h.invoke(this, m0, null)).intValue();
42 | } catch (Error | RuntimeException localError) {
43 | throw localError;
44 | } catch (Throwable localThrowable) {
45 | throw new UndeclaredThrowableException(localThrowable);
46 | }
47 | }
48 |
49 | public final void action() {
50 | try {
51 | this.h.invoke(this, m3, null);
52 | return;
53 | } catch (Error | RuntimeException localError) {
54 | throw localError;
55 | } catch (Throwable localThrowable) {
56 | throw new UndeclaredThrowableException(localThrowable);
57 | }
58 | }
59 |
60 | static {
61 | try {
62 | m1 = Class.forName("java.lang.Object").getMethod("equals",
63 | new Class[] {Class.forName("java.lang.Object")});
64 | m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
65 | m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
66 | m3 = Class.forName("com.jasongj.subject.ISubject").getMethod("action", new Class[0]);
67 | } catch (NoSuchMethodException localNoSuchMethodException) {
68 | throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
69 | } catch (ClassNotFoundException localClassNotFoundException) {
70 | throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/proxy/staticproxy/ProxySubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy.staticproxy;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.subject.ConcreteSubject;
7 | import com.jasongj.subject.ISubject;
8 |
9 | public class ProxySubject implements ISubject {
10 |
11 | private static final Logger LOG = LoggerFactory.getLogger(ProxySubject.class);
12 |
13 | private ISubject subject;
14 |
15 | public ProxySubject() {
16 | subject = new ConcreteSubject();
17 | }
18 |
19 | @Override
20 | public void action() {
21 | preAction();
22 | subject.action();
23 | postAction();
24 | }
25 |
26 | public void preAction() {
27 | LOG.info("ProxySubject.preAction()");
28 | }
29 |
30 | public void postAction() {
31 | LOG.info("ProxySubject.postAction()");
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/subject/ConcreteSubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 |
7 | public class ConcreteSubject implements ISubject {
8 |
9 | private static final Logger LOG = LoggerFactory.getLogger(ConcreteSubject.class);
10 |
11 | @Override
12 | public void action() {
13 | // LOG.info("ConcreteSubject action()");
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/java/com/jasongj/subject/ISubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | public interface ISubject {
4 |
5 | void action();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/DynamicProxy/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/bean/User.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.bean;
2 |
3 | public class User {
4 |
5 | private String username;
6 | private char[] password;
7 |
8 | public String getUsername() {
9 | return username;
10 | }
11 |
12 | public void setUsername(String username) {
13 | this.username = username;
14 | }
15 |
16 | public char[] getPassword() {
17 | return password;
18 | }
19 |
20 | public void setPassword(char[] password) {
21 | this.password = password;
22 | }
23 |
24 | @Override
25 | public String toString() {
26 | return "{username=" + username + ", password=" + new String(password) + "}";
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/client/Client.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.dao.IUserDao;
4 | import com.jasongj.factory.IDaoFactory;
5 | import com.jasongj.factory.MySQLDaoFactory;
6 |
7 | public class Client {
8 |
9 | public static void main(String[] args) {
10 | IDaoFactory factory = new MySQLDaoFactory();
11 | IUserDao userDao = factory.createUserDao();
12 | userDao.getUser("admin");
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/dao/IUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao;
2 |
3 | import com.jasongj.bean.User;
4 |
5 | public interface IUserDao {
6 |
7 | void addUser(User user);
8 | void removeUser(User user);
9 | User getUser(String username);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/dao/MySQLUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class MySQLUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(MySQLUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("MySQL added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("MySQL removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/dao/OracleUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class OracleUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(OracleUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("Oracle added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("Oracle removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/dao/PostgreSQLUserDao.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.dao;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.bean.User;
7 |
8 | public class PostgreSQLUserDao implements IUserDao {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(PostgreSQLUserDao.class);
11 |
12 | @Override
13 | public void addUser(User user) {
14 | LOG.info("PostgreSQL added User {}", user);
15 | }
16 |
17 | @Override
18 | public void removeUser(User user) {
19 | LOG.info("PostgreSQL removed User {}", user);
20 | }
21 |
22 | @Override
23 | public User getUser(String username) {
24 | User user = new User();
25 | user.setUsername(username);
26 | return user;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/factory/IDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.IUserDao;
4 |
5 | public interface IDaoFactory {
6 |
7 | IUserDao createUserDao();
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/factory/MySQLDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.IUserDao;
4 | import com.jasongj.dao.MySQLUserDao;
5 |
6 | public class MySQLDaoFactory implements IDaoFactory {
7 |
8 | @Override
9 | public IUserDao createUserDao() {
10 | return new MySQLUserDao();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/factory/OracleDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.IUserDao;
4 | import com.jasongj.dao.MySQLUserDao;
5 |
6 | public class OracleDaoFactory implements IDaoFactory {
7 |
8 | @Override
9 | public IUserDao createUserDao() {
10 | return new MySQLUserDao();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/java/com/jasongj/factory/PostgreSQLDaoFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import com.jasongj.dao.IUserDao;
4 | import com.jasongj.dao.MySQLUserDao;
5 |
6 | public class PostgreSQLDaoFactory implements IDaoFactory {
7 |
8 | @Override
9 | public IUserDao createUserDao() {
10 | return new MySQLUserDao();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/java/com/jasongj/client/FlyWeightClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.factory.FlyWeightFactory;
4 | import com.jasongj.flyweight.FlyWeight;
5 |
6 | public class FlyWeightClient {
7 |
8 | public static void main(String[] args) {
9 | FlyWeight bmw1 = FlyWeightFactory.getFlyWeight("bmw");
10 | FlyWeight bmw2 = FlyWeightFactory.getFlyWeight("bmw");
11 | FlyWeight bmw3 = FlyWeightFactory.getFlyWeight("bmw");
12 |
13 | bmw1.action("start");
14 | bmw2.action("stop");
15 | bmw3.action("boost");
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/java/com/jasongj/factory/FlyWeightFactory.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import java.util.concurrent.ConcurrentHashMap;
4 |
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import com.jasongj.flyweight.ConcreteFlyWeight;
9 | import com.jasongj.flyweight.FlyWeight;
10 |
11 | public class FlyWeightFactory {
12 |
13 | private static final Logger LOG = LoggerFactory.getLogger(FlyWeightFactory.class);
14 |
15 | private static ConcurrentHashMap allFlyWeight = new ConcurrentHashMap();
16 |
17 | public static FlyWeight getFlyWeight(String name) {
18 | if (allFlyWeight.get(name) == null) {
19 | synchronized (allFlyWeight) {
20 | if (allFlyWeight.get(name) == null) {
21 | LOG.info("Instance of name = {} does not exist, creating it");
22 | FlyWeight flyWeight = new ConcreteFlyWeight(name);
23 | LOG.info("Instance of name = {} created");
24 | allFlyWeight.put(name, flyWeight);
25 | }
26 | }
27 | }
28 | return allFlyWeight.get(name);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/java/com/jasongj/flyweight/ConcreteFlyWeight.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.flyweight;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class ConcreteFlyWeight implements FlyWeight {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(ConcreteFlyWeight.class);
9 |
10 | private String name;
11 |
12 | public ConcreteFlyWeight(String name) {
13 | this.name = name;
14 | }
15 |
16 | @Override
17 | public void action(String externalState) {
18 | LOG.info("name = {}, outerState = {}", this.name, externalState);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/java/com/jasongj/flyweight/FlyWeight.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.flyweight;
2 |
3 | public interface FlyWeight {
4 |
5 | void action(String externalState);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/java/com/jasongj/flyweight/UnSharedFlyWeight.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.flyweight;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class UnSharedFlyWeight implements FlyWeight {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(UnSharedFlyWeight.class);
9 |
10 | private String name;
11 |
12 | public UnSharedFlyWeight(String name) {
13 | this.name = name;
14 | }
15 |
16 | @Override
17 | public void action(String externalState) {
18 | LOG.info("name = {}, outerState = {}", this.name, externalState);
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/FlyweightPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/client/Client1.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.observer.Architect;
4 | import com.jasongj.observer.ITalent;
5 | import com.jasongj.observer.JuniorEngineer;
6 | import com.jasongj.observer.SeniorEngineer;
7 | import com.jasongj.subject.HeadHunter;
8 | import com.jasongj.subject.AbstractHR;
9 |
10 | public class Client1 {
11 |
12 | public static void main(String[] args) {
13 | ITalent juniorEngineer = new JuniorEngineer();
14 | ITalent seniorEngineer = new SeniorEngineer();
15 | ITalent architect = new Architect();
16 |
17 | AbstractHR subject = new HeadHunter();
18 | subject.addTalent(juniorEngineer);
19 | subject.addTalent(seniorEngineer);
20 | subject.addTalent(architect);
21 |
22 | subject.publishJob("Top 500 big data position");
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/observer/Architect.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.observer;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class Architect implements ITalent {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(Architect.class);
9 |
10 | @Override
11 | public void newJob(String job) {
12 | LOG.info("Architect get new position {}", job);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/observer/ITalent.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.observer;
2 |
3 | public interface ITalent {
4 |
5 | void newJob(String job);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/observer/JuniorEngineer.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.observer;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class JuniorEngineer implements ITalent {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(JuniorEngineer.class);
9 |
10 | @Override
11 | public void newJob(String job) {
12 | LOG.info("Junior engineer get new position {}", job);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/observer/SeniorEngineer.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.observer;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | public class SeniorEngineer implements ITalent {
7 |
8 | private static final Logger LOG = LoggerFactory.getLogger(SeniorEngineer.class);
9 |
10 | @Override
11 | public void newJob(String job) {
12 | LOG.info("Senior engineer get new position {}", job);
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/subject/AbstractHR.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collection;
5 |
6 | import com.jasongj.observer.ITalent;
7 |
8 | public abstract class AbstractHR {
9 |
10 | protected Collection allTalents = new ArrayList();
11 |
12 | public abstract void publishJob(String job);
13 |
14 | public void addTalent(ITalent talent) {
15 | allTalents.add(talent);
16 | }
17 |
18 | public void removeTalent(ITalent talent) {
19 | allTalents.remove(talent);
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/java/com/jasongj/subject/HeadHunter.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | public class HeadHunter extends AbstractHR {
4 |
5 | @Override
6 | public void publishJob(String job) {
7 | allTalents.forEach(talent -> talent.newJob(job));
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/ObserverPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/client/DecoratorClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.decorator.SubjectPostDecorator;
4 | import com.jasongj.decorator.SubjectPreDecorator;
5 | import com.jasongj.subject.ConcreteSubject;
6 | import com.jasongj.subject.ISubject;
7 |
8 | public class DecoratorClient {
9 |
10 | public static void main(String[] args) {
11 | ISubject subject = new ConcreteSubject();
12 | ISubject preDecorator = new SubjectPreDecorator(subject);
13 | ISubject postDecorator = new SubjectPostDecorator(preDecorator);
14 | postDecorator.action();
15 |
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/client/StaticProxyClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.proxy.ProxySubject;
4 | import com.jasongj.subject.ISubject;
5 |
6 | public class StaticProxyClient {
7 |
8 | public static void main(String[] args) {
9 | ISubject subject = new ProxySubject();
10 | subject.action();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/decorator/SubjectPostDecorator.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.decorator;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.subject.ISubject;
7 |
8 | public class SubjectPostDecorator implements ISubject {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(SubjectPostDecorator.class);
11 |
12 | private ISubject subject;
13 |
14 | public SubjectPostDecorator(ISubject subject) {
15 | this.subject = subject;
16 | }
17 |
18 | @Override
19 | public void action() {
20 | subject.action();
21 | postAction();
22 | }
23 |
24 | private void postAction() {
25 | LOG.info("SubjectPostDecorator.preAction()");
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/decorator/SubjectPreDecorator.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.decorator;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.subject.ISubject;
7 |
8 | public class SubjectPreDecorator implements ISubject {
9 |
10 | private static final Logger LOG = LoggerFactory.getLogger(SubjectPreDecorator.class);
11 |
12 | private ISubject subject;
13 |
14 | public SubjectPreDecorator(ISubject subject) {
15 | this.subject = subject;
16 | }
17 |
18 | @Override
19 | public void action() {
20 | preAction();
21 | subject.action();
22 | }
23 |
24 | private void preAction() {
25 | LOG.info("SubjectPreDecorator.preAction()");
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/proxy/ProxySubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.proxy;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import java.util.Random;
6 |
7 | import com.jasongj.subject.ConcreteSubject;
8 | import com.jasongj.subject.ISubject;
9 |
10 | public class ProxySubject implements ISubject {
11 |
12 | private static final Logger LOG = LoggerFactory.getLogger(ProxySubject.class);
13 |
14 | private ISubject subject;
15 |
16 | public ProxySubject() {
17 | subject = new ConcreteSubject();
18 | }
19 |
20 | @Override
21 | public void action() {
22 | preAction();
23 | if((new Random()).nextBoolean()){
24 | subject.action();
25 | } else {
26 | LOG.info("Permission denied");
27 | }
28 | postAction();
29 | }
30 |
31 | private void preAction() {
32 | LOG.info("ProxySubject.preAction()");
33 | }
34 |
35 | private void postAction() {
36 | LOG.info("ProxySubject.postAction()");
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/subject/ConcreteSubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 |
7 | public class ConcreteSubject implements ISubject {
8 |
9 | private static final Logger LOG = LoggerFactory.getLogger(ConcreteSubject.class);
10 |
11 | @Override
12 | public void action() {
13 | LOG.info("ConcreteSubject action()");
14 | }
15 |
16 | }
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/java/com/jasongj/subject/ISubject.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.subject;
2 |
3 | public interface ISubject {
4 |
5 | void action();
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/ProxyAndDecoratorPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Java设计模式
2 | ---
3 |
4 | 本项目主要记录《Java设计模式》系列文章对应的实例代码,具体的文章详见[作者个人博客](http://www.jasongj.com)[http://www.jasongj.com](http://www.jasongj.com)
5 |
6 | - [Java设计模式(一) 简单工厂模式不简单](http://www.jasongj.com/design_pattern/simple_factory/)
7 | - [Java设计模式(二) 工厂方法模式](http://www.jasongj.com/design_pattern/factory_method/)
8 | - [Java设计模式(三) 抽象工厂模式](http://www.jasongj.com/design_pattern/abstract_factory/)
9 | - [Java设计模式(四) 观察者模式 ](http://www.jasongj.com/design_pattern/observer/)
10 | - [Java设计模式(五) 组合模式](http://www.jasongj.com/design_pattern/composite/)
11 | - [Java设计模式(六) 代理模式 VS. 装饰模式](http://www.jasongj.com/design_pattern/proxy_decorator/)
12 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/annotation/Vehicle.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.annotation;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.RetentionPolicy;
5 | import java.lang.annotation.Target;
6 | import java.lang.annotation.ElementType;
7 | import java.lang.annotation.Inherited;
8 | import java.lang.annotation.Retention;
9 |
10 | @Target(ElementType.TYPE)
11 | @Retention(RetentionPolicy.RUNTIME)
12 | @Documented
13 | @Inherited
14 | public @interface Vehicle{
15 | String type () default "";
16 | }
17 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/Driver1.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.product.BenzCar;
4 |
5 | public class Driver1 {
6 |
7 | public static void main(String[] args) {
8 | BenzCar car = new BenzCar();
9 | car.drive();
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/Driver2.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import org.apache.commons.configuration.ConfigurationException;
4 | import org.apache.commons.configuration.XMLConfiguration;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import com.jasongj.product.BMWCar;
9 | import com.jasongj.product.BenzCar;
10 | import com.jasongj.product.Car;
11 | import com.jasongj.product.LandRoverCar;
12 |
13 | public class Driver2 {
14 |
15 | private static final Logger LOG = LoggerFactory.getLogger(Driver2.class);
16 |
17 | public static void main(String[] args) throws ConfigurationException {
18 | XMLConfiguration config = new XMLConfiguration("car.xml");
19 | String name = config.getString("driver2.name");
20 | Car car;
21 |
22 | switch (name) {
23 | case "Land Rover":
24 | car = new LandRoverCar();
25 | break;
26 | case "BMW":
27 | car = new BMWCar();
28 | break;
29 | case "Benz":
30 | car = new BenzCar();
31 | break;
32 | default:
33 | car = null;
34 | break;
35 | }
36 | LOG.info("Created car name is {}", name);
37 | car.drive();
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/Driver3.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.factory.CarFactory1;
4 | import com.jasongj.product.Car;
5 |
6 | public class Driver3 {
7 |
8 | public static void main(String[] args) {
9 | Car car = CarFactory1.newCar();
10 | car.drive();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/Driver4.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.factory.CarFactory2;
4 | import com.jasongj.product.Car;
5 |
6 | public class Driver4 {
7 |
8 | public static void main(String[] args) {
9 | Car car = CarFactory2.newCar();
10 | car.drive();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/Driver5.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.factory.CarFactory3;
4 | import com.jasongj.product.Car;
5 |
6 | public class Driver5 {
7 |
8 | public static void main(String[] args) {
9 | Car car = CarFactory3.newCar();
10 | car.drive();
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/client/JDBC.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.PreparedStatement;
6 | import java.sql.SQLException;
7 |
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 |
11 | public class JDBC {
12 |
13 | private static final Logger LOG = LoggerFactory.getLogger(JDBC.class);
14 |
15 | public static void main(String[] args) {
16 | Connection conn = null;
17 | try {
18 | Class.forName("org.apache.hive.jdbc.HiveDriver");
19 | conn = DriverManager.getConnection("jdbc:hive2://127.0.0.1:10000/default");
20 | PreparedStatement ps = conn.prepareStatement("select count(*) from test.test");
21 | ps.execute();
22 | } catch (SQLException ex) {
23 | LOG.warn("Execute query failed", ex);
24 | } catch(ClassNotFoundException e) {
25 | LOG.warn("Load Hive driver failed", e);
26 | } finally{
27 | if(conn != null ){
28 | try {
29 | conn.close();
30 | } catch (SQLException e) {
31 | // NO-OPT
32 | }
33 | }
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/factory/CarFactory1.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import org.apache.commons.configuration.ConfigurationException;
4 | import org.apache.commons.configuration.XMLConfiguration;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import com.jasongj.product.BMWCar;
9 | import com.jasongj.product.BenzCar;
10 | import com.jasongj.product.Car;
11 | import com.jasongj.product.LandRoverCar;
12 |
13 | public class CarFactory1 {
14 |
15 | private static final Logger LOG = LoggerFactory.getLogger(CarFactory1.class);
16 |
17 | public static Car newCar() {
18 | Car car = null;
19 | String name = null;
20 | try {
21 | XMLConfiguration config = new XMLConfiguration("car.xml");
22 | name = config.getString("factory1.name");
23 | } catch (ConfigurationException ex) {
24 | LOG.error("parse xml configuration file failed", ex);
25 | }
26 |
27 | switch (name) {
28 | case "Land Rover":
29 | car = new LandRoverCar();
30 | break;
31 | case "BMW":
32 | car = new BMWCar();
33 | break;
34 | case "Benz":
35 | car = new BenzCar();
36 | break;
37 | default:
38 | car = null;
39 | break;
40 | }
41 | LOG.info("Created car name is {}", name);
42 | return car;
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/factory/CarFactory2.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import org.apache.commons.configuration.ConfigurationException;
4 | import org.apache.commons.configuration.XMLConfiguration;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 |
8 | import com.jasongj.product.Car;
9 |
10 | public class CarFactory2 {
11 |
12 | private static final Logger LOG = LoggerFactory.getLogger(CarFactory2.class);
13 |
14 | public static Car newCar() {
15 | Car car = null;
16 | String name = null;
17 | try {
18 | XMLConfiguration config = new XMLConfiguration("car.xml");
19 | name = config.getString("factory2.class");
20 | } catch (ConfigurationException ex) {
21 | LOG.error("Parsing xml configuration file failed", ex);
22 | }
23 |
24 | try {
25 | car = (Car)Class.forName(name).newInstance();
26 | LOG.info("Created car class name is {}", name);
27 | } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
28 | LOG.error("Instantiate car {} failed", name);
29 | }
30 | return car;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/factory/CarFactory3.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.factory;
2 |
3 | import java.util.Collections;
4 | import java.util.Map;
5 | import java.util.Set;
6 | import java.util.concurrent.ConcurrentHashMap;
7 |
8 | import org.apache.commons.configuration.ConfigurationException;
9 | import org.apache.commons.configuration.XMLConfiguration;
10 | import org.reflections.Reflections;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.jasongj.annotation.Vehicle;
15 | import com.jasongj.product.Car;
16 |
17 | public class CarFactory3 {
18 |
19 | private static final Logger LOG = LoggerFactory.getLogger(CarFactory3.class);
20 |
21 | private static Map allCars;
22 |
23 | static {
24 | Reflections reflections = new Reflections("com.jasongj.product");
25 | Set> annotatedClasses = reflections.getTypesAnnotatedWith(Vehicle.class);
26 | allCars = new ConcurrentHashMap();
27 | for (Class> classObject : annotatedClasses) {
28 | Vehicle vehicle = (Vehicle) classObject.getAnnotation(Vehicle.class);
29 | allCars.put(vehicle.type(), classObject);
30 | }
31 | allCars = Collections.unmodifiableMap(allCars);
32 | }
33 |
34 | public static Car newCar() {
35 | Car car = null;
36 | String type = null;
37 | try {
38 | XMLConfiguration config = new XMLConfiguration("car.xml");
39 | type = config.getString("factory3.type");
40 | LOG.info("car type is {}", type);
41 | } catch (ConfigurationException ex) {
42 | LOG.error("Parsing xml configuration file failed", ex);
43 | }
44 |
45 | if (allCars.containsKey(type)) {
46 | LOG.info("Created car type is {}", type);
47 | try {
48 | car = (Car) allCars.get(type).newInstance();
49 | } catch (InstantiationException | IllegalAccessException ex) {
50 | LOG.error("Instantiate car failed", ex);
51 | }
52 | } else {
53 | LOG.error("specified car type {} does not exist", type);
54 | }
55 | return car;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/product/BMWCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.annotation.Vehicle;
7 |
8 | @Vehicle(type="BMW")
9 | public class BMWCar extends Car {
10 |
11 | private static Logger LOG = LoggerFactory.getLogger(BMWCar.class);
12 |
13 | public BMWCar() {
14 | this.name = "BMW";
15 | }
16 |
17 | @Override
18 | public void drive() {
19 | LOG.info("My name is {}. I'm on my way", name);
20 | };
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/product/BenzCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.annotation.Vehicle;
7 |
8 | @Vehicle(type="Benz")
9 | public class BenzCar extends Car {
10 |
11 | private static Logger LOG = LoggerFactory.getLogger(BenzCar.class);
12 |
13 | public BenzCar() {
14 | this.name = "Benz";
15 | }
16 |
17 | @Override
18 | public void drive() {
19 | LOG.info("My name is {}. I'm on my way", name);
20 | };
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/product/Car.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.product;
2 |
3 | public abstract class Car {
4 |
5 | protected String name;
6 |
7 | public abstract void drive();
8 |
9 | public String getName(){
10 | return this.name;
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/java/com/jasongj/product/LandRoverCar.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.product;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import com.jasongj.annotation.Vehicle;
7 |
8 | @Vehicle(type="Land Rover")
9 | public class LandRoverCar extends Car {
10 |
11 | private static Logger LOG = LoggerFactory.getLogger(LandRoverCar.class);
12 |
13 | public LandRoverCar() {
14 | this.name = "Land Rover";
15 | }
16 |
17 | @Override
18 | public void drive() {
19 | LOG.info("My name is {}. I'm on my way", name);
20 | };
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/resources/car.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Land Rover
4 |
5 |
6 | BMW
7 |
8 |
9 | com.jasongj.product.BenzCar
10 |
11 |
12 | BMW
13 |
14 |
--------------------------------------------------------------------------------
/SimpleFactoryPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton1/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton1;
2 |
3 | public class Singleton {
4 |
5 | private static Singleton INSTANCE;
6 |
7 | private Singleton() {};
8 |
9 | public static Singleton getInstance() {
10 | if (INSTANCE == null) {
11 | INSTANCE = new Singleton();
12 | }
13 | return INSTANCE;
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton2/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton2;
2 |
3 | public class Singleton {
4 |
5 | private static Singleton INSTANCE;
6 |
7 | private Singleton() {};
8 |
9 | public static synchronized Singleton getInstance() {
10 | if (INSTANCE == null) {
11 | INSTANCE = new Singleton();
12 | }
13 | return INSTANCE;
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton3/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton3;
2 |
3 | public class Singleton {
4 |
5 | private static Singleton INSTANCE;
6 |
7 | private Singleton() {};
8 |
9 | public static Singleton getInstance() {
10 | if (INSTANCE == null) {
11 | synchronized (Singleton.class) {
12 | INSTANCE = new Singleton();
13 | }
14 | }
15 | return INSTANCE;
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton4/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton4;
2 |
3 | public class Singleton {
4 |
5 | private static volatile Singleton INSTANCE;
6 |
7 | private Singleton() {};
8 |
9 | public static Singleton getInstance() {
10 | if (INSTANCE == null) {
11 | synchronized(Singleton.class){
12 | if(INSTANCE == null) {
13 | INSTANCE = new Singleton();
14 | }
15 | }
16 | }
17 | return INSTANCE;
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton5/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton5;
2 |
3 | public class Singleton {
4 |
5 | private static volatile Singleton INSTANCE;
6 |
7 | private Singleton() {};
8 |
9 | public static Singleton getInstance() {
10 | if (INSTANCE == null) {
11 | synchronized (Singleton.class) {
12 | if (INSTANCE == null) {
13 | INSTANCE = new Singleton();
14 | }
15 | }
16 | }
17 | return INSTANCE;
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton6/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton6;
2 |
3 | public class Singleton {
4 |
5 | private static final Singleton INSTANCE = new Singleton();
6 |
7 | private Singleton() {};
8 |
9 | public static Singleton getInstance() {
10 | return INSTANCE;
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton7/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton7;
2 |
3 | public class Singleton {
4 |
5 | private static Singleton INSTANCE;
6 |
7 | static{
8 | INSTANCE = new Singleton();
9 | }
10 |
11 | private Singleton() {};
12 |
13 | public static Singleton getInstance() {
14 | return INSTANCE;
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton8/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton8;
2 |
3 | public class Singleton {
4 |
5 | private Singleton() {};
6 |
7 | public static Singleton getInstance() {
8 | return InnerClass.INSTANCE;
9 | }
10 |
11 | private static class InnerClass {
12 | private static final Singleton INSTANCE = new Singleton();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/java/com/jasongj/singleton9/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.singleton9;
2 |
3 | public enum Singleton {
4 |
5 | INSTANCE;
6 |
7 | public void whatSoEverMethod() { }
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/SingletonPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/annotation/Strategy.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.annotation;
2 |
3 | import java.lang.annotation.Documented;
4 | import java.lang.annotation.ElementType;
5 | import java.lang.annotation.Inherited;
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 | import java.lang.annotation.Target;
9 |
10 | @Target(ElementType.TYPE)
11 | @Retention(RetentionPolicy.RUNTIME)
12 | @Documented
13 | @Inherited
14 | public @interface Strategy{
15 | String name () default "";
16 | }
17 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/client/SimpleClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.context.SimpleContext;
4 | import com.jasongj.strategy.ConcreteStrategyA;
5 | import com.jasongj.strategy.Strategy;
6 |
7 | public class SimpleClient {
8 |
9 | public static void main(String[] args) {
10 | Strategy strategy = new ConcreteStrategyA();
11 | SimpleContext context = new SimpleContext(strategy);
12 | context.action("Hellow, world");
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/client/SimpleFactoryClient.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.client;
2 |
3 | import com.jasongj.context.SimpleFactoryContext;
4 |
5 | public class SimpleFactoryClient {
6 |
7 | public static void main(String[] args) {
8 | SimpleFactoryContext context = new SimpleFactoryContext();
9 | context.action("Hellow, world");
10 | }
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/context/SimpleContext.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.context;
2 |
3 | import com.jasongj.strategy.Strategy;
4 |
5 | public class SimpleContext {
6 |
7 | private Strategy strategy;
8 |
9 | public SimpleContext(Strategy strategy) {
10 | this.strategy = strategy;
11 | }
12 |
13 | public void action(String input) {
14 | strategy.strategy(input);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/context/SimpleFactoryContext.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.context;
2 |
3 | import java.util.Collections;
4 | import java.util.Map;
5 | import java.util.Set;
6 | import java.util.concurrent.ConcurrentHashMap;
7 |
8 | import org.apache.commons.configuration.ConfigurationException;
9 | import org.apache.commons.configuration.XMLConfiguration;
10 | import org.reflections.Reflections;
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.jasongj.strategy.Strategy;
15 |
16 | public class SimpleFactoryContext {
17 |
18 | private static final Logger LOG = LoggerFactory.getLogger(SimpleFactoryContext.class);
19 | private static Map allStrategies;
20 |
21 | static {
22 | Reflections reflections = new Reflections("com.jasongj.strategy");
23 | Set> annotatedClasses =
24 | reflections.getTypesAnnotatedWith(com.jasongj.annotation.Strategy.class);
25 | allStrategies = new ConcurrentHashMap();
26 | for (Class> classObject : annotatedClasses) {
27 | com.jasongj.annotation.Strategy strategy = (com.jasongj.annotation.Strategy) classObject
28 | .getAnnotation(com.jasongj.annotation.Strategy.class);
29 | allStrategies.put(strategy.name(), classObject);
30 | }
31 | allStrategies = Collections.unmodifiableMap(allStrategies);
32 | }
33 |
34 | private Strategy strategy;
35 |
36 | public SimpleFactoryContext() {
37 | String name = null;
38 | try {
39 | XMLConfiguration config = new XMLConfiguration("strategy.xml");
40 | name = config.getString("strategy.name");
41 | LOG.info("strategy name is {}", name);
42 | } catch (ConfigurationException ex) {
43 | LOG.error("Parsing xml configuration file failed", ex);
44 | }
45 |
46 | if (allStrategies.containsKey(name)) {
47 | LOG.info("Created strategy name is {}", name);
48 | try {
49 | strategy = (Strategy) allStrategies.get(name).newInstance();
50 | } catch (InstantiationException | IllegalAccessException ex) {
51 | LOG.error("Instantiate Strategy failed", ex);
52 | }
53 | } else {
54 | LOG.error("Specified Strategy name {} does not exist", name);
55 | }
56 |
57 | }
58 |
59 | public void action(String input) {
60 | strategy.strategy(input);
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/strategy/ConcreteStrategyA.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.strategy;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | @com.jasongj.annotation.Strategy(name="StrategyA")
7 | public class ConcreteStrategyA implements Strategy {
8 |
9 | private static final Logger LOG = LoggerFactory.getLogger(ConcreteStrategyB.class);
10 |
11 | @Override
12 | public void strategy(String input) {
13 | LOG.info("Strategy A for input : {}", input);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/strategy/ConcreteStrategyB.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.strategy;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | @com.jasongj.annotation.Strategy(name="StrategyB")
7 | public class ConcreteStrategyB implements Strategy {
8 |
9 | private static final Logger LOG = LoggerFactory.getLogger(ConcreteStrategyB.class);
10 |
11 | @Override
12 | public void strategy(String input) {
13 | LOG.info("Strategy B for input : {}", input);
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/java/com/jasongj/strategy/Strategy.java:
--------------------------------------------------------------------------------
1 | package com.jasongj.strategy;
2 |
3 | public interface Strategy {
4 |
5 | void strategy(String input);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
9 |
10 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} [%L] %msg%n
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/StrategyPattern/src/main/resources/strategy.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | StrategyB
4 |
5 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | apply plugin: 'java'
3 |
4 | repositories {
5 | jcenter()
6 | mavenCentral()
7 | }
8 |
9 | // In this section you declare the dependencies for your production and test code
10 | dependencies {
11 | compile 'org.slf4j:slf4j-api:1.7.14'
12 | }
13 |
14 | subprojects {
15 | apply plugin: 'java'
16 | apply plugin: 'eclipse'
17 | apply plugin: 'maven'
18 | version = '1.0'
19 | sourceCompatibility = 1.8
20 | targetCompatibility = 1.8
21 | [compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'
22 | ext {
23 | springVersion = '3.2.11.RELEASE'
24 | hibernateVersion='4.3.1.Final'
25 | }
26 | repositories {
27 | mavenCentral()
28 | }
29 | jar {
30 | manifest {
31 | attributes("Implementation-Title": "Gradle")
32 | }
33 | }
34 | configurations {
35 | // Ignore dependencies
36 | all*.exclude group: 'commons-httpclient'
37 | all*.exclude group: 'commons-beanutils', module: 'commons-beanutils'
38 | }
39 | dependencies {
40 | compile(
41 | 'commons-configuration:commons-configuration:1.10',
42 | 'commons-collections:commons-collections:3.2.2',
43 | 'org.apache.logging.log4j:log4j-core:2.5',
44 | 'org.slf4j:slf4j-api:1.7.19',
45 | 'org.apache.logging.log4j:log4j-slf4j-impl:2.5',
46 | 'org.reflections:reflections:0.9.10',
47 | 'org.apache.commons:commons-lang3:3.4'
48 | )
49 | // Non-maven dependencies
50 | ext.jarTree = fileTree(dir: 'libs', include: '**/*.jar')
51 | ext.rootProjectLibs = new File(rootProject.rootDir, 'libs').getAbsolutePath()
52 | ext.jarTree += fileTree(dir: rootProjectLibs, include: '**/*.jar')
53 | compile jarTree
54 | testCompile(
55 | "org.springframework:spring-test:$springVersion",
56 | "junit:junit:4.11"
57 | )
58 | }
59 | // 显示当前项目下所有用于 compile 的 jar.
60 | task listJars(description: 'Display all compile jars.') << {
61 | configurations.compile.each { File file -> println file.name }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/habren/JavaDesignPattern/349a55aeca159fb775741ecdcae3876f23214356/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Mar 15 09:26:24 CST 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * This settings file was auto generated by the Gradle buildInit task
3 | * by 'jason' at '3/15/16 9:26 AM' with Gradle 2.11
4 | *
5 | * The settings file is used to specify which projects to include in your build.
6 | * In a single project build this file can be empty or even removed.
7 | *
8 | * Detailed information about configuring a multi-project build in Gradle can be found
9 | * in the user guide at https://docs.gradle.org/2.11/userguide/multi_project_builds.html
10 | */
11 |
12 | /*
13 | // To declare projects as part of a multi-project build use the 'include' method
14 | include 'shared'
15 | include 'api'
16 | include 'services:webservice'
17 | */
18 |
19 | include 'SimpleFactoryPattern'
20 | include 'FactoryMethodPattern'
21 | include 'AbstractFactoryPattern'
22 | include 'ObserverPattern'
23 | include 'CompositePattern'
24 | include 'ProxyAndDecoratorPattern'
25 | include 'DynamicProxy'
26 | include 'AdapterPattern'
27 | include 'BridgePattern'
28 | include 'SingletonPattern'
29 | include 'FlyweightPattern'
30 | include 'StrategyPattern'
31 |
32 | rootProject.name = 'JavaDesignPattern'
33 |
--------------------------------------------------------------------------------