public interface Filter {
void doFilter(Request request, Response response, FilterChain chain);
}
public class FilterChain implements Filter {
List<Filter> filters = new ArrayList<Filter>();
int index = 0;
public FilterChain addFilter(Filter f) {
this.filters.add(f);
return this;
}
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
if(index == filters.size()) return ;
Filter f = filters.get(index);
index ++;
f.doFilter(request, response, chain);
}
}
public class HTMLFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
//process the html tag <>
request.requestStr = request.requestStr.replace('<', '[')
.replace('>', ']') + "---HTMLFilter()";
chain.doFilter(request, response, chain);
response.responseStr += "---HTMLFilter()";
}
}
public class SesitiveFilter implements Filter {
@Override
public void doFilter(Request request, Response response, FilterChain chain) {
request.requestStr = request.requestStr.replace("", "")
.replace("", "") + "---SesitiveFilter()";
chain.doFilter(request, response, chain);
response.responseStr += "---SesitiveFilter()";
}
}
public class Request {
String requestStr;
public String getRequestStr() {
return requestStr;
}
public void setRequestStr(String requestStr) {
this.requestStr = requestStr;
}
}
public class Response {
String responseStr;
public String getResponseStr() {
return responseStr;
}
public void setResponseStr(String responseStr) {
this.responseStr = responseStr;
}
}
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
String msg = "";
Request request = new Request();
request.setRequestStr(msg);
Response response = new Response();
response.setResponseStr("response");
FilterChain fc = new FilterChain();
fc.addFilter(new HTMLFilter())
.addFilter(new SesitiveFilter())
;
fc.doFilter(request, response, fc);
System.out.println(request.getRequestStr());
System.out.println(response.getResponseStr());
}
}
单例模式分为恶汉式,就是直接在类中new出,直接返回对象,懒汉式是在调用对象时判断对象是否是null,如果null,先new出,再返回,否则直接返回对象,但是这种方式会线程不安全,所以采用双重检查的设计思想,保证线程安全。
package singleton;
public class Teacher3 {
private Teacher3(){}
private static Teacher3 t=null;
public static Teacher3 getTeacher3(){
if(t==null){
synchronized (Teacher3.class) {
if(t==null){
t=new Teacher3();
}
}
}
return t;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二种方式,内部类方式
package test;
/**
* 在多线程中使用单例对象的设计模式,内部类
*
*/
public class InnerSingleton {
private static class Singleton{
private static Singleton s=new Singleton();
public void add(){
}
}
public static Singleton getSingleton(){
return Singleton.s;
}
public static void main(String[] args) {
Singleton singleton = InnerSingleton.getSingleton();
singleton.add();
}
}
具体的水果工厂实现类 | 具体的苹果实现类 | 具体的香蕉实现类 | 具体的枣实现类 |
中国水果工厂实现类 | 中国苹果 | 中国香蕉 | 中国枣 |
日本水果工厂实现类 | 日本苹果 | 日本香蕉 | 日本枣 |
以此类推,在工厂的接口中创建所有水果的方法声明。
public interface IFruit {
public void get();
}
public abstract class AbstractApple implements IFruit{
public abstract void get();
}
public abstract class AbstractBanana implements IFruit{
public abstract void get();
}
public interface IFruitFactory {
public IFruit getApple();
public IFruit getBanana();
}
public class NorthApple extends AbstractApple {
public void get() {
System.out.println("北方苹果");
}
}
public class NorthBanana extends AbstractBanana {
public void get() {
System.out.println("北方香蕉");
}
}
public class NorthFruitFactory implements IFruitFactory {
public IFruit getApple() {
return new NorthApple();
}
public IFruit getBanana() {
return new NorthBanana();
}
}
public class SouthApple extends AbstractApple {
public void get() {
System.out.println("南方苹果");
}
}
public class SouthBanana extends AbstractBanana {
public void get() {
System.out.println("南方香蕉");
}
}
public class SouthFruitFactory implements IFruitFactory {
public IFruit getApple() {
return new SouthApple();
}
public IFruit getBanana() {
return new SouthBanana();
}
}
package methodFactory;
public interface People {
void say();
}
package methodFactory;
public class Man implements People{
public void say() {
System.out.println("男人");
}
}
package methodFactory;
public class Woman implements People{
public void say() {
System.out.println("女人");
}
}
package methodFactory;
public interface PeopleFactory {
People create();
}
package methodFactory;
public class ManFactory implements PeopleFactory{
public People create() {
return new Man();
}
}
package methodFactory;
public class WomanFactory implements PeopleFactory{
public People create() {
return new Woman();
}
}
package methodFactory;
public class Test {
public static void main(String[] args) {
PeopleFactory manf= new ManFactory();
People man = manf.create();
man.say();
PeopleFactory wf= new WomanFactory();
People w = wf.create();
w.say();
}
}
好处是新增加的子类不会影响以前的实现,代码的扩展性好。
package simpleFactory;
public interface People {
void say();
}
package simpleFactory;
public class Man implements People{
public void say() {
System.out.println("男人");
}
}
package simpleFactory;
public class Woman implements People{
public void say() {
System.out.println("女人");
}
}
package simpleFactory;
public class SimpleFactory {
public static People create(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
Class class1 = Class.forName(className);
return (People) class1.newInstance();
}
}
package simpleFactory;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
People man = SimpleFactory.create("simpleFactory.Man");
People woman = SimpleFactory.create("simpleFactory.Woman");
man.say();
woman.say();
}
}
/**
* 处理接收任务
*/
@Test
public void test4(){
String executionId = "2101";
pe.getRuntimeService().signal(executionId );
}
由于接收任务在任务表中没有任务,所有可以传递流程实例的ID或者执行ID处理接收任务。
package com.task.group;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
/**
* 公共任务测试
*
*
*/
public class GroupTaskTest {
static ProcessEngine pe =null;
static{
ProcessEngineConfiguration conf = ProcessEngineConfiguration.
createStandaloneProcessEngineConfiguration();
conf.setJdbcDriver("com.mysql.jdbc.Driver");
conf.setJdbcUrl("jdbc:mysql://localhost:3306/activiti02?useUnicode=true&characterEncoding=UTF-8");
conf.setJdbcUsername("root");
conf.setJdbcPassword("root");
conf.setDatabaseSchemaUpdate("true");
pe = conf.buildProcessEngine();
}
/**
* 部署流程定义
*/
@Test
public void test1() {
DeploymentBuilder deploymentBuilder = pe.getRepositoryService()
.createDeployment();
deploymentBuilder
.addClasspathResource("com/task/group/groupTask.bpmn");
deploymentBuilder .addClasspathResource("com/task/group/groupTask.png");
Deployment deployment = deploymentBuilder.deploy();
}
/**
* 启动流程实例
*/
@Test
public void test2(){
String processDefinitionId = "grouptTask:1:7404";
pe.getRuntimeService().startProcessInstanceById(processDefinitionId);
}
/**
* 办理个人任务
*/
@Test
public void test3(){
String taskId = "7504";
pe.getTaskService().complete(taskId);
}
/**
* 查询公共任务列表
*/
@Test
public void test4(){
TaskQuery query = pe.getTaskService().createTaskQuery();
String candidateUser = "王五";
//根据候选人过滤
query.taskCandidateUser(candidateUser);
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getName());
}
}
/**
* 拾取任务(将公共任务变为个人任务)
*/
@Test
public void test5(){
String taskId = "7602";
String userId = "王五";
pe.getTaskService().claim(taskId , userId);
}
/**
* 退回任务(将个人任务变为公共任务)
*/
@Test
public void test6(){
String taskId = "1602";
pe.getTaskService().setAssignee(taskId , null);
}
}
/**
* RuntimeService得到流程变量
*/
@org.junit.Test
public void testRuntimeServiceGetVar(){
String executionId="5701";
Map<String, Object> variables = processEngine.getRuntimeService().getVariables(executionId);
}
/**
* taskService得到流程变量
*/
@org.junit.Test
public void testTaskServiceGetVar(){
String taskId="5804";
Map<String, Object> variables = processEngine.getTaskService().getVariables(taskId);
}
/**
* 在启动流程实例时设置流程变量
*/
@org.junit.Test
public void testStartProcessInstanceByKey(){
String processDefinitionKey="qjlc";
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("k1", 11);
variables.put("k2", 22);
ProcessInstance query = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);
}
/**
* 在办理任务时设置流程变量
*/
@org.junit.Test
public void testTaskComplete(){
String taskId="6002";
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("k3", 11);
variables.put("k4", 22);
processEngine.getTaskService().complete(taskId, variables);
}
/**
* RuntimeService设置流程变量
*/
@org.junit.Test
public void testRuntimeService(){
String executionId="5701";
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("k5", 3);
variables.put("k6", 4);
processEngine.getRuntimeService().setVariables(executionId, variables);
}
/**
* taskService设置流程变量
*/
@org.junit.Test
public void testTaskService(){
String taskId="5804";
Map<String,Object> variables = new HashMap<String, Object>();
variables.put("k5", 31);
variables.put("k6", 41);
processEngine.getTaskService().setVariables(taskId, variables);
}
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class CallAbleTest {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService service = Executors.newFixedThreadPool(2);
MyCallAbled m1= new MyCallAbled("aa");
MyCallAbled m2= new MyCallAbled("bb");
Future future1 = service.submit(m1);
Future future2 =service.submit(m2);
System.out.println(future1.get().toString());
System.out.println(future2.get().toString());
service.shutdown();
}
static class MyCallAbled implements Callable{
private String name;
public MyCallAbled(String name){
this.name=name;
}
public MyCallAbled(){
}
@Override
public Object call() throws Exception {
return name;
}
}
}