Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
ydf0509 committed Oct 10, 2019
1 parent 6974056 commit cb7bf77
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ app/logs/
nohup.out
apidoc/
node_modules/
hotelApi/
hotelApi/
忘掉设计模式,使用最强最稳定最有套路的万能编程思维-python oop四步转化公式/*
95 changes: 95 additions & 0 deletions J2eeInterceptingFilterDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
拦截过滤器模式(Intercepting Filter Pattern)用于对应用程序的请求或响应做一些预处理/后处理。定义过滤器,并在把请求传给实际目标应用程序之前应用在请求上。过滤器可以做认证/授权/记录日志,或者跟踪请求,然后把请求传给相应的处理程序。以下是这种设计模式的实体。
过滤器(Filter) - 过滤器在请求处理程序执行请求之前或之后,执行某些任务。
过滤器链(Filter Chain) - 过滤器链带有多个过滤器,并在 Target 上按照定义的顺序执行这些过滤器。
Target - Target 对象是请求处理程序。
过滤管理器(Filter Manager) - 过滤管理器管理过滤器和过滤器链。
客户端(Client) - Client 是向 Target 对象发送请求的对象。
*/

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

public interface Filter {
public void execute(String request);
}

class AuthenticationFilter implements Filter {
public void execute(String request){
System.out.println("Authenticating request: " + request);
}
}

class DebugFilter implements Filter {
public void execute(String request){
System.out.println("request log: " + request);
}
}

class Target {
public void execute(String request){
System.out.println("Executing request: " + request);
}
}

class FilterChain {
private List<Filter> filters = new ArrayList<Filter>();
private Target target;

public void addFilter(Filter filter){
filters.add(filter);
}

public void execute(String request){
for (Filter filter : filters) {
filter.execute(request);
}
target.execute(request);
}

public void setTarget(Target target){
this.target = target;
}
}
class FilterManager {
FilterChain filterChain;

public FilterManager(Target target){
filterChain = new FilterChain();
filterChain.setTarget(target);
}
public void setFilter(Filter filter){
filterChain.addFilter(filter);
}

public void filterRequest(String request){
filterChain.execute(request);
}
}

class Client {
FilterManager filterManager;

public void setFilterManager(FilterManager filterManager){
this.filterManager = filterManager;
}

public void sendRequest(String request){
filterManager.filterRequest(request);
}
}

public class J2eeInterceptingFilterDemo {
public static void main(String[] args) {
FilterManager filterManager = new FilterManager(new Target());
filterManager.setFilter(new AuthenticationFilter());
filterManager.setFilter(new DebugFilter());

Client client = new Client();
client.setFilterManager(filterManager);
client.sendRequest("HOME");
}
}

//j2ee模式-拦截过滤器模式
117 changes: 117 additions & 0 deletions J2eeServiceLocatorPatternDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*服务定位器模式(Service Locator Pattern)用在我们想使用 JNDI 查询定位各种服务的时候。考虑到为某个服务查找 JNDI 的代价很高,服务定位器模式充分利用了缓存技术。在首次请求某个服务时,服务定位器在 JNDI 中查找服务,并缓存该服务对象。当再次请求相同的服务时,服务定位器会在它的缓存中查找,这样可以在很大程度上提高应用程序的性能。以下是这种设计模式的实体。
服务(Service) - 实际处理请求的服务。对这种服务的引用可以在 JNDI 服务器中查找到。
Context / 初始的 Context - JNDI Context 带有对要查找的服务的引用。
服务定位器(Service Locator) - 服务定位器是通过 JNDI 查找和缓存服务来获取服务的单点接触。
缓存(Cache) - 缓存存储服务的引用,以便复用它们。
客户端(Client) - Client 是通过 ServiceLocator 调用服务的对象。*/
import java.util.ArrayList;
import java.util.List;

public interface Service {
public String getName();
public void execute();
}

class Service1 implements Service {
public void execute(){
System.out.println("Executing Service1");
}

@Override
public String getName() {
return "Service1";
}
}
class Service2 implements Service {
public void execute(){
System.out.println("Executing Service2");
}

@Override
public String getName() {
return "Service2";
}
}

class InitialContext {
public Object lookup(String jndiName){
if(jndiName.equalsIgnoreCase("SERVICE1")){
System.out.println("Looking up and creating a new Service1 object");
return new Service1();
}else if (jndiName.equalsIgnoreCase("SERVICE2")){
System.out.println("Looking up and creating a new Service2 object");
return new Service2();
}
return null;
}
}


class Cache {

private List<Service> services;

public Cache(){
services = new ArrayList<Service>();
}

public Service getService(String serviceName){
for (Service service : services) {
if(service.getName().equalsIgnoreCase(serviceName)){
System.out.println("Returning cached "+serviceName+" object");
return service;
}
}
return null;
}

public void addService(Service newService){
boolean exists = false;
for (Service service : services) {
if(service.getName().equalsIgnoreCase(newService.getName())){
exists = true;
}
}
if(!exists){
services.add(newService);
}
}
}

