{"id":38029,"date":"2024-11-26T16:09:01","date_gmt":"2024-11-26T08:09:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38029\/"},"modified":"2024-11-26T16:09:01","modified_gmt":"2024-11-26T08:09:01","slug":"%e6%8e%8c%e6%8f%a1%e7%ad%96%e7%95%a5%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%ef%bc%9a%e5%bc%80%e5%8f%91%e4%ba%ba%e5%91%98%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38029\/","title":{"rendered":"\u638c\u63e1\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5f00\u53d1\u4eba\u5458\u6307\u5357"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173148372860836.jpg\" class=\"aligncenter\" title=\"\u638c\u63e1\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5f00\u53d1\u4eba\u5458\u6307\u5357\u63d2\u56fe\" alt=\"\u638c\u63e1\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5f00\u53d1\u4eba\u5458\u6307\u5357\u63d2\u56fe\" \/><\/p>\n<p>\u4f5c\u4e3a\u8f6f\u4ef6\u5de5\u7a0b\u5e08\uff0c\u6211\u4eec\u4e0d\u65ad\u5730\u627f\u62c5\u7740\u521b\u5efa\u53ef\u7ef4\u62a4\u3001\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u7cfb\u7edf\u7684\u4efb\u52a1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ee5\u7ed3\u6784\u5316\u548c\u53ef\u91cd\u7528\u7684\u65b9\u5f0f\u89e3\u51b3\u91cd\u590d\u51fa\u73b0\u7684\u95ee\u9898\u3002\u5176\u4e2d\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u662f<strong>\u7b56\u7565\u6a21\u5f0f<\/strong>\uff0c\u5b83\u662f<strong>\u884c\u4e3a\u6a21\u5f0f<\/strong>\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u5141\u8bb8\u60a8\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5c01\u88c5\u6bcf\u4e2a\u7b97\u6cd5\uff0c\u5e76\u4f7f\u5b83\u4eec\u53ef\u4ee5\u4e92\u6362\u3002\u8fd9\u610f\u5473\u7740\u5ba2\u6237\u7aef\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u6216\u7b56\u7565\uff0c\u800c\u65e0\u9700\u6539\u53d8\u7cfb\u7edf\u7684\u6838\u5fc3\u529f\u80fd\u3002 <\/p>\n<p>\u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\uff0c\u6211\u5c06\u6df1\u5165\u63a2\u8ba8\u7b56\u7565\u6a21\u5f0f\u3001\u5176\u5173\u952e\u6982\u5ff5\u548c\u7ec4\u4ef6\u3001\u4e00\u4e2a\u73b0\u5b9e\u4e16\u754c\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u4f55\u65f6\u4ee5\u53ca\u4e3a\u4f55\u5e94\u8be5\u4f7f\u7528\u5b83\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u7d22\u7b56\u7565\u6a21\u5f0f\u5982\u4f55\u4e0e\u62bd\u8c61\u3001\u679a\u4e3e\u751a\u81f3\u5de5\u5382\u6a21\u5f0f\u4e00\u8d77\u5de5\u4f5c\uff0c\u4ee5\u4f7f\u8bbe\u8ba1\u66f4\u52a0\u5065\u58ee\u548c\u7075\u6d3b\u3002<\/p>\n<hr>\n<h2> <strong>\u4ec0\u4e48\u662f\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1f<\/strong> <\/h2>\n<p><strong>\u7b56\u7565\u6a21\u5f0f<\/strong>\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u7b97\u6cd5\u7684\u884c\u4e3a\u3002\u7b56\u7565\u6a21\u5f0f\u4e0d\u662f\u91c7\u7528\u5355\u4e00\u7684\u6574\u4f53\u7b97\u6cd5\uff0c\u800c\u662f\u5141\u8bb8\u884c\u4e3a\uff08\u6216\u7b56\u7565\uff09\u53ef\u4ee5\u4e92\u6362\uff0c\u8fd9\u4f7f\u5f97\u7cfb\u7edf\u66f4\u52a0\u7075\u6d3b\u4e14\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<h3> <strong>\u6838\u5fc3\u7406\u5ff5\uff1a<\/strong> <\/h3>\n<ul>\n<li>\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff08\u7b56\u7565\uff09\u3002<\/li>\n<li>\u5c06\u6bcf\u4e2a\u7b97\u6cd5\u5c01\u88c5\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u4e2d\u3002<\/li>\n<li>\u4f7f\u7b97\u6cd5\u53ef\u4ee5\u4e92\u6362\u3002<\/li>\n<li>\u8ba9\u5ba2\u6237\u7aef\u9009\u62e9\u5728\u8fd0\u884c\u65f6\u4f7f\u7528\u54ea\u79cd\u7b97\u6cd5\u3002<\/li>\n<\/ul>\n<hr>\n<h2> <strong>\u4f55\u65f6\u4ee5\u53ca\u4e3a\u4f55\u5e94\u8be5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff1f<\/strong> <\/h2>\n<h3> <strong>\u7528\u4f8b<\/strong>\uff1a <\/h3>\n<p>\u7b56\u7565\u6a21\u5f0f\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u7279\u522b\u6709\u7528\uff1a<\/p>\n<ul>\n<li>\u60a8\u6709\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5ba2\u6237\u7aef\u5fc5\u987b\u9009\u62e9\u4e00\u4e2a\u6765\u6267\u884c\u3002<\/li>\n<li>\u60a8\u9700\u8981\u52a8\u6001\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff08\u4f8b\u5982\u6392\u5e8f\u3001\u5b9a\u4ef7\u3001\u4ed8\u6b3e\u5904\u7406\uff09\u3002<\/li>\n<li>\u8be5\u884c\u4e3a\u72ec\u7acb\u4e8e\u5ba2\u6237\u7aef\uff0c\u4f46\u6839\u636e\u4e0a\u4e0b\u6587\u800c\u53d8\u5316\u3002<\/li>\n<li>\u60a8\u5e0c\u671b\u907f\u514d\u51b3\u5b9a\u6267\u884c\u54ea\u79cd\u884c\u4e3a\u7684\u5927\u578b\u6761\u4ef6\u8bed\u53e5\uff08\u4f8b\u5982 if \u6216 \uff09\u3002<\/li>\n<\/ul>\n<h3> <strong>\u4f7f\u7528\u5b83\uff1f<\/strong> <\/h3>\n<ul>\n<li>\n<p><strong>\u5173\u6ce8\u70b9\u5206\u79bb<\/strong>\uff1a\u7b56\u7565\u6a21\u5f0f\u5c06\u7b97\u6cd5\u7684\u5173\u6ce8\u70b9\u4e0e\u7cfb\u7edf\u7684\u5176\u4f59\u90e8\u5206\u5206\u5f00\u3002\u5ba2\u6237\u7aef\u4ee3\u7801\u4e0d\u77e5\u9053\u7b97\u6cd5\u5185\u90e8\u5982\u4f55\u5de5\u4f5c\uff0c\u4f7f\u5176\u66f4\u52a0\u6a21\u5757\u5316\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u53ef\u6269\u5c55\u6027<\/strong>\uff1a\u65e0\u9700\u66f4\u6539\u73b0\u6709\u4ee3\u7801\uff0c\u53ea\u9700\u6dfb\u52a0\u65b0\u7b56\u7565\u7c7b\u5373\u53ef\u6dfb\u52a0\u65b0\u7b97\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u901a\u8fc7\u5c06\u4e0d\u540c\u7684\u884c\u4e3a\u59d4\u6258\u7ed9\u5404\u4e2a\u7b56\u7565\u7c7b\u6765\u964d\u4f4e\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u4ece\u800c\u4f7f\u7ef4\u62a4\u66f4\u5bb9\u6613\u3002<\/p>\n<\/li>\n<\/ul>\n<h3> <strong>\u4ec0\u4e48\u65f6\u5019\u4e0d\u4f7f\u7528\uff1f<\/strong> <\/h3>\n<ul>\n<li>\n<p><strong>\u7b80\u5355\u7b97\u6cd5<\/strong>\uff1a\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u7b97\u6cd5\u5f88\u7b80\u5355\u5e76\u4e14\u4e0d\u4f1a\u6539\u53d8\uff0c\u90a3\u4e48\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u53ef\u80fd\u4f1a\u6709\u70b9\u77eb\u6789\u8fc7\u6b63\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u592a\u591a\u7b56\u7565<\/strong>\uff1a\u5982\u679c\u4f60\u6709\u5927\u91cf\u7b56\u7565\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7c7b\u7206\u70b8\uff0c\u8fd9\u53ef\u80fd\u4f1a\u635f\u5bb3\u53ef\u8bfb\u6027\u5e76\u589e\u52a0\u590d\u6742\u6027\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4e0d\u9891\u7e41\u66f4\u6539<\/strong>\uff1a\u5982\u679c\u7b97\u6cd5\u4e0d\u7ecf\u5e38\u66f4\u6539\uff0c\u5f15\u5165\u7b56\u7565\u6a21\u5f0f\u53ef\u80fd\u4f1a\u5e26\u6765\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/li>\n<\/ul>\n<hr>\n<h2> <strong>\u7b56\u7565\u6a21\u5f0f\u7684\u5173\u952e\u6982\u5ff5\u548c\u7ec4\u6210\u90e8\u5206<\/strong> <\/h2>\n<p>\u7b56\u7565\u6a21\u5f0f\u7531\u4ee5\u4e0b\u5173\u952e\u7ec4\u4ef6\u7ec4\u6210\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4e0a\u4e0b\u6587<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8fd9\u662f\u5c06\u4e0e\u7b56\u7565\u5bf9\u8c61\u4ea4\u4e92\u7684\u7c7b\u3002\u5b83\u901a\u5e38\u5305\u542b\u5bf9\u7b56\u7565\u7684\u5f15\u7528\u5e76\u5c06\u5b9e\u9645\u884c\u4e3a\u59d4\u6258\u7ed9\u8be5\u7b56\u7565\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7b56\u7565<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8fd9\u662f\u4e00\u4e2a\u63a5\u53e3\uff08\u6216\u62bd\u8c61\u7c7b\uff09\uff0c\u58f0\u660e\u4e86\u6267\u884c\u7b97\u6cd5\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u7b56\u7565\u5b9e\u73b0\u6b64\u63a5\u53e3\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5177\u4f53\u7b56\u7565<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8fd9\u4e9b\u662f\u5b9e\u73b0\u7b56\u7565\u63a5\u53e3\u5e76\u5b9a\u4e49\u7279\u5b9a\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u7c7b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr>\n<h2> <strong>\u73b0\u5b9e\u793a\u4f8b\uff1a\u652f\u4ed8\u5904\u7406\u7cfb\u7edf<\/strong> <\/h2>\n<p>\u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u4e0d\u540c\u65b9\u5f0f\u4ed8\u6b3e\u7684\u652f\u4ed8\u5904\u7406\u7cfb\u7edf\uff0c\u4f8b\u5982<strong>\u4fe1\u7528\u5361<\/strong>\u3001<strong>paypal<\/strong>\u548c<strong><\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u7684\u4ed8\u6b3e\u5904\u7406\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u4e0a\u4e0b\u6587\uff08\u672c\u4f8b\u4e2d\u4e3a shoppingcart\uff09\u9700\u8981\u80fd\u591f\u5904\u7406\u4ed8\u6b3e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u6bcf\u79cd\u4ed8\u6b3e\u65b9\u6cd5\u7684\u5177\u4f53\u60c5\u51b5\u3002<\/p>\n<h3> <strong>\u7b2c 1 \u6b65\uff1a\u5b9a\u4e49 paymentmethod \u679a\u4e3e<\/strong> <\/h3>\n<p>\u6211\u4eec\u5c06\u9996\u5148\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u4e0d\u540c\u7684\u4ed8\u6b3e\u65b9\u5f0f\u3002\u8fd9\u4f7f\u5f97\u4ed8\u6b3e\u65b9\u5f0f\u9009\u62e9\u7c7b\u578b\u5b89\u5168\u5e76\u4e14\u66f4\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<pre>public enum paymentmethod {\n    credit_card,\n    paypal,\n    cryptocurrency;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c 2 \u6b65\uff1a\u521b\u5efa paymentinformation \u7c7b<\/strong> <\/h3>\n<p>\u6b64\u7c7b\u5c01\u88c5\u4e86\u5904\u7406\u4ed8\u6b3e\u6240\u9700\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5b83\u5305\u542b\u4ed8\u6b3e\u65b9\u5f0f\u548c\u4ed8\u6b3e\u8be6\u7ec6\u4fe1\u606f\uff08\u4f8b\u5982\u5361\u53f7\u3001\u7535\u5b50\u90ae\u4ef6\u6216\u52a0\u5bc6\u8d27\u5e01\u5730\u5740\uff09\u3002<\/p>\n<pre>public class paymentinformation {\n    private paymentmethod paymentmethod;\n    private string paymentdetails;\n\n    public paymentinformation(paymentmethod paymentmethod, string paymentdetails) {\n        this.paymentmethod = paymentmethod;\n        this.paymentdetails = paymentdetails;\n    }\n\n    public paymentmethod getpaymentmethod() {\n        return paymentmethod;\n    }\n\n    public string getpaymentdetails() {\n        return paymentdetails;\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c 3 \u6b65\uff1a\u5b9a\u4e49 paymentstrategy \u63a5\u53e3<\/strong> <\/h3>\n<p>\u8fd9\u5c06\u662f\u6240\u6709\u652f\u4ed8\u7b56\u7565\u7684\u57fa\u672c\u754c\u9762\u3002\u5b83\u5b9a\u4e49\u4e86\u901a\u7528\u65b9\u6cd5 pay()\uff0c\u6240\u6709\u5177\u4f53\u7b56\u7565\u90fd\u4f1a\u5b9e\u73b0\u8be5\u65b9\u6cd5\u3002<\/p>\n<pre>public abstract class paymentstrategy {\n    protected paymentinformation paymentinformation;\n\n    public paymentstrategy(paymentinformation paymentinformation) {\n        this.paymentinformation = paymentinformation;\n    }\n\n    public abstract void pay(double amount);\n\n    protected boolean validatepaymentdetails() {\n        return paymentinformation != null &amp;&amp; paymentinformation.getpaymentdetails() != null &amp;&amp; !paymentinformation.getpaymentdetails().isempty();\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c\u56db\u6b65\uff1a\u5b9e\u65bd\u5177\u4f53\u7b56\u7565<\/strong> <\/h3>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86 creditcardpayment\u3001paypalpayment \u548c cryptopayment \u7684\u5177\u4f53\u7b56\u7565\u3002\u8fd9\u4e9b\u7c7b\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u6839\u636e\u4ed8\u6b3e\u7c7b\u578b\u5b9e\u73b0 pay() \u65b9\u6cd5\u3002<\/p>\n<h4> <strong>\u4fe1\u7528\u5361\u4ed8\u6b3e\u7b56\u7565<\/strong> <\/h4>\n<pre>public class creditcardpayment extends paymentstrategy {\n    public creditcardpayment(paymentinformation paymentinformation) {\n        super(paymentinformation);\n    }\n\n    @override\n    public void pay(double amount) {\n        if (validatepaymentdetails()) {\n            system.out.println(\"paid \" + amount + \" using credit card: \" + paymentinformation.getpaymentdetails());\n        } else {\n            system.out.println(\"invalid credit card details.\");\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> <strong>paypal \u4ed8\u6b3e\u7b56\u7565<\/strong> <\/h4>\n<pre>public class paypalpayment extends paymentstrategy {\n    public paypalpayment(paymentinformation paymentinformation) {\n        super(paymentinformation);\n    }\n\n    @override\n    public void pay(double amount) {\n        if (validatepaymentdetails()) {\n            system.out.println(\"paid \" + amount + \" using paypal: \" + paymentinformation.getpaymentdetails());\n        } else {\n            system.out.println(\"invalid paypal details.\");\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h4> <strong>\u52a0\u5bc6\u8d27\u5e01\u652f\u4ed8\u7b56\u7565<\/strong> <\/h4>\n<pre>public class cryptopayment extends paymentstrategy {\n    public cryptopayment(paymentinformation paymentinformation) {\n        super(paymentinformation);\n    }\n\n    @override\n    public void pay(double amount) {\n        if (validatepaymentdetails()) {\n            system.out.println(\"paid \" + amount + \" using cryptocurrency to address: \" + paymentinformation.getpaymentdetails());\n        } else {\n            system.out.println(\"invalid cryptocurrency address.\");\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c5\u6b65\uff1a\u5de5\u5382\u9009\u62e9\u7b56\u7565<\/strong> <\/h3>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528<strong>\u5de5\u5382\u6a21\u5f0f<\/strong>\u6839\u636e\u4ed8\u6b3e\u65b9\u5f0f\u5b9e\u4f8b\u5316\u9002\u5f53\u7684\u4ed8\u6b3e\u7b56\u7565\u3002\u8fd9\u4f7f\u5f97\u7cfb\u7edf\u66f4\u52a0\u7075\u6d3b\uff0c\u5e76\u5141\u8bb8\u5ba2\u6237\u7aef\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u4ed8\u6b3e\u65b9\u5f0f\u3002<\/p>\n<pre>public class paymentstrategyfactory {\n    public static paymentstrategy createpaymentstrategy(paymentinformation paymentinformation) {\n        switch (paymentinformation.getpaymentmethod()) {\n            case credit_card:\n                return new creditcardpayment(paymentinformation);\n            case paypal:\n                return new paypalpayment(paymentinformation);\n            case cryptocurrency:\n                return new cryptopayment(paymentinformation);\n            default:\n                throw new illegalargumentexception(\"unsupported payment method: \" + paymentinformation.getpaymentmethod());\n        }\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c 6 \u6b65\uff1a\u5ba2\u6237\u7aef\u4ee3\u7801\uff08\u8d2d\u7269\u8f66\uff09<\/strong> <\/h3>\n<p>shoppingcart \u7c7b\u662f\u4f7f\u7528\u652f\u4ed8\u7b56\u7565\u7684\u4e0a\u4e0b\u6587\u3002\u5b83\u5c06\u652f\u4ed8\u8d23\u4efb\u59d4\u6258\u7ed9\u5de5\u5382\u9009\u62e9\u7684\u7b56\u7565\u3002<\/p>\n<pre>public class shoppingcart {\n    private paymentstrategy paymentstrategy;\n\n    public shoppingcart(paymentinformation paymentinformation) {\n        this.paymentstrategy = paymentstrategyfactory.createpaymentstrategy(paymentinformation);\n    }\n\n    public void checkout(double amount) {\n        paymentstrategy.pay(amount);\n    }\n\n    public void setpaymentinformation(paymentinformation paymentinformation) {\n        this.paymentstrategy = paymentstrategyfactory.createpaymentstrategy(paymentinformation);\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> <strong>\u7b2c 7 \u6b65\uff1a\u8fd0\u884c\u793a\u4f8b<\/strong> <\/h3>\n<pre>public class main {\n    public static void main(string[] args) {\n        paymentinformation cardinfo = new paymentinformation(paymentmethod.credit_card, \"1234-5678-9876\");\n        shoppingcart cart = new shoppingcart(cardinfo);\n        cart.checkout(250.0);\n\n        paymentinformation paypalinfo = new paymentinformation(paymentmethod.paypal, \"john.doe@example.com\");\n        cart.setpaymentinformation(paypalinfo);\n        cart.checkout(150.0);\n\n        paymentinformation cryptoinfo = new paymentinformation(paymentmethod.cryptocurrency, \"1a2b3c4d5e6f\");\n        cart.setpaymentinformation(cryptoinfo);\n        cart.checkout(500.0);\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> <strong>\u8f93\u51fa<\/strong>\uff1a <\/h3>\n<pre>Paid 250.0 using Credit Card: 1234-5678-9876\nPaid 150.0 using PayPal: john.doe@example.com\nPaid 500.0 using Cryptocurrency to address: 1A2B3C4D5E6F\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2> <strong>\u7b56\u7565\u6a21\u5f0f\u7684\u597d\u5904<\/strong> <\/h2>\n<ul>\n<li> <strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8f7b\u677e\u4ea4\u6362\u7b56\u7565\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001<\/li>\n<\/ul>\n<p>\u884c\u4e3a\u6539\u53d8\u800c\u4e0d\u4fee\u6539\u6838\u5fc3\u903b\u8f91\u3002<\/p>\n<ul>\n<li> <strong>\u53ef\u6269\u5c55\u6027<\/strong>\uff1a\u6dfb\u52a0\u65b0\u7b56\u7565\u4e0d\u9700\u8981\u4fee\u6539\u73b0\u6709\u4ee3\u7801\uff1b\u60a8\u53ea\u9700\u521b\u5efa\u65b0\u7684\u7b56\u7565\u7c7b\u3002<\/li>\n<li> <strong>\u5173\u6ce8\u70b9\u5206\u79bb<\/strong>\uff1a\u7b56\u7565\u5c01\u88c5\u4e86\u7b97\u6cd5\uff0c\u56e0\u6b64\u4e0a\u4e0b\u6587\u7c7b\uff08\u4f8b\u5982 shoppingcart\uff09\u4e0d\u77e5\u9053\u4ed8\u6b3e\u662f\u5982\u4f55\u5904\u7406\u7684\u3002<\/li>\n<li> <strong>\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u4ee3\u7801\u66f4\u5e72\u51c0\u4e14\u66f4\u6613\u4e8e\u7ef4\u62a4\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7b56\u7565\u7684\u903b\u8f91\u90fd\u9694\u79bb\u5728\u81ea\u5df1\u7684\u7c7b\u4e2d\u3002<\/li>\n<\/ul>\n<hr>\n<h2> <strong>\u7b56\u7565\u6a21\u5f0f\u7684\u7f3a\u70b9<\/strong> <\/h2>\n<ul>\n<li> <strong>\u590d\u6742\u6027<\/strong>\uff1a\u5f15\u5165\u591a\u79cd\u7b56\u7565\u4f1a\u589e\u52a0\u7cfb\u7edf\u4e2d\u7c7b\u7684\u6570\u91cf\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5bfc\u822a\u53d8\u5f97\u66f4\u52a0\u56f0\u96be\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u7b80\u5355\u7684\u7528\u4f8b\u3002<\/li>\n<li> <strong>\u5f00\u9500<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u7b56\u7565\u6570\u91cf\u5f88\u5c11\uff0c\u4f7f\u7528\u6b64\u6a21\u5f0f\u53ef\u80fd\u4f1a\u5f15\u5165\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61\u548c\u5f00\u9500\u3002<\/li>\n<li> <strong>\u4f9d\u8d56\u7ba1\u7406<\/strong>\uff1a\u7ba1\u7406\u7b56\u7565\u53ca\u5176\u521d\u59cb\u5316\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u5f00\u9500\uff0c\u7279\u522b\u662f\u5f53\u7b56\u7565\u4f9d\u8d56\u4e8e\u5916\u90e8\u8d44\u6e90\u65f6\u3002<\/li>\n<\/ul>\n<hr>\n<h2> <strong>\u7ed3\u8bba<\/strong> <\/h2>\n<p><strong>\u7b56\u7565\u6a21\u5f0f<\/strong>\u662f\u5b9e\u73b0\u7cfb\u7edf\u7075\u6d3b\u6027\u548c\u6a21\u5757\u5316\u7684\u57fa\u672c\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u5c01\u88c5\u7b97\u6cd5\uff0c\u5e76\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u8fd0\u884c\u65f6\u7684\u7075\u6d3b\u6027\u3002\u65e0\u8bba\u60a8\u662f\u6784\u5efa\u652f\u4ed8\u5904\u7406\u7cfb\u7edf\u3001\u6392\u5e8f\u7b97\u6cd5\u5e93\uff0c\u751a\u81f3\u662f\u6e38\u620f  \u5f15\u64ce\uff0c\u7b56\u7565\u6a21\u5f0f\u90fd\u53ef\u4ee5\u5e2e\u52a9\u60a8\u7684\u4ee3\u7801\u66f4\u5177\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6269\u5c55\u6027\uff0c\u5e76\u4e14\u968f\u7740\u9700\u6c42\u7684\u53d1\u5c55\u66f4\u5bb9\u6613\u4fee\u6539\u3002<\/p>\n<p>\u901a\u8fc7\u5229\u7528\u62bd\u8c61\u3001\u679a\u4e3e\u548c\u5de5\u5382\u6a21\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6784\u5efa\u66f4\u5f3a\u5927\u7684\u7cfb\u7edf\uff0c\u65e2\u7c7b\u578b\u5b89\u5168\u53c8\u7075\u6d3b\u3002<\/p>\n<hr>\n<h3> <strong>\u5ef6\u4f38\u9605\u8bfb<\/strong> <\/h3>\n<ol>\n<li> <em>\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u53ef\u91cd\u7528\u9762\u5411\u5bf9\u8c61\u8f6f\u4ef6\u7684\u5143\u7d20<\/em>\u4f5c\u8005\uff1aerich gamma\u3001richard helm\u3001ralph johnson\u3001john vlissides \u2013 \u8fd9\u662f\u4e00\u672c\u5f00\u521b\u6027\u7684\u4e66\uff0c\u4ecb\u7ecd\u4e86\u8bb8\u591a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5305\u62ec\u7b56\u7565\u6a21\u5f0f\u3002<\/li>\n<li> <em>head first \u8bbe\u8ba1\u6a21\u5f0f<\/em> \u4f5c\u8005\uff1aeric freeman\u3001elisabeth robson \u2013 \u901a\u8fc7\u5b9e\u9645\u793a\u4f8b\u5bf9\u8bbe\u8ba1\u6a21\u5f0f\u8fdb\u884c\u5e73\u6613\u8fd1\u4eba\u7684\u4ecb\u7ecd\u3002<\/li>\n<li> <em>\u91cd\u6784\uff1a\u6539\u8fdb\u73b0\u6709\u4ee3\u7801\u7684\u8bbe\u8ba1<\/em>\uff0c\u4f5c\u8005\uff1amartin fowler \u2013 \u63a2\u7d22\u8bbe\u8ba1\u6a21\u5f0f\u5728\u4e2d\u7684\u4ef7\u503c\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u638c\u63e1\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1a\u5f00\u53d1\u4eba\u5458\u6307\u5357\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u4e3a\u8f6f\u4ef6\u5de5\u7a0b\u5e08\uff0c\u6211\u4eec\u4e0d\u65ad\u5730\u627f\u62c5\u7740\u521b\u5efa\u53ef\u7ef4\u62a4\u3001\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u7cfb\u7edf\u7684\u4efb\u52a1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4ee5\u7ed3\u6784\u5316\u548c\u53ef\u91cd\u7528\u7684\u65b9\u5f0f\u89e3\u51b3\u91cd\u590d\u51fa\u73b0\u7684\u95ee\u9898\u3002\u5176\u4e2d\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u662f\u7b56\u7565\u6a21\u5f0f\uff0c\u5b83\u662f\u884c\u4e3a\u6a21\u5f0f\u5bb6\u65cf\u7684\u4e00\u90e8\u5206\u3002 \u7b56\u7565\u6a21\u5f0f\u5141\u8bb8\u60a8\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5c01\u88c5\u6bcf\u4e2a\u7b97\u6cd5\uff0c\u5e76\u4f7f\u5b83\u4eec\u53ef\u4ee5\u4e92\u6362\u3002\u8fd9\u610f\u5473\u7740\u5ba2\u6237\u7aef\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u6216\u7b56\u7565\uff0c\u800c\u65e0\u9700\u6539\u53d8\u7cfb\u7edf\u7684\u6838\u5fc3\u529f\u80fd\u3002 \u5728\u8fd9\u7bc7\u535a\u5ba2\u4e2d\uff0c\u6211\u5c06\u6df1\u5165\u63a2\u8ba8\u7b56\u7565\u6a21\u5f0f\u3001\u5176\u5173\u952e\u6982\u5ff5\u548c\u7ec4\u4ef6\u3001\u4e00\u4e2a\u73b0\u5b9e\u4e16\u754c\u7684\u793a\u4f8b\uff0c\u4ee5\u53ca\u4f55\u65f6\u4ee5\u53ca\u4e3a\u4f55\u5e94\u8be5\u4f7f\u7528\u5b83\u3002\u6211\u4eec\u8fd8\u5c06\u63a2\u7d22\u7b56\u7565\u6a21\u5f0f\u5982\u4f55\u4e0e\u62bd\u8c61\u3001\u679a\u4e3e\u751a\u81f3\u5de5\u5382\u6a21\u5f0f\u4e00\u8d77\u5de5\u4f5c\uff0c\u4ee5\u4f7f\u8bbe\u8ba1\u66f4\u52a0\u5065\u58ee\u548c\u7075\u6d3b\u3002 \u4ec0\u4e48\u662f\u7b56\u7565\u8bbe\u8ba1\u6a21\u5f0f\uff1f \u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u9009\u62e9\u7b97\u6cd5\u7684\u884c\u4e3a\u3002\u7b56\u7565\u6a21\u5f0f\u4e0d\u662f\u91c7\u7528\u5355\u4e00\u7684\u6574\u4f53\u7b97\u6cd5\uff0c\u800c\u662f\u5141\u8bb8\u884c\u4e3a\uff08\u6216\u7b56\u7565\uff09\u53ef\u4ee5\u4e92\u6362\uff0c\u8fd9\u4f7f\u5f97\u7cfb\u7edf\u66f4\u52a0\u7075\u6d3b\u4e14\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002 \u6838\u5fc3\u7406\u5ff5\uff1a \u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff08\u7b56\u7565\uff09\u3002 \u5c06\u6bcf\u4e2a\u7b97\u6cd5\u5c01\u88c5\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u4e2d\u3002 \u4f7f\u7b97\u6cd5\u53ef\u4ee5\u4e92\u6362\u3002 \u8ba9\u5ba2\u6237\u7aef\u9009\u62e9\u5728\u8fd0\u884c\u65f6\u4f7f\u7528\u54ea\u79cd\u7b97\u6cd5\u3002 \u4f55\u65f6\u4ee5\u53ca\u4e3a\u4f55\u5e94\u8be5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff1f \u7528\u4f8b\uff1a \u7b56\u7565\u6a21\u5f0f\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u7279\u522b\u6709\u7528\uff1a \u60a8\u6709\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5ba2\u6237\u7aef\u5fc5\u987b\u9009\u62e9\u4e00\u4e2a\u6765\u6267\u884c\u3002 \u60a8\u9700\u8981\u52a8\u6001\u9009\u62e9\u4e0d\u540c\u7684\u884c\u4e3a\uff08\u4f8b\u5982\u6392\u5e8f\u3001\u5b9a\u4ef7\u3001\u4ed8\u6b3e\u5904\u7406\uff09\u3002 \u8be5\u884c\u4e3a\u72ec\u7acb\u4e8e\u5ba2\u6237\u7aef\uff0c\u4f46\u6839\u636e\u4e0a\u4e0b\u6587\u800c\u53d8\u5316\u3002 \u60a8\u5e0c\u671b\u907f\u514d\u51b3\u5b9a\u6267\u884c\u54ea\u79cd\u884c\u4e3a\u7684\u5927\u578b\u6761\u4ef6\u8bed\u53e5\uff08\u4f8b\u5982 if \u6216 \uff09\u3002 \u4f7f\u7528\u5b83\uff1f \u5173\u6ce8\u70b9\u5206\u79bb\uff1a\u7b56\u7565\u6a21\u5f0f\u5c06\u7b97\u6cd5\u7684\u5173\u6ce8\u70b9\u4e0e\u7cfb\u7edf\u7684\u5176\u4f59\u90e8\u5206\u5206\u5f00\u3002\u5ba2\u6237\u7aef\u4ee3\u7801\u4e0d\u77e5\u9053\u7b97\u6cd5\u5185\u90e8\u5982\u4f55\u5de5\u4f5c\uff0c\u4f7f\u5176\u66f4\u52a0\u6a21\u5757\u5316\u3002 \u53ef\u6269\u5c55\u6027\uff1a\u65e0\u9700\u66f4\u6539\u73b0\u6709\u4ee3\u7801\uff0c\u53ea\u9700\u6dfb\u52a0\u65b0\u7b56\u7565\u7c7b\u5373\u53ef\u6dfb\u52a0\u65b0\u7b97\u6cd5\u3002 \u53ef\u7ef4\u62a4\u6027\uff1a\u901a\u8fc7\u5c06\u4e0d\u540c\u7684\u884c\u4e3a\u59d4\u6258\u7ed9\u5404\u4e2a\u7b56\u7565\u7c7b\u6765\u964d\u4f4e\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u4ece\u800c\u4f7f\u7ef4\u62a4\u66f4\u5bb9\u6613\u3002 \u4ec0\u4e48\u65f6\u5019\u4e0d\u4f7f\u7528\uff1f \u7b80\u5355\u7b97\u6cd5\uff1a\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u7b97\u6cd5\u5f88\u7b80\u5355\u5e76\u4e14\u4e0d\u4f1a\u6539\u53d8\uff0c\u90a3\u4e48\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u53ef\u80fd\u4f1a\u6709\u70b9\u77eb\u6789\u8fc7\u6b63\u3002 \u592a\u591a\u7b56\u7565\uff1a\u5982\u679c\u4f60\u6709\u5927\u91cf\u7b56\u7565\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7c7b\u7206\u70b8\uff0c\u8fd9\u53ef\u80fd\u4f1a\u635f\u5bb3\u53ef\u8bfb\u6027\u5e76\u589e\u52a0\u590d\u6742\u6027\u3002 \u4e0d\u9891\u7e41\u66f4\u6539\uff1a\u5982\u679c\u7b97\u6cd5\u4e0d\u7ecf\u5e38\u66f4\u6539\uff0c\u5f15\u5165\u7b56\u7565\u6a21\u5f0f\u53ef\u80fd\u4f1a\u5e26\u6765\u4e0d\u5fc5\u8981\u7684\u590d\u6742\u6027\u3002 \u7b56\u7565\u6a21\u5f0f\u7684\u5173\u952e\u6982\u5ff5\u548c\u7ec4\u6210\u90e8\u5206 \u7b56\u7565\u6a21\u5f0f\u7531\u4ee5\u4e0b\u5173\u952e\u7ec4\u4ef6\u7ec4\u6210\uff1a \u4e0a\u4e0b\u6587\uff1a \u8fd9\u662f\u5c06\u4e0e\u7b56\u7565\u5bf9\u8c61\u4ea4\u4e92\u7684\u7c7b\u3002\u5b83\u901a\u5e38\u5305\u542b\u5bf9\u7b56\u7565\u7684\u5f15\u7528\u5e76\u5c06\u5b9e\u9645\u884c\u4e3a\u59d4\u6258\u7ed9\u8be5\u7b56\u7565\u3002 \u7b56\u7565\uff1a \u8fd9\u662f\u4e00\u4e2a\u63a5\u53e3\uff08\u6216\u62bd\u8c61\u7c7b\uff09\uff0c\u58f0\u660e\u4e86\u6267\u884c\u7b97\u6cd5\u7684\u65b9\u6cd5\u3002\u5177\u4f53\u7b56\u7565\u5b9e\u73b0\u6b64\u63a5\u53e3\u4ee5\u63d0\u4f9b\u4e0d\u540c\u7684\u884c\u4e3a\u3002 \u5177\u4f53\u7b56\u7565\uff1a \u8fd9\u4e9b\u662f\u5b9e\u73b0\u7b56\u7565\u63a5\u53e3\u5e76\u5b9a\u4e49\u7279\u5b9a\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u7c7b\u3002 \u73b0\u5b9e\u793a\u4f8b\uff1a\u652f\u4ed8\u5904\u7406\u7cfb\u7edf \u8ba9\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u4e0d\u540c\u65b9\u5f0f\u4ed8\u6b3e\u7684\u652f\u4ed8\u5904\u7406\u7cfb\u7edf\uff0c\u4f8b\u5982\u4fe1\u7528\u5361\u3001paypal\u548c\u3002\u6bcf\u79cd\u65b9\u6cd5\u7684\u4ed8\u6b3e\u5904\u7406\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u4e0a\u4e0b\u6587\uff08\u672c\u4f8b\u4e2d\u4e3a shoppingcart\uff09\u9700\u8981\u80fd\u591f\u5904\u7406\u4ed8\u6b3e\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u6bcf\u79cd\u4ed8\u6b3e\u65b9\u6cd5\u7684\u5177\u4f53\u60c5\u51b5\u3002 \u7b2c 1 \u6b65\uff1a\u5b9a\u4e49 paymentmethod \u679a\u4e3e \u6211\u4eec\u5c06\u9996\u5148\u4f7f\u7528\u679a\u4e3e\u6765\u5b9a\u4e49\u4e0d\u540c\u7684\u4ed8\u6b3e\u65b9\u5f0f\u3002\u8fd9\u4f7f\u5f97\u4ed8\u6b3e\u65b9\u5f0f\u9009\u62e9\u7c7b\u578b\u5b89\u5168\u5e76\u4e14\u66f4\u6613\u4e8e\u7ba1\u7406\u3002 public enum paymentmethod { credit_card, paypal, cryptocurrency; } \u767b\u5f55\u540e\u590d\u5236 \u7b2c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38029","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38029"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38029\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}