{"id":10497,"date":"2015-07-07T12:07:17","date_gmt":"2015-07-07T04:07:17","guid":{"rendered":"https:\/\/lifeparty.idv.tw\/blog\/?p=10497"},"modified":"2015-07-07T14:09:57","modified_gmt":"2015-07-07T06:09:57","slug":"%e4%bb%a5%e5%bb%b6%e9%81%b2%e9%81%8b%e7%ae%97%e5%af%a6%e7%8f%be%e6%b5%81%e7%a8%8b%e6%a8%a1%e7%b5%84%e5%8c%96","status":"publish","type":"post","link":"https:\/\/lifeparty.idv.tw\/blog\/archives\/10497","title":{"rendered":"\u4ee5\u5ef6\u9072\u904b\u7b97\u5be6\u73fe\u6d41\u7a0b\u6a21\u7d44\u5316"},"content":{"rendered":"<p>\u5728\u670d\u52d9\u5c0e\u5411\u67b6\u69cb\u7684\u670d\u52d9\u5be6\u4f5c\u7576\u4e2d\uff0c\u6bcf\u4e00\u500b\u670d\u52d9\u8655\u7406\u7684\u4e3b\u8981\u6d41\u7a0b\u5927\u81f4\u4e0a\u6703\u4f9d\u7167\u4e0b\u5217\u6b65\u9a5f\u4f86\u5be6\u4f5c\uff1a<\/p>\n<ol>\n<li>\u62c6\u89e3\u8a0a\u606f<\/li>\n<li>\u9a57\u8b49\u6b04\u4f4d<\/li>\n<li>\u8cc7\u6599\u8655\u7406\u53ca\u904b\u7b97<\/li>\n<li>\u56de\u61c9\u8655\u7406\u7d50\u679c<\/li>\n<\/ol>\n<p>\u7136\u800c\uff0c\u5728\u5927\u90e8\u5206\u9a57\u8b49\u6b04\u4f4d\u53ca\u5c11\u90e8\u5206\u8cc7\u6599\u8655\u7406\u53ca\u904b\u7b97\u7684\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u5e38\u6703\u78b0\u5230\u9700\u8981\u6d41\u7a0b\u63a7\u5236\u5206\u6b67\u72c0\u6cc1\uff0c\u4f7f\u5f97\u7a0b\u5f0f\u78bc\u7684\u5be6\u4f5c\u8b8a\u5f97\u5f88\u7e41\u8907\u800c\u4e0d\u5bb9\u6613\u62bd\u96e2\u4ee5\u9054\u5230\u91cd\u8907\u4f7f\u7528\u7684\u6a21\u7d44\u3002\u8209\u500b\u4f8b\u5b50\u4f86\u8aaa\uff0c\u4ed8\u6b3e\u7684\u4ea4\u6613\u9700\u8981\u6aa2\u6838\u4ed8\u6b3e\u8005\u5e33\u865f\u3001\u5e33\u6236\u9918\u984d\u3001\u6536\u6b3e\u9280\u884c\u8207\u5e33\u865f\u7b49\u8cc7\u8a0a\uff0c\u7a0b\u5f0f\u78bc\u5927\u6982\u6703\u9577\u5f97\u50cf\u9019\u6a23\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">public PaymentResult pay(PaymentRequest request) {\r\n  PaymentResult response = PaymentResult.getNoting();\r\n\r\n  if (isPayerValidated(request)) {\r\n    if (isPayerBalanceEnough(request)) {\r\n      if (isPayeeValidated(request)) {\r\n        response = debitPayer(request);\r\n        sendFinpay(request);\r\n      }\r\n      else {\r\n        response = getBansta(request, PAYEE_IS_NOT_AUTHORITY);\r\n      }\r\n    }\r\n    else {\r\n        response = getBansta(request, BALANCE_IS_NOT_ENOUGH);\r\n    }\r\n  }\r\n  else {\r\n    response = getBansta(request, PAYER_IS_NOT_AUTHORITY);\r\n  }\r\n  return response;\r\n}<\/pre>\n<p>\u7576\u7136\uff0c\u4e0a\u9762\u9019\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u9084\u53ef\u4ee5\u5f9e\u5de2\u72c0 if \u7684\u5beb\u6cd5\u6539\u6210\u677f\u72c0 if\uff0c\u518d\u52a0\u4e0a<a href=\"https:\/\/lifeparty.idv.tw\/blog\/archives\/9967\">\u9632\u885b\u5b50\u53e5<\/a>\uff0c\u7a0b\u5f0f\u78bc\u6703\u8b8a\u5f97\u6bd4\u8f03\u6e05\u6670\u53ef\u8b80\uff0c\u4f46\u7121\u53ef\u907f\u514d\u7684\uff0c\u5728\u7a0b\u5f0f\u78bc\u7576\u4e2d\u9084\u662f\u514d\u4e0d\u4e86\u8981\u8655\u7406\u975e\u4e3b\u6d41\u7a0b\u7684\u5206\u652f\u8655\u7406\u3002\u9019\u6a23\u7684\u7f3a\u9ede\u662f\u589e\u52a0\u6bcf\u4e00\u500b\u670d\u52d9\u7684\u5be6\u4f5c\uff0c\u9019\u4e9b\u975e\u4e3b\u8981\u6d41\u7a0b\u7684\u5206\u652f\u8655\u7406\u90fd\u7121\u6cd5\u7701\u7565\uff0c\u7121\u5f62\u4e4b\u4e2d\u5c07\u6703\u5206\u6563\u7de8\u7a0b\u8005\u805a\u7126\u7a0b\u5ea6\u8207\u589e\u52a0\u4eba\u70ba\u932f\u8aa4\u7684\u758f\u5931\u3002<\/p>\n<p>\u5176\u5be6\uff0c\u8981\u89e3\u6c7a\u9019\u500b\u554f\u984c\u4e26\u4e0d\u56f0\u96e3\uff0c\u4f46\u9700\u8981\u8a2d\u8a08\u601d\u7dad\u7684\u8f49\u8b8a\uff0c\u53ea\u9700\u8981\u628a\u547d\u4ee4\u5f0f\u7de8\u7a0b\u6539\u6210\u5ba3\u544a\u5f0f\u7de8\u7a0b\u5c31\u53ef\u4ee5\u4e86\uff0c\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u904b\u7528\u5ef6\u9072\u904b\u7b97\u7684\u624b\u6cd5\u4f86\u6e1b\u5c11\u5206\u652f\u8655\u7406\u7684\u51fa\u73fe\u6b21\u6578\u3002<!--more-->\u90a3\u9ebc\u9019\u8981\u600e\u9ebc\u505a\u5462\uff1f\u6211\u5011\u5148\u4f86\u770b\u770b\u4e0b\u9762\u9019\u4e00\u6bb5\u5ba3\u544a\u5f0f\u7de8\u7a0b\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">public PaymentResult pay(PaymentRequest request) {\r\n  PaymentResultContext context  =\r\n    processSequenceFactory.forRequest(request)\r\n      .with(payerValidation)\r\n      .with(payerBalanceValidation)\r\n      .with(payeeValidation)\r\n      .with(debitExecution)\r\n      .with(finpayNotification);\r\n      .getResultContext();\r\n\r\n  return context.getResult();\r\n}<\/pre>\n<p>\u6211\u5011\u770b\u5230\u4e0a\u9762\u9019\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u5df2\u7d93\u628a\u6d41\u7a0b\u7684\u6bcf\u4e00\u500b\u6b65\u9a5f\u90fd\u8b8a\u6210\u4ed8\u6b3e\u6a21\u7d44\u7684\u5143\u4ef6\uff0c\u5b83\u5011\u53ef\u4ee5\u8996\u696d\u52d9\u6d41\u7a0b\u7684\u9700\u8981\u7d44\u5408\u5728\u4e00\u8d77\u4f86\u5f7c\u6b64\u5408\u4f5c\uff0c\u4f86\u5b8c\u6210\u4e00\u6b21\u7684\u6d41\u7a0b\u5be6\u4f8b\uff0c\u540c\u4eba\u7a31\u547c\u9019\u6d41\u7a0b\u5be6\u4f8b\u53eb\u4f5c ProcessSequence\uff0c\u540c\u6642\u4e5f\u5b9a\u7fa9\u751f\u6210\u5b83\u7684 Factory \u754c\u9762\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">public interface ProcessSequence&lt;RequestType, ContextType&gt; {\r\n  public ProcessSequence&lt;RequestType, ContextType&gt; with(\r\n    ProcessSequenceAction&lt;RequestType, ContextType&gt; action);\r\n\r\n  public ContextTyep getResult()\r\n    throws ProcessSequenceUnhandledException;\r\n\r\n}\r\n\r\npublic interface ProcessSequenceAction&lt;RequestType,\r\n  ContextType&gt; {\r\n  public void execute(RequestType request, ContextType context)\r\n    throws ProcessSequenceActionException;\r\n}\r\n\r\npublic interface ProcessSequenceFactory&lt;RequestType,\r\n  ContextType&gt; {\r\n  public ProcessSequence&lt;RequestType, ContextType&gt; forRequest(\r\n    RequestType request);\r\n\r\n}<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5be6\u4f5c ProcessSequenceBase \u4f86\u7576\u6210\u5404\u500b\u5be6\u73fe ProcessSequence \u7684 template \u57fa\u790e\u985e\u5225\uff0c\u4e3b\u8981\u662f\u5c01\u88dd\u4e86\u62bd\u8c61\u65b9\u6cd5 initiateSequenceContext \u7528\u4f86\u521d\u59cb\u5316 ContextType \u7684\u5be6\u4f8b\uff0c\u9019\u500b ContextType \u7684\u5be6\u4f8b\u6240\u626e\u6f14\u7684\u5176\u5be6\u5c31\u662f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Builder_pattern\">GOF \u5efa\u9020\u8005\u6a23\u5f0f<\/a>\u7684 Builder \u89d2\u8272\uff0c\u5b83\u6703\u900f\u904e\u5404\u500b\u50b3\u7d66 with() \u7684 ProcessSequenceAction \u4f86\u9010\u6b65\u5efa\u9020 ResultContext\uff0c\u7136\u5f8c\u900f\u904e ProcessSequence \u7684 getResult() \u5f97\u5230\u6700\u5f8c\u7684 ResultContext\uff0c\u518d\u5f9e \u9019\u500b ResultContext \u53d6\u5f97\u56de\u61c9\u7d50\u679c\u3002ProcessSequenceBase \u7684\u5be6\u4f5c\u5982\u4e0b\u5982\u793a\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">public interface ProcessSequenceExceptionHandler&lt;ContextType&gt; {\r\n\r\n  public boolean processException(Throwable ex, \r\n    ContextType result);\r\n\r\n}\r\n\r\npublic abstract class ProcessSequenceBase&lt;RequestType, \r\n  ContextType&gt; implements ProcessSequence&lt;RequestType, \r\n  ContextType&gt; {\r\n\r\n  private RequestType request;\r\n\r\n  private List&lt;ProcessSequenceAction&lt;RequestType, \r\n    ContextType&gt;&gt; sequenceActions = \r\n    new ArrayList&lt;ProcessSequenceAction&lt;RequestType, \r\n    ContextType&gt;&gt;();\r\n\r\n  public ProcessSequenceBase(RequestType request) {\r\n    this.request = request;\r\n  }\r\n\r\n  protected abstract ContextType initiateSequenceContext(\r\n    RequestType request);\r\n\r\n  protected ProcessSequenceExceptionHandler&lt;ContextType&gt; \r\n    getExceptionHandler() {\r\n    return null;\r\n  }\r\n\r\n  @Override\r\n  public ProcessSequence&lt;RequestType, ContextType&gt; with(\r\n    ProcessSequenceAction&lt;RequestType, ContextType&gt; action) {\r\n      sequenceActions.add(action);\r\n      return this;\t\r\n    }\r\n\r\n  @Override\r\n  public ContextType getResult() \r\n    throws ProcessSequenceUnhandledException {\r\n    Iterator&lt;ProcessSequenceAction&lt;RequestType, ContextType&gt;&gt; \r\n      actionIterator = sequenceActions.iterator();\r\n\r\n    final ContextType context = \r\n      initiateSequenceContext(request);\r\n\r\n    boolean broken = false;\r\n    while (!broken &amp;&amp; actionIterator.hasNext()) {\r\n      ProcessSequenceAction&lt;RequestType, ContextType&gt; \r\n        sequenceAction = actionIterator.next();\r\n      try {\r\n        sequenceAction.execute(request, context);\r\n      } catch (Throwable ex) {\r\n        if (ex instanceof ProcessSequenceUnhandledException) {\r\n          throw (ProcessSequenceUnhandledException) ex;\r\n        }\r\n\r\n        ProcessSequenceExceptionHandler&lt;ContextType&gt; \r\n          exHandler = getExceptionHandler();\r\n\r\n        broken = ((null == exHandler) ? true : \r\n          exHandler.processException(ex, context));\r\n      }\r\n    }\r\n    return context;\r\n  }\r\n}<\/pre>\n<p>\u4e0a\u6bb5\u7a0b\u5f0f\u78bc\u6709\u4e00\u500b\u503c\u5f97\u6ce8\u610f\u7684\u5730\u65b9\u662f getExceptionHandler() \u9810\u8a2d\u8655\u7406 ProcessSequenceAction \u62cb\u51fa\u7684\u7570\u5e38\u662f\u56de\u50b3 null\uff0c\u4ee3\u8868\u4e0d\u505a\u7279\u5225\u7684\u7570\u5e38\u8655\u7406\uff0c\u4e5f\u5c31\u662f\u7576\u767c\u751f\u7570\u5e38\u6642\u4e2d\u65b7\u6d41\u7a0b\u7684\u8655\u7406\u3002\u7576\u7136\uff0c\u7e7c\u627f ProcessSequenceBase \u7684\u884d\u751f\u985e\u5225\u53ef\u4ee5\u8986\u5beb getExceptionHandler() \u4f86\u7279\u5b9a\u7684\u7570\u5e38\u8655\u7406\u800c\u4e14\u53ef\u4ee5\u6c7a\u5b9a\u662f\u5426\u4e2d\u65b7\u6d41\u7a0b\u3002\u9019\u8ce6\u4e88\u66f4\u591a\u7684\u5f48\u6027\uff0c\u8b93\u6211\u5011\u53ef\u6839\u64da\u4e0d\u540c\u7a2e\u985e\u7684 Exception \u4f86\u63a7\u5236\u6d41\u7a0b\uff0c\u7576\u6d41\u7a0b\u88ab\u4e2d\u65b7\u6642\uff0cProcessSequenceBase \u6703\u4e2d\u65b7 getResult() \u800c\u62cb\u51fa\u6709\u7570\u5e38\u672a\u88ab\u8655\u7406\u7684\u7570\u5e38 ProcessSequenceUnhandledException\uff0c\u6514\u622a\u6b64\u7570\u5e38\u53ef\u900f\u904e getCause() \u53d6\u5f97\u672a\u88ab\u8655\u7406\u7684\u7570\u5e38\u3002<\/p>\n<p>\u65bc\u662f\uff0c\u6211\u5011\u6309\u7167\u4ee5\u4e0a\u7684\u898f\u683c\u5be6\u4f5c\u51fa PaymentProcessSequenceFactory \u548c PaymentProcessSequence\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">public class PaymentProcessSequenceFactory\r\n  implements ProcessSequenceFactory&lt;PaymentRequest, \r\n  PaymentResultContext&gt; {\r\n\r\n  @Override\r\n  public ProcessSequence&lt;PaymentRequest, \r\n    PaymentResultContext&gt; forRequest(\r\n      PaymentRequest request) {\r\n    return new PaymentProcessSequence(request);\r\n  }\r\n}\r\n\r\npublic class PaymentProcessSequence \r\n  extends ProcessSequenceBase&lt;PaymentRequest, \r\n  PaymentResultContext&gt; {\r\n\r\n  public PaymentProcessSequence(PaymentRequest request) {\r\n    super(request);\r\n  }\r\n\r\n  @Override\r\n  protected ClientResultContext initiateSequenceContext(\r\n    PaymentRequest request) {\r\n    PaymentResultContext result = new PaymentResultContext(\r\n      request);\r\n    return result;\r\n  }\r\n}<\/pre>\n<p>\u5f9e\u4ee5\u4e0a\u4ee5\u5ef6\u9072\u904b\u7b97\u5be6\u73fe\u6d41\u7a0b\u6a21\u7d44\u5316\u8b93\u6211\u5011\u770b\u5230\uff0c\u9019\u500b\u8a2d\u8a08\u6a23\u5f0f\u548c\u540c\u4eba\u5148\u524d\u5206\u4eab\u904e\u7684<a href=\"https:\/\/lifeparty.idv.tw\/blog\/archives\/10196\">\u8a9e\u8a00\u6574\u5408\u67e5\u8a62<\/a>\u6709\u4e00\u4e9b\u4e0d\u4e00\u6a23\u7684\u5730\u65b9\u3002\u8a9e\u8a00\u6574\u5408\u67e5\u8a62\u662f\u904b\u7528\u8868\u793a\u5f0f\u7684\u5957\u758a\u904b\u7b97\u7684\u539f\u7406\uff1b\u800c\u9019\u500b\u8a2d\u8a08\u5247\u662f\u5f15\u7528 Context \u7684\u6982\u5ff5\uff0c\u8b93\u5404\u500b\u6d41\u7a0b\u6a21\u7d44\u5143\u4ef6\u53ef\u4ee5\u5b58\u53d6\u4e26\u5206\u4eab\u8655\u7406\u904e\u7a0b\u4e2d\u7684\u8cc7\u6599\u8b8a\u5316\uff0c\u4e26\u4e14\u904b\u7528 Exception Handler \u7684\u6a5f\u5236\uff0c\u4f86\u9054\u5230\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u81f4\u6027\u3002\u6211\u5011\u767c\u73fe Context \u7684\u672c\u8cea\u6b63\u662f GOF \u7684\u5efa\u9020\u8005\u6a23\u5f0f\uff0c\u6709\u5225\u65bc\u4ee5 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Value_object\">Value Object<\/a> \u7d44\u5408\u56de\u50b3\u7d50\u679c\u7684\u7684\u65b9\u5f0f\uff0c\u63a1\u7528 Builder \u7dad\u6301\u6211\u5011\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstraction_(computer_science)\">\u62bd\u8c61\u6982\u5ff5<\/a>\uff0c\u4e5f\u4e0d\u6703\u9055\u53cd Value Object \u61c9\u8a72\u662f<a href=\"https:\/\/en.wikipedia.org\/wiki\/Immutable_object\">\u4e0d\u8b8a\u7269\u4ef6<\/a>\u7684\u539f\u5247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f9e\u4ee5\u4e0a\u4ee5\u5ef6\u9072\u904b\u7b97\u5be6\u73fe\u6d41\u7a0b\u6a21\u7d44\u5316\u8b93\u6211\u5011\u770b\u5230\uff0c\u9019\u500b\u8a2d\u8a08\u6a23\u5f0f\u548c\u540c\u4eba\u5148\u524d\u5206\u4eab\u904e\u7684\u8a9e\u8a00\u6574\u5408\u67e5\u8a62\u6709\u4e00\u4e9b\u4e0d\u4e00\u6a23\u7684\u5730\u65b9\u3002\u8a9e\u8a00\u6574\u5408\u67e5\u8a62\u662f\u904b\u7528\u8868\u793a\u5f0f\u7684\u5957\u758a\u904b\u7b97\u7684\u539f\u7406\uff1b\u800c\u9019\u500b\u8a2d\u8a08\u5247\u662f\u5f15\u7528 Context \u7684\u6982\u5ff5\uff0c\u8b93\u5404\u500b\u6d41\u7a0b\u6a21\u7d44\u5143\u4ef6\u53ef\u4ee5\u5b58\u53d6\u4e26\u5206\u4eab\u8655\u7406\u904e\u7a0b\u4e2d\u7684\u8cc7\u6599\u8b8a\u5316\uff0c\u4e26\u4e14\u904b\u7528 Exception Handler \u7684\u6a5f\u5236\uff0c\u4f86\u9054\u5230\u6d41\u7a0b\u63a7\u5236\u7684\u4e00\u81f4\u6027\u3002 <a href=\"https:\/\/lifeparty.idv.tw\/blog\/archives\/10497\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,37,19],"tags":[],"class_list":["post-10497","post","type-post","status-publish","format-standard","hentry","category-29","category-37","category-19"],"_links":{"self":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/10497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/comments?post=10497"}],"version-history":[{"count":71,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/10497\/revisions"}],"predecessor-version":[{"id":10567,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/10497\/revisions\/10567"}],"wp:attachment":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/media?parent=10497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/categories?post=10497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/tags?post=10497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}