forked from ydf0509/python36patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
313 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ app/logs/ | |
nohup.out | ||
apidoc/ | ||
node_modules/ | ||
hotelApi/ | ||
hotelApi/ | ||
忘掉设计模式,使用最强最稳定最有套路的万能编程思维-python oop四步转化公式/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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模式-拦截过滤器模式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 服务定位器模式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 对象传输模式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters