Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
tjqgithub
javaDesignMode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
javaDesignMode
/
src
/
designMode
/
a05_Prototype
/
Test.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
32 lines (27 loc) · 2.04 KB
master
Breadcrumbs
javaDesignMode
/
src
/
designMode
/
a05_Prototype
/
Test.java
Copy path
Top
File metadata and controls
Code
Blame
32 lines (27 loc) · 2.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package
designMode
.
a05_Prototype
;
import
java
.
util
.
ArrayList
;
/**
* 原型模式 :
* 用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
*
* 原型模式的优点及适用场景:
使用原型模式创建对象比直接new一个对象在性能上要好的多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特别是复制大对象时,性能的差别非常明显。
使用原型模式的另一个好处是简化对象的创建,使得创建对象就像我们在编辑文档时的复制粘贴一样简单。
因为以上优点,所以在需要重复地创建相似对象时可以考虑使用原型模式。比如需要在一个循环体内创建对象,假如对象创建过程比较复杂或者循环次数很多的话,使用原型模式不但可以简化创建过程,而且可以使系统的整体性能提高很多。
实现Cloneable接口。在java语言有一个Cloneable接口,它的作用只有一个,就是在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法。在java虚拟机中,只有实现了这个接口的类才可以被拷贝,否则在运行时会抛出CloneNotSupportedException异常。
重写Object类中的clone方法。Java中,所有类的父类都是Object类,Object类中有一个clone方法,作用是返回对象的一个拷贝,但是其作用域protected类型的,一般的类无法调用,因此,Prototype类需要将clone方法的作用域修改为public类型。
*
*/
public
class
Test
{
public
static
void
main
(
String
[]
args
) {
ArrayList
<
String
>
stuffs
=
new
ArrayList
<
String
>();
stuffs
.
add
(
"aaa"
);
stuffs
.
add
(
"bbb"
);
Sheep
s1
=
new
Sheep
(
"多利"
,
stuffs
);
Sheep
s2
=
s1
.
cloneBySerial
();
System
.
out
.
println
(
"克隆出来的s2 叫:"
+
s2
.
getName
());
s2
.
getStuff
().
add
(
"ccc"
);
System
.
out
.
println
(
"s1的东西:"
+
s1
.
getStuff
());
System
.
out
.
println
(
"s2的东西:"
+
s2
.
getStuff
());
}
}
You can’t perform that action at this time.