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出,直接返回對象,懶漢式是在調(diào)用對象時判斷對象是否是null,如果null,先new出,再返回,否則直接返回對象,但是這種方式會線程不安全,所以采用雙重檢查的設(shè)計思想,保證線程安全。
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;
}
}
第二種方式,內(nèi)部類方式
package test;
/**
* 在多線程中使用單例對象的設(shè)計模式,內(nèi)部類
*
*/
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();
}
}
具體的水果工廠實現(xiàn)類 | 具體的蘋果實現(xiàn)類 | 具體的香蕉實現(xiàn)類 | 具體的棗實現(xiàn)類 |
中國水果工廠實現(xiàn)類 | 中國蘋果 | 中國香蕉 | 中國棗 |
日本水果工廠實現(xiàn)類 | 日本蘋果 | 日本香蕉 | 日本棗 |
以此類推,在工廠的接口中創(chuàng)建所有水果的方法聲明。
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();
}
}
好處是新增加的子類不會影響以前的實現(xiàn),代碼的擴展性好。
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();
}
}
/**
* 處理接收任務(wù)
*/
@Test
public void test4(){
String executionId = "2101";
pe.getRuntimeService().signal(executionId );
}
由于接收任務(wù)在任務(wù)表中沒有任務(wù),所有可以傳遞流程實例的ID或者執(zhí)行ID處理接收任務(wù)。
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;
/**
* 公共任務(wù)測試
*
*
*/
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);
}
/**
* 辦理個人任務(wù)
*/
@Test
public void test3(){
String taskId = "7504";
pe.getTaskService().complete(taskId);
}
/**
* 查詢公共任務(wù)列表
*/
@Test
public void test4(){
TaskQuery query = pe.getTaskService().createTaskQuery();
String candidateUser = "王五";
//根據(jù)候選人過濾
query.taskCandidateUser(candidateUser);
List<Task> list = query.list();
for (Task task : list) {
System.out.println(task.getName());
}
}
/**
* 拾取任務(wù)(將公共任務(wù)變?yōu)閭€人任務(wù))
*/
@Test
public void test5(){
String taskId = "7602";
String userId = "王五";
pe.getTaskService().claim(taskId , userId);
}
/**
* 退回任務(wù)(將個人任務(wù)變?yōu)楣踩蝿?wù))
*/
@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);
}
/**
* 在啟動流程實例時設(shè)置流程變量
*/
@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);
}
/**
* 在辦理任務(wù)時設(shè)置流程變量
*/
@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設(shè)置流程變量
*/
@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設(shè)置流程變量
*/
@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;
}
}
}