{"id":1113,"date":"2023-03-12T19:59:30","date_gmt":"2023-03-12T11:59:30","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1113"},"modified":"2023-04-29T11:22:59","modified_gmt":"2023-04-29T03:22:59","slug":"design-pattern-prototype-pattern","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/design-pattern-prototype-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\uff084\uff09\u539f\u578b\u6a21\u5f0f"},"content":{"rendered":"<h2>\u5b9a\u4e49<\/h2>\n<p>\u7528\u539f\u578b\u5b9e\u4f8b\u6307\u5b9a\u521b\u5efa\u5bf9\u8c61\u7684\u79cd\u7c7b\uff0c\u5e76\u901a\u8fc7\u62f7\u8d1d\u8fd9\u4e9b\u539f\u578b\u521b\u5efa\u65b0\u7684\u5bf9\u8c61\u3002\u88ab\u590d\u5236\u7684\u5b9e\u4f8b\u5c31\u662f\u201c\u539f\u578b\u201d\uff0c\u8fd9\u4e2a\u539f\u578b\u662f\u53ef\u5b9a\u5236\u7684\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<ul>\n<li>\u7c7b\u521d\u59cb\u5316\u9700\u8981\u6d88\u5316\u975e\u5e38\u591a\u7684\u8d44\u6e90\uff0c\u8fd9\u4e2a\u8d44\u6e90\u5305\u62ec\u6570\u636e\u3001\u786c\u4ef6\u8d44\u6e90\u7b49\uff0c\u901a\u8fc7\u539f\u578b\u62f7\u8d1d\u907f\u514d\u8fd9\u4e9b\u6d88\u8017\u3002 <\/li>\n<li>\u901a\u8fc7new\u4ea7\u751f\u7684\u4e00\u4e2a\u5bf9\u8c61\u9700\u8981\u975e\u5e38\u7e41\u7410\u7684\u6570\u636e\u51c6\u5907\u6216\u8005\u6743\u9650\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u539f\u578b\u6a21\u5f0f\u3002 <\/li>\n<li>\u4e00\u4e2a\u5bf9\u8c61\u9700\u8981\u63d0\u4f9b\u7ed9\u5176\u4ed6\u5bf9\u8c61\u8bbf\u95ee\uff0c\u800c\u4e14\u5404\u4e2a\u8c03\u7528\u8005\u53ef\u80fd\u90fd\u9700\u8981\u4fee\u6539\u5176\u503c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u539f\u578b\u6a21\u5f0f\u62f7\u8d1d\u591a\u4e2a\u5bf9\u8c61\u4f9b\u8c03\u7528\u8005\u4f7f\u7528\uff0c\u5373\u4fdd\u62a4\u6027\u62f7\u8d1d\u3002<\/li>\n<\/ul>\n<h2>\u7b80\u5355\u5b9e\u73b0<\/h2>\n<pre><code class=\"language-java\">public class Book implements Cloneable {\n    private int price;\n    private String title;\n    private String content;\n    private ArrayList&lt;String&gt; image = new ArrayList&lt;String&gt;();\n\n    public Book() {\n        super();\n    }\n\n    public Book(int price, String title, String content) {\n        super();\n        this.price = price;\n        this.title = title;\n        this.content = content;\n    }\n\n    public ArrayList&lt;String&gt; getImage() {\n        return image;\n    }\n\n    public void addImage(String img) {\n        this.image.add(img);\n    }\n\n    public int getPrice() {\n        return price;\n    }\n\n    public void setPrice(int price) {\n        this.price = price;\n    }\n\n    public String getTitle() {\n        return title;\n    }\n\n    public void setTitle(String title) {\n        this.title = title;\n    }\n\n    public String getContent() {\n        return content;\n    }\n\n    public void setContent(String content) {\n        this.content = content;\n    }\n\n    @Override\n    protected Object clone() throws CloneNotSupportedException {\n        Book book = (Book) super.clone();\n        return book;\n    }\n\n    public void showBook() {\n        System.out.println(&quot;=====Start=====&quot;);\n\n        System.out.println(&quot;title: &quot;+title);\n        for (String img : image) {\n            System.out.println(&quot;image name: &quot; + img);\n        }\n\n        System.out.println(&quot;======End======&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6267\u884cmain\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) {\n    try {\n        Book book1 = new Book(50, &quot;\u4e661&quot;, &quot;\u5185\u5bb9&quot;);\n        book1.addImage(&quot;\u56fe1&quot;);\n        book1.showBook();\n\n        Book book2 = (Book) book1.clone();\n        book2.showBook();\n\n        book2.setTitle(&quot;\u4e662&quot;);\n        book2.addImage(&quot;\u56fe2&quot;);\n        book2.showBook();\n\n        book1.showBook();\n    } catch (CloneNotSupportedException e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6211\u4eec\u53d1\u73b0\u901a\u8fc7\u4fee\u6539book2\u540e\uff0c\u53ea\u662f\u5f71\u54cd\u4e86book1\u7684image\uff0c\u800c\u6ca1\u6709\u6539\u53d8title\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u8fd9\u4e2a\u6211\u4eec\u6682\u65f6\u653e\u4e0b\uff0c\u5148\u770b\u770b\u53d8\u5316\u7684image\uff0c\u5f88\u660e\u663ebook1\u4e0ebook2\u662f\u5f15\u7528\u4e86\u540c\u4e00\u5730\u5740\uff0c\u6240\u4ee5\u4e00\u65b9\u4fee\u6539\u4e24\u8fb9\u90fd\u6539\u53d8\u3002\u90a3\u4e48\u600e\u4e48\u89e3\u51b3\uff1f\u8986\u76d6Object\u4e2d\u7684clone\u65b9\u6cd5\uff0c\u5b9e\u73b0\u6df1\u62f7\u8d1d\u3002\u540c\u65f6\u6211\u4eec\u79f0\u4e0a\u9762\u7684\u539f\u578b\u6a21\u5f0f\u4e3a\u6d45\u62f7\u8d1d\u3002<\/p>\n<p>\u90a3\u4e48\u4fee\u6539\u5982\u4e0b(\u5176\u4ed6\u4e0d\u53d8)\uff1a<\/p>\n<pre><code class=\"language-java\">@Override\nprotected Object clone() throws CloneNotSupportedException {\n    Book book = (Book) super.clone();\n    book.image = (ArrayList&lt;String&gt;) this.image.clone();\n    return book;\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51fa\u73b0\u5728\u4e92\u4e0d\u5f71\u54cd\uff0c\u8fd9\u4e2a\u53eb\u505a\u6df1\u62f7\u8d1d\u3002<\/p>\n<p>\u63a5\u7740\u4e0a\u9762\u7684\u7591\u95ee\uff0c\u5176\u5b9eString\u7c7b\u578b\u5728\u6d45\u62f7\u8d1d\u65f6\u548c\u5f15\u7528\u7c7b\u578b\u4e00\u6837\uff0c\u6ca1\u6709\u5355\u72ec\u590d\u5236\uff0c\u800c\u662f\u5f15\u7528\u540c\u4e00\u5730\u5740\uff0c\u56e0\u4e3aString\u6ca1\u6709\u5b9e\u73b0cloneable\u63a5\u53e3\uff0c\u4e5f\u5c31\u662f\u8bf4\u53ea\u80fd\u590d\u5236\u5f15\u7528\u3002\uff08\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u6e90\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u800cArrayList\u5b9e\u73b0\u4e86cloneable\u63a5\u53e3\uff09\u4f46\u662f\u5f53\u4fee\u6539\u5176\u4e2d\u7684\u4e00\u4e2a\u503c\u7684\u65f6\u5019\uff0c\u4f1a\u65b0\u5206\u914d\u4e00\u5757\u5185\u5b58\u7528\u6765\u4fdd\u5b58\u65b0\u7684\u503c\uff0c\u8fd9\u4e2a\u5f15\u7528\u6307\u5411\u65b0\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u539f\u6765\u7684String\u56e0\u4e3a\u8fd8\u5b58\u5728\u6307\u5411\u4ed6\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u8fd9\u6837\uff0c\u867d\u7136\u662f\u590d\u5236\u7684\u5f15\u7528\uff0c\u4f46\u662f\u4fee\u6539\u503c\u7684\u65f6\u5019\uff0c\u5e76\u6ca1\u6709\u6539\u53d8\u88ab\u590d\u5236\u5bf9\u8c61\u7684\u503c\u3002 <\/p>\n<p>\u6240\u4ee5\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u628aString\u5728clone\u7684\u65f6\u5019\u548c\u57fa\u672c\u7c7b\u578b\u505a\u76f8\u540c\u7684\u5904\u7406\uff0c\u53ea\u662f\u5728equals\u65f6\u6ce8\u610f\u4e00\u4e9b\u5c31\u884c\u4e86\u3002<\/p>\n<h2>Android\u6e90\u7801\u4e2d\u7684\u539f\u578b\u6a21\u5f0f<\/h2>\n<ul>\n<li>Intent<\/li>\n<\/ul>\n<pre><code class=\"language-java\">Uri uri = Uri.parse(&quot;smsto:10086&quot;);\nIntent intent = new Intent(Intent.ACTION_SEND, uri);\nintent.putExtra(&quot;sms_body&quot;, &quot;The SMS text&quot;);\n\/\/\u514b\u9686\nIntent intent2 = (Intent) intent.clone();\nstartActivity(intent2);<\/code><\/pre>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u4f18\u70b9<\/p>\n<p>\uff081\uff09\u539f\u578b\u6a21\u5f0f\u662f\u5728\u5185\u5b58\u4e2d\u4e8c\u8fdb\u5236\u6d41\u7684\u62f7\u8d1d\uff0c\u8981\u6bd4\u76f4\u63a5new\u4e00\u4e2a\u5bf9\u8c61\u6027\u80fd\u597d\u5f88\u591a\uff0c\u7279\u522b\u662f\u8981\u5728\u4e00\u4e2a\u5faa\u73af\u4f53\u5185\u4ea7\u751f\u5927\u91cf\u5bf9\u8c61\u65f6\uff0c\u539f\u578b\u6a21\u5f0f\u53ef\u80fd\u66f4\u597d\u7684\u4f53\u73b0\u5176\u4f18\u70b9\u3002<br \/>\n\uff082\uff09\u8fd8\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u7528\u9014\u5c31\u662f\u4fdd\u62a4\u6027\u62f7\u8d1d\uff0c\u4e5f\u5c31\u662f\u5bf9\u67d0\u4e2a\u5bf9\u8c61\u5bf9\u5916\u53ef\u80fd\u662f\u53ea\u8bfb\u7684\uff0c\u4e3a\u4e86\u9632\u6b62\u5916\u90e8\u5bf9\u8fd9\u4e2a\u53ea\u8bfb\u5bf9\u8c61\u7684\u4fee\u6539\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u62f7\u8d1d\u7684\u5f62\u5f0f\u5b9e\u73b0\u53ea\u8bfb\u7684\u9650\u5236\u3002<\/p>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<p>\uff081\uff09\u8fd9\u65e2\u662f\u5b83\u7684\u4f18\u70b9\u4e5f\u662f\u7f3a\u70b9\uff0c\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u62f7\u8d1d\uff0c\u6784\u9020\u51fd\u6570\u662f\u4e0d\u4f1a\u6267\u884c\u7684\uff0c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e94\u8be5\u6ce8\u610f\u8fd9\u4e2a\u6f5c\u5728\u95ee\u9898\u3002\u4f18\u70b9\u662f\u51cf\u5c11\u4e86\u7ea6\u675f\uff0c\u7f3a\u70b9\u4e5f\u662f\u51cf\u5c11\u4e86\u7ea6\u675f\uff0c\u9700\u8981\u5927\u5bb6\u5728\u5b9e\u9645\u5e94\u7528\u65f6\u8003\u8651\u3002<br \/>\n\uff082\uff09\u901a\u8fc7\u5b9e\u73b0Cloneable\u63a5\u53e3\u7684\u539f\u578b\u6a21\u5f0f\u5728\u8c03\u7528clone\u51fd\u6570\u6784\u9020\u5b9e\u4f8b\u65f6\u5e76\u4e0d\u4e00\u5b9a\u6bd4\u901a\u8fc7new\u64cd\u4f5c\u901f\u5ea6\u5feb\uff0c\u53ea\u6709\u5f53\u901a\u8fc7new\u6784\u9020\u5bf9\u8c61\u8f83\u4e3a\u8017\u65f6\u6216\u8005\u8bf4\u6210\u672c\u8f83\u9ad8\u65f6\uff0c\u901a\u8fc7clone\u65b9\u6cd5\u624d\u80fd\u591f\u83b7\u5f97\u6548\u7387\u4e0a\u7684\u63d0\u5347\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49 \u7528\u539f\u578b\u5b9e\u4f8b\u6307\u5b9a\u521b\u5efa\u5bf9\u8c61\u7684\u79cd\u7c7b\uff0c\u5e76\u901a\u8fc7\u62f7\u8d1d\u8fd9\u4e9b\u539f\u578b\u521b\u5efa\u65b0\u7684\u5bf9\u8c61\u3002\u88ab\u590d\u5236\u7684\u5b9e\u4f8b\u5c31\u662f\u201c\u539f\u578b\u201d\uff0c\u8fd9\u4e2a\u539f\u578b\u662f\u53ef\u5b9a\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[303],"tags":[],"class_list":["post-1113","post","type-post","status-publish","format-standard","hentry","category-design-pattern"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1113"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}