class ServiceLocator {
private static Cache cache;

static {
cache = new Cache();
}

public static Service getService(String jndiName){

Service service = cache.getService(jndiName);

if(service != null){
return service;
}

InitialContext context = new InitialContext();
Service service1 = (Service)context.lookup(jndiName);
cache.addService(service1);
return service1;
}
}

public class J2eeServiceLocatorPatternDemo {
public static void main(String[] args) {
Service service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
service = ServiceLocator.getService("Service1");
service.execute();
service = ServiceLocator.getService("Service2");
service.execute();
}
}

//j2ee 服务定位器模式
95 changes: 95 additions & 0 deletions J2eeTransferObjectPatternDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
传输对象模式(Transfer Object Pattern)用于从客户端向服务器一次性传递带有多个属性的数据。传输对象也被称为数值对象。传输对象是一个具有 getter/setter 方法的简单的 POJO 类,它是可序列化的,所以它可以通过网络传输。它没有任何的行为。服务器端的业务类通常从数据库读取数据,然后填充 POJO,并把它发送到客户端或按值传递它。对于客户端,传输对象是只读的。客户端可以创建自己的传输对象,并把它传递给服务器,以便一次性更新数据库中的数值。以下是这种设计模式的实体。
业务对象(Business Object) - 为传输对象填充数据的业务服务。
传输对象(Transfer Object) - 简单的 POJO,只有设置/获取属性的方法。
客户端(Client) - 客户端可以发送请求或者发送传输对象到业务对象。
*/

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

class StudentVO {
private String name;
private int rollNo;

StudentVO(String name, int rollNo){
this.name = name;
this.rollNo = rollNo;
}

public String getName() {
return name;
}

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

public int getRollNo() {
return rollNo;
}

public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}


class StudentBO {

//列表是当作一个数据库
List<StudentVO> students;

public StudentBO(){
students = new ArrayList<StudentVO>();
StudentVO student1 = new StudentVO("Robert",0);
StudentVO student2 = new StudentVO("John",1);
students.add(student1);
students.add(student2);
}
public void deleteStudent(StudentVO student) {
students.remove(student.getRollNo());
System.out.println("Student: Roll No "
+ student.getRollNo() +", deleted from database");
}

//从数据库中检索学生名单
public List<StudentVO> getAllStudents() {
return students;
}

public StudentVO getStudent(int rollNo) {
return students.get(rollNo);
}

public void updateStudent(StudentVO student) {
students.get(student.getRollNo()).setName(student.getName());
System.out.println("Student: Roll No "
+ student.getRollNo() +", updated in the database");
}
}

public class J2eeTransferObjectPatternDemo {
public static void main(String[] args) {
StudentBO studentBusinessObject = new StudentBO();

//输出所有的学生
for (StudentVO student : studentBusinessObject.getAllStudents()) {
System.out.println("Student: [RollNo : "
+ student.getRollNo()+", Name : "+student.getName()+" ]");
}

//更新学生
StudentVO student =studentBusinessObject.getAllStudents().get(0);
student.setName("Michael");
studentBusinessObject.updateStudent(student);

//获取学生
studentBusinessObject.getStudent(0);
System.out.println("Student: [RollNo : "
+student.getRollNo()+", Name : "+student.getName()+" ]");
}
}

//j2ee 对象传输模式
2 changes: 2 additions & 0 deletions monkey_patch_pattern/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 猴子补丁模式,要演示多个文件,所以建了个文件夹。演示猴子补丁,正确的使用猴子补丁模式。
```
猴子补丁是python特有设计模式。是一种非常强悍,非常有趣的设计模式,能使用很少的修改达到全局运行 修改。
80%的py人员把猴子补丁和gevent库居然划等号。这样想大错特错。
gevent库是使用猴子补丁设计模式之一的库,比较复杂。
任何人都有权利去使用甚至创造猴子补丁,这不是gevent库的专利,
Expand Down
3 changes: 2 additions & 1 deletion 行为型模式-观察者模式-重新实现日志系统.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知它的依赖对象。观察者模式属于行为型模式。
有时,我们希望在一个对象的状态改变时更新另外一组对象。
说这么多抽象的概念,说点具体的就是以日志为例,有人把print当做日志用,里面的区别相当大,日志不仅可以有streamhandler和filehandler,还有mailhandler httphandler 等几亿种自定义handler。logger debug时候自动触发各种handler的emit方法。
说这么多抽象的概念,说点具体的就是以日志为例,有人把print当做日志用,里面的区别相当大,日志不仅可以有streamhandler和filehandler,还有mailhandler httphandler
等几亿种自定义handler。logger debug时候自动触发各种handler的emit方法。
用不会日志不理解日志,对logger addHandler各种handler懵逼不知道在干嘛,最主要是不懂观察者模式造成的。
希望举得例子是接近真实有意义,下面简单使用观察者模式重新模拟实现一个伪日志包。
"""
Expand Down

0 comments on commit cb7bf77

Please sign in to comment.