{"id":602,"date":"2015-04-30T19:21:52","date_gmt":"2015-04-30T11:21:52","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=602"},"modified":"2026-08-18T12:09:32","modified_gmt":"2026-08-18T04:09:32","slug":"%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b9%8bdecorator%e6%b2%b9%e6%bc%86%e5%b7%a5","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=602","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\u4e4bDecorator(\u6cb9\u6f06\u5de5)"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u88c5\u9970\u6a21\u5f0f Decorator \u5e38\u88ab\u8bd1\u6210\u201c\u88c5\u9970\u201d\uff0c\u8bd1\u6210<strong style=\"color: #4870ac;\">\u6cb9\u6f06\u5de5<\/strong>\u66f4\u5f62\u8c61\uff1a\u6cb9\u6f06\u5de5\uff08decorator\uff09\u5237\u6cb9\u6f06\uff0c\u88ab\u5237\u7684\u5bf9\u8c61\u53eb decoratee\u3002\u8fd9\u4e24\u79cd\u89d2\u8272\u5728\u6a21\u5f0f\u91cc\u7f3a\u4e00\u4e0d\u53ef\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5b9a\u4e49\uff1a\u52a8\u6001\u5730\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u52a0\u4e0a\u989d\u5916\u804c\u8d23\uff0c\u5c31\u50cf\u5728\u5899\u4e0a\u5237\u6cb9\u6f06\u3002\u6bd4\u8d77\u9760\u751f\u6210\u5b50\u7c7b\u6765\u6269\u5145\u529f\u80fd\uff0cDecorator \u66f4\u7075\u6d3b\u2014\u2014\u529f\u80fd\u7684\u79cd\u7c7b\u3001\u65f6\u673a\u90fd\u53ef\u4ee5\u5728\u8fd0\u884c\u671f\u7531\u7528\u6237\u51b3\u5b9a\u3002<\/p>\n<p><!--more--><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u4e3a\u4ec0\u4e48\u4e0d\u7528\u7ee7\u627f\uff1f<\/h2>\n<p style=\"margin: 1em 0px;\">\u7ee7\u627f\u4e5f\u80fd\u62d3\u5c55\u529f\u80fd\uff0c\u4f46\u79cd\u7c7b\u4e00\u591a\u5c31\u4f1a\u6d3e\u751f\u51fa\u5927\u91cf\u5b50\u7c7b\uff0c\u7cfb\u7edf\u53d8\u590d\u6742\uff1b\u800c\u4e14\u8fd9\u4e9b\u529f\u80fd\u5fc5\u987b\u5728\u7f16\u8bd1\u671f\u5c31\u9884\u89c1\uff0c\u662f\u9759\u6001\u7684\u3002<\/p>\n<p style=\"margin: 1em 0px;\">Decorator \u7684\u7406\u7531\u662f\uff1a\u52a0\u5165\u65b9\u5f0f\u548c\u65f6\u673a\u8981\u7531\u7528\u6237\u52a8\u6001\u51b3\u5b9a\uff0c\u63d0\u4f9b\u201c\u5373\u63d2\u5373\u7528\u201d\u3002<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u89d2\u8272<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u542b\u4e49<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u672c\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">decoratee<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u88ab\u5237\u6cb9\u6f06\u7684\u5bf9\u8c61<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">SquarePeg<\/code>\uff08\u65b9\u5f62\u6869\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Decorator<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5f80\u5916\u518d\u5305\u4e00\u5c42\u804c\u8d23<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Decorator<\/code>\uff08\u6316\u5751\u3001\u9489\u6728\u677f\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5171\u540c\u63a5\u53e3<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8c03\u7528\u65b9\u53ea\u8ba4\u63a5\u53e3<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Work.insert()<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u6253\u6869\u4f8b\u5b50<\/h2>\n<p style=\"margin: 1em 0px;\">Adapter \u6587\u91cc\u6709\u65b9\u5f62\u6869\u3001\u5706\u5f62\u6869\u3002Decorator \u4e0d\u5173\u5fc3\u600e\u4e48\u7ec4\u5408\u8fd9\u4e24\u4e2a\u65e0\u5173\u7c7b\uff0c\u53ea\u5173\u5fc3\u6253\u6869\u65f6\u989d\u5916\u505a\u7684\u4e8b\uff1a\u6316\u5751\u3001\u5728\u6869\u4e0a\u9489\u6728\u677f\uff0c\u800c\u4e14\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u80fd\u968f\u65f6\u589e\u5220\u6539\uff08\u6bd4\u5982\u6253\u5b8c\u518d\u9489\u67b6\u5b50\uff09\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5148\u5b9a\u63a5\u53e3\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public interface Work {\n    public void insert();\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u2014\u2014\u63d2\u5165\u65b9\u5f62\u6869\uff08\u5706\u5f62\u6869\u5bf9 Decorator \u65e0\u6240\u8c13\uff0c\u8fd9\u91cc\u53ea\u793a\u8303\u65b9\u5f62\uff09\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class SquarePeg implements Work {\n    public void insert() {\n        System.out.println(\"\u65b9\u5f62\u6869\u63d2\u5165\");\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u65b9\u5f62\u6869\u662f decoratee\u3002\u628a\u989d\u5916\u529f\u80fd\u5305\u8fdb Decorator\uff0c\u7528\u7ec4\u5408\u5f15\u5165 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">Work<\/code>\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class Decorator implements Work {\n\n    private Work work;\n    \/\/ \u989d\u5916\u529f\u80fd\u6253\u5305\u5728\u8fd9\u4e2a List \u91cc\n    private ArrayList others = new ArrayList();\n\n    public Decorator(Work work) {\n        this.work = work;\n        others.add(\"\u6316\u5751\");\n        others.add(\"\u9489\u6728\u677f\");\n    }\n\n    public void insert() {\n        newMethod();\n    }\n\n    \/\/ insert \u524d\u540e\u52a0\u4ec0\u4e48\u3001\u4ec0\u4e48\u6b21\u5e8f\uff0c\u7531\u7528\u6237\u7075\u6d3b\u6307\u5b9a\n    public void newMethod() {\n        otherMethod();\n        work.insert();\n    }\n\n    public void otherMethod() {\n        ListIterator listIterator = others.listIterator();\n        while (listIterator.hasNext()) {\n            System.out.println(((String) (listIterator.next())) + \" \u6b63\u5728\u8fdb\u884c\");\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u4e0a\u4f8b\u628a\u6316\u5751\u3001\u9489\u6728\u677f\u90fd\u6392\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">insert<\/code> \u524d\u9762\uff0c\u53ea\u662f\u4e3a\u4e86\u8bf4\u660e\u6b21\u5e8f\u53ef\u4ee5\u4efb\u610f\u5b89\u6392\u3002\u8c03\u7528\u65b9\u5f0f\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Work squarePeg = new SquarePeg();\nWork decorator = new Decorator(squarePeg);\ndecorator.insert();<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u548c Java I\/O \u662f\u540c\u4e00\u5957\u8def<\/h2>\n<p style=\"margin: 1em 0px;\">\u8bfb\u6587\u4ef6\u65f6\u5e38\u89c1\u7684\u5305\u4e00\u5c42\uff0c\u5176\u5b9e\u5c31\u662f Decorator\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">FileReader fr = new FileReader(filename);\nBufferedReader br = new BufferedReader(fr);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">Java I\/O API \u6b63\u662f\u7528 Decorator \u62fc\u51fa\u6765\u7684\u3002I\/O \u53d8\u79cd\u6781\u591a\uff0c\u82e5\u5168\u9760\u7ee7\u627f\u4f1a\u70b8\u51fa\u4e00\u5927\u7247\u5b50\u7c7b\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001Jive \u91cc\u7684\u8fc7\u6ee4<\/h2>\n<p style=\"margin: 1em 0px;\">\u8bba\u575b\u91cc\u6709\u4e9b\u8bcd\u4e0d\u80fd\u51fa\u73b0\uff08\u5982\u201c\u6253\u5012 XXX\u201d\uff09\uff0c\u9700\u8981\u8fc7\u6ee4\u6216\u9ad8\u4eae\u3002IBM Java \u4e13\u680f\u8c08 Jive \u65f6\u63d0\u5230 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ForumMessageFilter<\/code> \u4f7f\u7528\u4e86 Decorator\u2014\u2014\u4e25\u683c\u8bf4\u90a3\u4efd\u7a0b\u5e8f\u8fd8\u6ca1\u6709\u771f\u6b63\u843d\u5730\u6210 Decorator\uff0c\u53ea\u662f\u63d0\u793a\uff1a\u9488\u5bf9\u7279\u522b\u8bba\u575b\u53ef\u4ee5\u518d\u52a0\u8fc7\u6ee4\uff0c\u5c31\u53ef\u4ee5\u628a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">ForumMessageFilter<\/code> \u91cd\u7ec4\u4e3a\u8be5\u6a21\u5f0f\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u5224\u65ad\u662f\u4e0d\u662f Decorator\uff0c\u8981\u6293\u4f4f\u5b9a\u4e49\u548c\u4e24\u4e2a\u89d2\u8272\uff1a<strong style=\"color: #4870ac;\">Decoratee<\/strong> \u4e0e <strong style=\"color: #4870ac;\">Decorator<\/strong>\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u529f\u80fd\u8981\u8fd0\u884c\u671f\u518d\u53e0\u4e00\u5c42\uff0c\u5c31\u522b\u72c2\u5efa\u5b50\u7c7b\uff1a\u7528\u5171\u540c\u63a5\u53e3\u5305\u4f4f decoratee\uff0c\u6cb9\u6f06\u5de5\u6309\u9700\u5237\u4e0a\u53bb\u2014\u2014Java I\/O \u5c31\u662f\u8fd9\u6837\u5e72\u7684\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u88c5\u9970\u6a21\u5f0f Decorator \u5e38\u88ab\u8bd1\u6210\u201c\u88c5\u9970\u201d\uff0c\u8bd1\u6210\u6cb9\u6f06\u5de5\u66f4\u5f62\u8c61\uff1a\u6cb9\u6f06\u5de5\uff08decorator\uff09\u5237\u6cb9\u6f06\uff0c\u88ab\u5237\u7684\u5bf9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[58],"tags":[43,179],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/602"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=602"}],"version-history":[{"count":2,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}