java學(xué)習(xí)

          java學(xué)習(xí)

           

          設(shè)計模式之職責(zé)鏈實現(xiàn)攔截器棧

          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());
          }
          }

          posted @ 2017-09-04 17:52 楊軍威 閱讀(264) | 評論 (0)編輯 收藏

          設(shè)計模式之單例模式

          單例模式分為惡漢式,就是直接在類中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();
          }
          }

          posted @ 2017-09-04 10:17 楊軍威 閱讀(123) | 評論 (0)編輯 收藏

          設(shè)計模式之抽象工廠

          具體的水果工廠實現(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();
          }
          }

          posted @ 2017-09-01 17:04 楊軍威 閱讀(125) | 評論 (0)編輯 收藏

          設(shè)計模式之工廠方法

          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),代碼的擴展性好。

          posted @ 2017-09-01 15:10 楊軍威 閱讀(142) | 評論 (0)編輯 收藏

          設(shè)計模式之簡單工廠

          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();
          }
          }

          posted @ 2017-09-01 14:18 楊軍威 閱讀(105) | 評論 (0)編輯 收藏

          activiti處理接收任務(wù)

          /**
          * 處理接收任務(wù)
          */
          @Test
          public void test4(){
          String executionId = "2101";
          pe.getRuntimeService().signal(executionId );
          }
          由于接收任務(wù)在任務(wù)表中沒有任務(wù),所有可以傳遞流程實例的ID或者執(zhí)行ID處理接收任務(wù)。

          posted @ 2017-08-29 11:10 楊軍威 閱讀(405) | 評論 (0)編輯 收藏

          activiti辦理組任務(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&amp;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);
          }
          }

          posted @ 2017-08-29 10:42 楊軍威 閱讀(342) | 評論 (0)編輯 收藏

          activiti得到流程變量的2中方案

          /**
          * 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);
          }

          posted @ 2017-08-28 17:31 楊軍威 閱讀(176) | 評論 (0)編輯 收藏

          activiti設(shè)置流程變量的4中方案

          /**
          * 在啟動流程實例時設(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);
          }

          posted @ 2017-08-28 17:15 楊軍威 閱讀(296) | 評論 (0)編輯 收藏

          java實現(xiàn)有返回值的線程

          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;
          }
          }
          }

          posted @ 2017-08-17 17:26 楊軍威 閱讀(187) | 評論 (0)編輯 收藏

          僅列出標題
          共43頁: First 上一頁 6 7 8 9 10 11 12 13 14 下一頁 Last 

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 陆河县| 遵义市| 伊川县| 东乌珠穆沁旗| 河源市| 迁安市| 郁南县| 泾川县| 阳泉市| 湛江市| 西昌市| 合山市| 九龙县| 昌吉市| 武威市| 甘南县| 滦南县| 贡觉县| 宁陵县| 宜丰县| 辽宁省| 龙川县| 沁源县| 博爱县| 罗城| 沅江市| 昌图县| 凭祥市| 体育| 烟台市| 大英县| 武安市| 丰县| 鄂州市| 开江县| 梓潼县| 黄大仙区| 葫芦岛市| 调兵山市| 杭锦旗| 乌兰察布市|