Skip to content

Commit cb7bf77

Browse files
committedOct 10, 2019
no message
1 parent 6974056 commit cb7bf77

6 files changed

+313
-2
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ app/logs/
1313
nohup.out
1414
apidoc/
1515
node_modules/
16-
hotelApi/
16+
hotelApi/
17+
忘掉设计模式,使用最强最稳定最有套路的万能编程思维-python oop四步转化公式/*

‎J2eeInterceptingFilterDemo.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理。定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上。过滤器可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序。以下是这种设计模式的实体。
3+
4+
过滤器(Filter) - 过滤器在请求处理程序执行请求之前或之后,执行某些任务。
5+
过滤器链(Filter Chain) - 过滤器链带有多个过滤器,并在 Target 上按照定义的顺序执行这些过滤器。
6+
Target - Target 对象是请求处理程序。
7+
过滤管理器(Filter Manager) - 过滤管理器管理过滤器和过滤器链。
8+
客户端(Client) - Client 是向 Target 对象发送请求的对象。
9+
*/
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public interface Filter {
15+
public void execute(String request);
16+
}
17+
18+
class AuthenticationFilter implements Filter {
19+
public void execute(String request){
20+
System.out.println("Authenticating request: " + request);
21+
}
22+
}
23+
24+
class DebugFilter implements Filter {
25+
public void execute(String request){
26+
System.out.println("request log: " + request);
27+
}
28+
}
29+
30+
class Target {
31+
public void execute(String request){
32+
System.out.println("Executing request: " + request);
33+
}
34+
}
35+
36+
class FilterChain {
37+
private List<Filter> filters = new ArrayList<Filter>();
38+
private Target target;
39+
40+
public void addFilter(Filter filter){
41+
filters.add(filter);
42+
}
43+
44+
public void execute(String request){
45+
for (Filter filter : filters) {
46+
filter.execute(request);
47+
}
48+
target.execute(request);
49+
}
50+
51+
public void setTarget(Target target){
52+
this.target = target;
53+
}
54+
}
55+
class FilterManager {
56+
FilterChain filterChain;
57+
58+
public FilterManager(Target target){
59+
filterChain = new FilterChain();
60+
filterChain.setTarget(target);
61+
}
62+
public void setFilter(Filter filter){
63+
filterChain.addFilter(filter);
64+
}
65+
66+
public void filterRequest(String request){
67+
filterChain.execute(request);
68+
}
69+
}
70+
71+
class Client {
72+
FilterManager filterManager;
73+
74+
public void setFilterManager(FilterManager filterManager){
75+
this.filterManager = filterManager;
76+
}
77+
78+
public void sendRequest(String request){
79+
filterManager.filterRequest(request);
80+
}
81+
}
82+
83+
public class J2eeInterceptingFilterDemo {
84+
public static void main(String[] args) {
85+
FilterManager filterManager = new FilterManager(new Target());
86+
filterManager.setFilter(new AuthenticationFilter());
87+
filterManager.setFilter(new DebugFilter());
88+
89+
Client client = new Client();
90+
client.setFilterManager(filterManager);
91+
client.sendRequest("HOME");
92+
}
93+
}
94+
95+
//j2ee模式-拦截过滤器模式

‎J2eeServiceLocatorPatternDemo.java

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。
2+
3+
服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
4+
Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
5+
服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
6+
缓存(Cache) - 缓存存储服务的引用,以便复用它们。
7+
客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。*/
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public interface Service {
12+
public String getName();
13+
public void execute();
14+
}
15+
16+
class Service1 implements Service {
17+
public void execute(){
18+
System.out.println("Executing Service1");
19+
}
20+
21+
@Override
22+
public String getName() {
23+
return "Service1";
24+
}
25+
}
26+
class Service2 implements Service {
27+
public void execute(){
28+
System.out.println("Executing Service2");
29+
}
30+
31+
@Override
32+
public String getName() {
33+
return "Service2";
34+
}
35+
}
36+
37+
class InitialContext {
38+
public Object lookup(String jndiName){
39+
if(jndiName.equalsIgnoreCase("SERVICE1")){
40+
System.out.println("Looking up and creating a new Service1 object");
41+
return new Service1();
42+
}else if (jndiName.equalsIgnoreCase("SERVICE2")){
43+
System.out.println("Looking up and creating a new Service2 object");
44+
return new Service2();
45+
}
46+
return null;
47+
}
48+
}
49+
50+
51+
class Cache {
52+
53+
private List<Service> services;
54+
55+
public Cache(){
56+
services = new ArrayList<Service>();
57+
}
58+
59+
public Service getService(String serviceName){
60+
for (Service service : services) {
61+
if(service.getName().equalsIgnoreCase(serviceName)){
62+
System.out.println("Returning cached "+serviceName+" object");
63+
return service;
64+
}
65+
}
66+
return null;
67+
}
68+
69+
public void addService(Service newService){
70+
boolean exists = false;
71+
for (Service service : services) {
72+
if(service.getName().equalsIgnoreCase(newService.getName())){
73+
exists = true;
74+
}
75+
}
76+
if(!exists){
77+
services.add(newService);
78+
}
79+
}
80+
}
81+
82+
class ServiceLocator {
83+
private static Cache cache;
84+
85+
static {
86+
cache = new Cache();
87+
}
88+
89+
public static Service getService(String jndiName){
90+
91+
Service service = cache.getService(jndiName);
92+
93+
if(service != null){
94+
return service;
95+
}
96+
97+
InitialContext context = new InitialContext();
98+
Service service1 = (Service)context.lookup(jndiName);
99+
cache.addService(service1);
100+
return service1;
101+
}
102+
}
103+
104+
public class J2eeServiceLocatorPatternDemo {
105+
public static void main(String[] args) {
106+
Service service = ServiceLocator.getService("Service1");
107+
service.execute();
108+
service = ServiceLocator.getService("Service2");
109+
service.execute();
110+
service = ServiceLocator.getService("Service1");
111+
service.execute();
112+
service = ServiceLocator.getService("Service2");
113+
service.execute();
114+
}
115+
}
116+
117+
//j2ee 服务定位器模式

