当前位置:   article > 正文

自创设计模式-----答题卡模式_element-plus, 答题卡

element-plus, 答题卡

该模式是解决触发条件和触发结果的解耦,同时兼顾触发条件资源的可释放性。目的是为了获得触发结果。

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. import java.util.Hashtable;
  6. import com.baidu.coolapp.composite.IComposite;
  7. /**
  8. * @author liweigao
  9. *
  10. */
  11. public interface IJury extends IComposite
  12. > {
  13. void check();
  14. Hashtable
  15. > getResults();
  16. Hashtable
  17. > getQuestions();
  18. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. /**
  6. * @author liweigao
  7. *
  8. */
  9. public interface IResult
  10. {
  11. void setResult(T result);
  12. T getResult();
  13. void setTag(String tag);
  14. String getTag();
  15. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. /**
  6. * @author liweigao
  7. *
  8. */
  9. public interface IQuestion
  10. {
  11. T answer();
  12. IResult
  13. createResult(Object... args);
  14. String getTag();
  15. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. /**
  6. * @author liweigao 2014年11月20日
  7. * @param
  8. */
  9. public abstract class AResult
  10. implements IResult
  11. {
  12. private T mResult;
  13. private String mTag;
  14. /*
  15. * (non-Javadoc)
  16. *
  17. * @see common.share.lwg.trueorfalse.IResult#setTag(java.lang.String)
  18. */
  19. @Override
  20. public void setTag(String tag) {
  21. this.mTag = tag;
  22. }
  23. /*
  24. * (non-Javadoc)
  25. *
  26. * @see common.share.lwg.trueorfalse.IResult#getTag()
  27. */
  28. @Override
  29. public String getTag() {
  30. // TODO Auto-generated method stub
  31. return this.mTag;
  32. }
  33. /*
  34. * (non-Javadoc)
  35. *
  36. * @see common.share.lwg.answer.IResult#setResult(java.lang.Object)
  37. */
  38. @Override
  39. public void setResult(T result) {
  40. this.mResult = result;
  41. }
  42. /*
  43. * (non-Javadoc)
  44. *
  45. * @see common.share.lwg.answer.IResult#getResult()
  46. */
  47. @Override
  48. public T getResult() {
  49. return this.mResult;
  50. }
  51. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. /**
  6. * @author liweigao
  7. *
  8. */
  9. public abstract class AQuestion
  10. implements IQuestion
  11. {
  12. private String mTag;
  13. public AQuestion(String tag) {
  14. this.mTag = tag;
  15. }
  16. /* (non-Javadoc)
  17. * @see common.share.lwg.trueorfalse.IQuestion#getTag()
  18. */
  19. @Override
  20. public String getTag() {
  21. // TODO Auto-generated method stub
  22. return this.mTag;
  23. }
  24. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.answer;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7. /**
  8. * @author liweigao
  9. *
  10. */
  11. public abstract class AJury implements IJury {
  12. private Hashtable
  13. > mQuestions = new Hashtable
  14. >();
  15. private Hashtable
  16. > mResults = new Hashtable
  17. >();
  18. /*
  19. * (non-Javadoc)
  20. *
  21. * @see common.share.lwg.composite.IComposite#add(java.lang.Object)
  22. */
  23. @Override
  24. public void add(IQuestion
  25. object) {
  26. String addTag = object.getTag();
  27. this.mQuestions.put(addTag, object);
  28. IResult
  29. result = object.createResult();
  30. result.setTag(addTag);
  31. this.mResults.put(addTag, result);
  32. }
  33. /*
  34. * (non-Javadoc)
  35. *
  36. * @see common.share.lwg.composite.IComposite#remove(java.lang.Object)
  37. */
  38. @Override
  39. public void remove(IQuestion
  40. object) {
  41. String tag = object.getTag();
  42. this.mQuestions.remove(tag);
  43. this.mResults.remove(tag);
  44. }
  45. /*
  46. * (non-Javadoc)
  47. *
  48. * @see common.share.lwg.trueorfalse.Ireferee#check()
  49. */
  50. @SuppressWarnings({ "rawtypes", "unchecked" })
  51. @Override
  52. public void check() {
  53. Enumeration
  54. > enus = mQuestions.elements(); while (enus.hasMoreElements()) { IQuestion
  55. question = enus.nextElement(); String tag = question.getTag(); IResult result = mResults.get(tag); result.setResult(question.answer()); } } /* * (non-Javadoc) * * @see common.share.lwg.answer.IJury#getResults() */ @Override public Hashtable
  56. > getResults() { return this.mResults; } /* * (non-Javadoc) * * @see common.share.lwg.answer.IJury#getQuestions() */ @Override public Hashtable
  57. > getQuestions() { return this.mQuestions; } }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.config.answer.sweetbox;
  5. import com.baidu.coolapp.config.mediator.IConfigColleague;
  6. /**
  7. * @author liweigao
  8. * 2014年11月21日
  9. */
  10. public class ResultBean {
  11. private IConfigColleague mSweetFile;
  12. private IConfigColleague mBoxFile;
  13. private boolean mNeedMerge = false;
  14. private boolean mNeedUpload = true;
  15. private boolean mNeedLoadConfig = false;
  16. /**
  17. * @return the sweetFile
  18. */
  19. public IConfigColleague getSweetFile() {
  20. return mSweetFile;
  21. }
  22. /**
  23. * @param sweetFile the sweetFile to set
  24. */
  25. public void setSweetFile(IConfigColleague sweetFile) {
  26. this.mSweetFile = sweetFile;
  27. }
  28. /**
  29. * @return the boxFile
  30. */
  31. public IConfigColleague getBoxFile() {
  32. return mBoxFile;
  33. }
  34. /**
  35. * @param boxFile the boxFile to set
  36. */
  37. public void setBoxFile(IConfigColleague boxFile) {
  38. this.mBoxFile = boxFile;
  39. }
  40. /**
  41. * @return the needMerge
  42. */
  43. public boolean isNeedMerge() {
  44. return mNeedMerge;
  45. }
  46. /**
  47. * @param needMerge the needMerge to set
  48. */
  49. public void setNeedMerge(boolean needMerge) {
  50. this.mNeedMerge = needMerge;
  51. }
  52. /**
  53. * @return the needUpload
  54. */
  55. public boolean isNeedUpload() {
  56. return mNeedUpload;
  57. }
  58. /**
  59. * @param needUpload the needUpload to set
  60. */
  61. public void setNeedUpload(boolean needUpload) {
  62. this.mNeedUpload = needUpload;
  63. }
  64. /**
  65. * @return the needLoadConfig
  66. */
  67. public boolean isNeedLoadConfig() {
  68. return mNeedLoadConfig;
  69. }
  70. /**
  71. * @param needLoadConfig the needLoadConfig to set
  72. */
  73. public void setNeedLoadConfig(boolean needLoadConfig) {
  74. this.mNeedLoadConfig = needLoadConfig;
  75. }
  76. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.config.answer.sweetbox;
  5. import java.io.File;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.Map;
  8. import org.apache.http.Header;
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.entity.StringEntity;
  11. import org.json.JSONException;
  12. import org.json.JSONObject;
  13. import android.content.Context;
  14. import com.baidu.java.HashMap;
  15. import com.loopj.android.http.JsonHttpResponseHandler;
  16. import com.loopj.android.http.RequestParams;
  17. import com.loopj.android.http.SyncHttpClient;
  18. import com.baidu.coolapp.answer.AJury;
  19. import com.baidu.coolapp.answer.IResult;
  20. import com.baidu.coolapp.config.mediator.ConfigMediator;
  21. import com.baidu.coolapp.config.mediator.IConfigColleague;
  22. import com.baidu.coolapp.config.merge.ConfigMergeProxy;
  23. import com.baidu.coolapp.config.merge.IMergeProxy;
  24. import com.baidu.coolapp.enumtype.FileEnum;
  25. import com.baidu.coolapp.enumtype.Id;
  26. import com.baidu.coolapp.enumtype.Key;
  27. import com.baidu.coolapp.enumtype.Tag;
  28. import com.baidu.coolapp.tools.JsonTool;
  29. import com.baidu.coolapp.tools.PropertyTool;
  30. import com.baidu.coolapp.tools.ResourceTool;
  31. import com.baidu.coolapp.tools.UserTool;
  32. /**
  33. * @author liweigao 2014年11月21日
  34. */
  35. public class SweetBoxJury extends AJury {
  36. private ConfigMediator mConfigMediator;
  37. public SweetBoxJury(ConfigMediator mediator) {
  38. this.mConfigMediator = mediator;
  39. }
  40. public void checkTwoFile(IConfigColleague left, IConfigColleague right,
  41. Context context) {
  42. this.add(new SweetBoxQuestion(Tag.SWEET_BOX_QUESTION.getName(), left,
  43. right));
  44. this.check();
  45. ResultBean resultBean = getSweetBoxResultBean();
  46. if (resultBean.isNeedMerge()) {
  47. IConfigColleague sweet = resultBean.getSweetFile();
  48. IConfigColleague box = resultBean.getBoxFile();
  49. if (Id.SERVER_CONFIG.getId().equals(sweet.getId())
  50. || Id.SERVER_CONFIG.getId().equals(box.getId())) {
  51. downLoadServerFile(context);
  52. } else {
  53. }
  54. mergeFile(sweet, box, context);
  55. if (Id.SERVER_CONFIG.getId().equals(box.getId())) {
  56. mConfigMediator.replaceFile(sweet, box);
  57. } else {
  58. }
  59. if (Id.BACK_CONFIG.getId().equals(box.getId())) {
  60. mConfigMediator.replaceFile(sweet, box);
  61. } else {
  62. }
  63. } else {
  64. IConfigColleague boxFile = resultBean.getBoxFile();
  65. if (Id.SERVER_CONFIG.getId().equals(boxFile.getId())) {
  66. downLoadServerFile(context);
  67. mConfigMediator.replaceFile(resultBean.getSweetFile(),
  68. resultBean.getBoxFile());
  69. } else {
  70. }
  71. }
  72. }
  73. private void mergeFile(IConfigColleague sweet, IConfigColleague box,
  74. Context context) {
  75. }
  76. private void downLoadServerFile(final Context context) {
  77. }
  78. @SuppressWarnings("unchecked")
  79. public IResult
  80. getSweetBoxResult() {
  81. IResult
  82. result = this.getResults().get(
  83. Tag.SWEET_BOX_QUESTION.getName());
  84. return (IResult
  85. ) result;
  86. }
  87. public ResultBean getSweetBoxResultBean() {
  88. return getSweetBoxResult().getResult();
  89. }
  90. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.config.answer.sweetbox;
  5. import com.baidu.coolapp.answer.AQuestion;
  6. import com.baidu.coolapp.answer.IResult;
  7. import com.baidu.coolapp.config.mediator.IConfigColleague;
  8. import com.baidu.coolapp.enumtype.Id;
  9. /**
  10. * @author liweigao
  11. * 2014年11月21日
  12. */
  13. public class SweetBoxQuestion extends AQuestion
  14. {
  15. private IConfigColleague mLeft;
  16. private IConfigColleague mRight;
  17. /**
  18. * @param tag
  19. */
  20. public SweetBoxQuestion(String tag,IConfigColleague left,IConfigColleague right) {
  21. super(tag);
  22. this.mLeft = left;
  23. this.mRight = right;
  24. }
  25. /* (non-Javadoc)
  26. * @see common.share.lwg.answer.IQuestion#answer()
  27. */
  28. @Override
  29. public ResultBean answer() {
  30. ResultBean resultBean = new ResultBean();
  31. int leftVersion = mLeft.getVersion();
  32. int rightVersion = mRight.getVersion();
  33. long leftTime = mLeft.getTime();
  34. long rightTime = mRight.getTime();
  35. if(leftVersion > rightVersion) {
  36. resultBean.setBoxFile(mLeft);
  37. resultBean.setSweetFile(mRight);
  38. if(leftTime > rightTime) {
  39. resultBean.setNeedMerge(false);
  40. } else if(leftTime < rightTime) {
  41. resultBean.setNeedMerge(true);
  42. } else {
  43. resultBean.setNeedMerge(false);
  44. }
  45. } else if(leftVersion < rightVersion) {
  46. resultBean.setBoxFile(mRight);
  47. resultBean.setSweetFile(mLeft);
  48. if(leftTime > rightTime) {
  49. resultBean.setNeedMerge(true);
  50. } else if(leftTime < rightTime) {
  51. resultBean.setNeedMerge(false);
  52. } else {
  53. resultBean.setNeedMerge(false);
  54. }
  55. } else {
  56. if(leftTime > rightTime) {
  57. resultBean.setBoxFile(mLeft);
  58. resultBean.setSweetFile(mRight);
  59. resultBean.setNeedMerge(false);
  60. } else if(leftTime < rightTime) {
  61. resultBean.setBoxFile(mRight);
  62. resultBean.setSweetFile(mLeft);
  63. resultBean.setNeedMerge(false);
  64. } else {
  65. resultBean.setNeedMerge(false);
  66. resultBean.setNeedUpload(false);
  67. String leftId = mLeft.getId();
  68. if(leftId.equals(Id.SDCARD_CONFIG.getId())) {
  69. resultBean.setBoxFile(mLeft);
  70. resultBean.setSweetFile(mRight);
  71. } else {
  72. resultBean.setBoxFile(mRight);
  73. resultBean.setSweetFile(mLeft);
  74. }
  75. }
  76. }
  77. return resultBean;
  78. }
  79. /* (non-Javadoc)
  80. * @see common.share.lwg.answer.IQuestion#createResult(java.lang.Object[])
  81. */
  82. @Override
  83. public IResult
  84. createResult(Object... args) {
  85. return new SweetBoxResult();
  86. }
  87. }

  1. /**
  2. *
  3. */
  4. package com.baidu.coolapp.config.answer.sweetbox;
  5. import com.baidu.coolapp.answer.AResult;
  6. /**
  7. * @author liweigao
  8. * 2014年11月21日
  9. */
  10. public class SweetBoxResult extends AResult
  11. {
  12. }







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/768325
推荐阅读
相关标签
  

闽ICP备14008679号