‎J2eeTransferObjectPatternDemo.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
传输对象模式(Transfer Object Pattern)用于从客户端向服务器一次性传递带有多个属性的数据。传输对象也被称为数值对象。传输对象是一个具有 getter/setter 方法的简单的 POJO 类,它是可序列化的,所以它可以通过网络传输。它没有任何的行为。服务器端的业务类通常从数据库读取数据,然后填充 POJO,并把它发送到客户端或按值传递它。对于客户端,传输对象是只读的。客户端可以创建自己的传输对象,并把它传递给服务器,以便一次性更新数据库中的数值。以下是这种设计模式的实体。
3+
4+
业务对象(Business Object) - 为传输对象填充数据的业务服务。
5+
传输对象(Transfer Object) - 简单的 POJO,只有设置/获取属性的方法。
6+
客户端(Client) - 客户端可以发送请求或者发送传输对象到业务对象。
7+
*/
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
class StudentVO {
13+
private String name;
14+
private int rollNo;
15+
16+
StudentVO(String name, int rollNo){
17+
this.name = name;
18+
this.rollNo = rollNo;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public int getRollNo() {
30+
return rollNo;
31+
}
32+
33+
public void setRollNo(int rollNo) {
34+
this.rollNo = rollNo;
35+
}
36+
}
37+
38+
39+
class StudentBO {
40+
41+
//列表是当作一个数据库
42+
List<StudentVO> students;
43+
44+
public StudentBO(){
45+
students = new ArrayList<StudentVO>();
46+
StudentVO student1 = new StudentVO("Robert",0);
47+
StudentVO student2 = new StudentVO("John",1);
48+
students.add(student1);
49+
students.add(student2);
50+
}
51+
public void deleteStudent(StudentVO student) {
52+
students.remove(student.getRollNo());
53+
System.out.println("Student: Roll No "
54+
+ student.getRollNo() +", deleted from database");
55+
}
56+
57+
//从数据库中检索学生名单
58+
public List<StudentVO> getAllStudents() {
59+
return students;
60+
}
61+
62+
public StudentVO getStudent(int rollNo) {
63+
return students.get(rollNo);
64+
}
65+
66+
public void updateStudent(StudentVO student) {
67+
students.get(student.getRollNo()).setName(student.getName());
68+
System.out.println("Student: Roll No "
69+
+ student.getRollNo() +", updated in the database");
70+
}
71+
}
72+
73+
public class J2eeTransferObjectPatternDemo {
74+
public static void main(String[] args) {
75+
StudentBO studentBusinessObject = new StudentBO();
76+
77+
//输出所有的学生
78+
for (StudentVO student : studentBusinessObject.getAllStudents()) {
79+
System.out.println("Student: [RollNo : "
80+
+ student.getRollNo()+", Name : "+student.getName()+" ]");
81+
}
82+
83+
//更新学生
84+
StudentVO student =studentBusinessObject.getAllStudents().get(0);
85+
student.setName("Michael");
86+
studentBusinessObject.updateStudent(student);
87+
88+
//获取学生
89+
studentBusinessObject.getStudent(0);
90+
System.out.println("Student: [RollNo : "
91+
+student.getRollNo()+", Name : "+student.getName()+" ]");
92+
}
93+
}
94+
95+
//j2ee 对象传输模式

‎monkey_patch_pattern/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 猴子补丁模式,要演示多个文件,所以建了个文件夹。演示猴子补丁,正确的使用猴子补丁模式。
22
```
3+
猴子补丁是python特有设计模式。是一种非常强悍,非常有趣的设计模式,能使用很少的修改达到全局运行 修改。
4+
35
80%的py人员把猴子补丁和gevent库居然划等号。这样想大错特错。
46
gevent库是使用猴子补丁设计模式之一的库,比较复杂。
57
任何人都有权利去使用甚至创造猴子补丁,这不是gevent库的专利,

‎行为型模式-观察者模式-重新实现日志系统.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。
77
有时,我们希望在一个对象的状态改变时更新另外一组对象。
88
9-
说这么多抽象的概念,说点具体的就是以日志为例,有人把print当做日志用,里面的区别相当大,日志不仅可以有streamhandler和filehandler,还有mailhandler httphandler 等几亿种自定义handler。logger debug时候自动触发各种handler的emit方法。
9+
说这么多抽象的概念,说点具体的就是以日志为例,有人把print当做日志用,里面的区别相当大,日志不仅可以有streamhandler和filehandler,还有mailhandler httphandler
10+
等几亿种自定义handler。logger debug时候自动触发各种handler的emit方法。
1011
用不会日志不理解日志,对logger addHandler各种handler懵逼不知道在干嘛,最主要是不懂观察者模式造成的。
1112
希望举得例子是接近真实有意义,下面简单使用观察者模式重新模拟实现一个伪日志包。
1213
"""

0 commit comments

Comments
 (0)
Please sign in to comment.