原型模式
- 1.1什么是原型模式
- 1.2为什么要使用原型模式
- 2.原型模式的解释
- 3.例子
- 3.1例子将要实现的目标
- 3.2例子设计
- 3.3原型类
- 3.4具体实现的3个原型实例类
- 3.5管理类
- 3.6工具类
- 3.7测试类
- 3.8测试结果
- 4原型模式的扩充
- 4.1管理类使用为单例模式
- 4.2增加单例对象初始化(客户化)
- 4.3修改测试类
- 4.4输出结果
- 5.总结
23种设计模式
1.1什么是原型模式
根据实例来创建实例。
1.2为什么要使用原型模式
主要为了解决以下3个问题:
1.当对象实例非常大,属性及操作非常多,无法全部装到一个类中时;
2.对象实例的创建非常复杂,比如创建一个对象实例,需要进行许多的操作,才能得到满足要求的实例;
3.框架解耦。使代码具有通用性,解耦与类的关联。
2.原型模式的解释
原型模式中的角色:
原型
原型是定义原型模式将以什么方式来创建原型,以及这些原型实例将要实现的方法。
原型的实现
原型的实现就是实现了原型中未实现的方法的类,也是具体的实例的类。
管理者
原型实例存储的对象,也是原型实例管理的类。
3.例子
3.1例子将要实现的目标
1.第一个类,我通过设置字符,将我传入的字符串用设置的字符包围输出
input:
how are you
output:
#############
#how are you#
#############
2.第二个类,输出传入字符串的回文字符串
input:
im OK
output:
im OKKO mi
paindromeemordniap
3.第三个类,对传入的字符串进行每个字符进行复制
input:
BaLaBaLa
output:
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
3.2例子设计
根据原型模式,例子中包含的类可以分为4类
1.原型
2.具体实现的原型
3.管理
4.测试
3.3原型类
public abstract interface Print extends Cloneable{
public abstract void print(String string);
public abstract Print myClone();
}
3.4具体实现的3个原型实例类
public class Surround implements Print{
private char c = ' ';
public void setChar(char c){
this.c = c;
}
@Override
public Print myClone() {
try {
super.clone();
} catch (Exception e) {
System.out.println("clone exception!");
}
Surround exclamation = new Surround();
exclamation.setChar(c);
return (Print) exclamation;
}
@Override
public void print(String string) {
PrintUtils.printSurround(c, string);
}
}
public class Palindrome implements Print {
private String ss = "";
public void setString(String string){
ss = string;
}
@Override
public Print myClone() {
try {
super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
Palindrome palindrome = new Palindrome();
palindrome.setString(ss);
return (Print) palindrome;
}
@Override
public void print(String string) {
PrintUtils.printPalindrome(string);
PrintUtils.printPalindrome(ss);
}
}
public class Reread implements Print{
private int num;
public void setNum(int num) {
this.num = num;
}
@Override
public void print(String string) {
while(num > 0){
PrintUtils.printReread(string);
num--;
}
}
@Override
public Print myClone() {
try {
super.clone();
} catch (Exception e) {
System.out.println("clone exception!");
}
Reread reread = new Reread();
reread.setNum(num);
return (Print) reread;
}
}
3.5管理类
import java.util.HashMap;
import java.util.Map;
public class Manage {
private Map<String, Print> proMap = new HashMap<String, Print>();
public void register(String name,Print print){
proMap.put(name, print.myClone());
}
public Print create(String name){
return proMap.get(name).myClone();
}
}
3.6工具类
public class PrintUtils {
public static void printReread(String string){
System.out.println();
for(int i = 0;i < string.length();i++){
System.out.print(string.charAt(i));
System.out.print(string.charAt(i));
}
System.out.println();
}
public static void printPalindrome(String string){
System.out.println();
System.out.print(string);
for(int i = string.length() - 1;i >= 0;i--){
System.out.print(string.charAt(i));
}
System.out.println();
}
public static void printSurround(char c,String string){
System.out.println();
for(int i = 0;i < string.length() + 2;i++){
System.out.print(c);
}
System.out.println();
System.out.print(c);
System.out.print(string);
System.out.println(c);
for(int i = 0;i < string.length() + 2;i++){
System.out.print(c);
}
System.out.println();
System.out.println();
}
}
3.7测试类
package cn.com.startimes.prototype;
public class Main {
public static void main(String[] args) {
Manage manage = new Manage();
Surround exclamation = new Surround();
exclamation.setChar('!');
manage.register("!", exclamation);
exclamation.setChar('@');
manage.register("@", exclamation);
exclamation.setChar('#');
manage.register("#", exclamation);
manage.create("#").print("how are you");
Palindrome palindrome = new Palindrome();
palindrome.setString("paindrome");
manage.register("pal", palindrome);
manage.create("pal").print("im OK");
Reread reread = new Reread();
reread.setNum(3);
manage.register("reread", reread);
manage.create("reread").print("BaLaBaLa");
}
}
3.8测试结果
#############
#how are you#
#############
im OKKO mi
paindromeemordniap
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
4原型模式的扩充
4.1管理类使用为单例模式
使用饿汉式—-类加载时创建
import java.util.HashMap;
import java.util.Map;
public class Manage {
private static final Manage MANAGE = new Manage();
private Manage(){
}
public static Manage getManage(){
return MANAGE;
}
private Map<String, Print> proMap = new HashMap<String, Print>();
public void register(String name,Print print){
proMap.put(name, print.myClone());
}
public Print create(String name){
return proMap.get(name).myClone();
}
}
4.2增加单例对象初始化(客户化)
public class ManageSurr{
//使用键盘字符初始化30-122
public static void init(){
Manage manage = Manage.getManage();
Surround exclamation = new Surround();
for(int i = 30;i <= 122;i++){
exclamation.setChar((char)i);
manage.register((char)i+"", exclamation);
}
}
}
4.3修改测试类
public class Main {
public static void main(String[] args) {
Manage manage = Manage.getManage();
ManageSurr.init();
for(int i = 30;i <= 122;i++){
manage.create((char)i+"").print("how are you");
}
Palindrome palindrome = new Palindrome();
palindrome.setString("paindrome");
manage.register("pal", palindrome);
manage.create("pal").print("im OK");
Reread reread = new Reread();
reread.setNum(3);
manage.register("reread", reread);
manage.create("reread").print("BaLaBaLa");
}
}
4.4输出结果
how are you
how are you
how are you
!!!!!!!!!!!!!
!how are you!
!!!!!!!!!!!!!
"""""""""""""
"how are you"
"""""""""""""
#############
#how are you#
#############
$$$$$$$$$$$$$
$how are you$
$$$$$$$$$$$$$
%%%%%%%%%%%%%
%how are you%
%%%%%%%%%%%%%
&&&&&&&&&&&&&
&how are you&
&&&&&&&&&&&&&
'''''''''''''
'how are you'
'''''''''''''
(((((((((((((
(how are you(
(((((((((((((
)))))))))))))
)how are you)
)))))))))))))
*************
*how are you*
*************
+++++++++++++
+how are you+
+++++++++++++
,,,,,,,,,,,,,
,how are you,
,,,,,,,,,,,,,
-------------
-how are you-
-------------
.............
.how are you.
.............
/////////////
/how are you/
/////////////
0000000000000
0how are you0
0000000000000
1111111111111
1how are you1
1111111111111
2222222222222
2how are you2
2222222222222
3333333333333
3how are you3
3333333333333
4444444444444
4how are you4
4444444444444
5555555555555
5how are you5
5555555555555
6666666666666
6how are you6
6666666666666
7777777777777
7how are you7
7777777777777
8888888888888
8how are you8
8888888888888
9999999999999
9how are you9
9999999999999
:::::::::::::
:how are you:
:::::::::::::
;;;;;;;;;;;;;
;how are you;
;;;;;;;;;;;;;
<<<<<<<<<<<<<
<how are you<
<<<<<<<<<<<<<
=============
=how are you=
=============
>>>>>>>>>>>>>
>how are you>
>>>>>>>>>>>>>
?????????????
?how are you?
?????????????
@@@@@@@@@@@@@
@how are you@
@@@@@@@@@@@@@
AAAAAAAAAAAAA
Ahow are youA
AAAAAAAAAAAAA
BBBBBBBBBBBBB
Bhow are youB
BBBBBBBBBBBBB
CCCCCCCCCCCCC
Chow are youC
CCCCCCCCCCCCC
DDDDDDDDDDDDD
Dhow are youD
DDDDDDDDDDDDD
EEEEEEEEEEEEE
Ehow are youE
EEEEEEEEEEEEE
FFFFFFFFFFFFF
Fhow are youF
FFFFFFFFFFFFF
GGGGGGGGGGGGG
Ghow are youG
GGGGGGGGGGGGG
HHHHHHHHHHHHH
Hhow are youH
HHHHHHHHHHHHH
IIIIIIIIIIIII
Ihow are youI
IIIIIIIIIIIII
JJJJJJJJJJJJJ
Jhow are youJ
JJJJJJJJJJJJJ
KKKKKKKKKKKKK
Khow are youK
KKKKKKKKKKKKK
LLLLLLLLLLLLL
Lhow are youL
LLLLLLLLLLLLL
MMMMMMMMMMMMM
Mhow are youM
MMMMMMMMMMMMM
NNNNNNNNNNNNN
Nhow are youN
NNNNNNNNNNNNN
OOOOOOOOOOOOO
Ohow are youO
OOOOOOOOOOOOO
PPPPPPPPPPPPP
Phow are youP
PPPPPPPPPPPPP
QQQQQQQQQQQQQ
Qhow are youQ
QQQQQQQQQQQQQ
RRRRRRRRRRRRR
Rhow are youR
RRRRRRRRRRRRR
SSSSSSSSSSSSS
Show are youS
SSSSSSSSSSSSS
TTTTTTTTTTTTT
Thow are youT
TTTTTTTTTTTTT
UUUUUUUUUUUUU
Uhow are youU
UUUUUUUUUUUUU
VVVVVVVVVVVVV
Vhow are youV
VVVVVVVVVVVVV
WWWWWWWWWWWWW
Whow are youW
WWWWWWWWWWWWW
XXXXXXXXXXXXX
Xhow are youX
XXXXXXXXXXXXX
YYYYYYYYYYYYY
Yhow are youY
YYYYYYYYYYYYY
ZZZZZZZZZZZZZ
Zhow are youZ
ZZZZZZZZZZZZZ
[[[[[[[[[[[[[
[how are you[
[[[[[[[[[[[[[
\\\\\\\\\\\\\
\how are you\
\\\\\\\\\\\\\
]]]]]]]]]]]]]
]how are you]
]]]]]]]]]]]]]
^^^^^^^^^^^^^
^how are you^
^^^^^^^^^^^^^
_____________
_how are you_
_____________
//这里因为英文的点和csdn的代码标记相同,所以认为加上\
//原本的输出是没有\
\`````````````
`how are you`
\`````````````
aaaaaaaaaaaaa
ahow are youa
aaaaaaaaaaaaa
bbbbbbbbbbbbb
bhow are youb
bbbbbbbbbbbbb
ccccccccccccc
chow are youc
ccccccccccccc
ddddddddddddd
dhow are youd
ddddddddddddd
eeeeeeeeeeeee
ehow are youe
eeeeeeeeeeeee
fffffffffffff
fhow are youf
fffffffffffff
ggggggggggggg
ghow are youg
ggggggggggggg
hhhhhhhhhhhhh
hhow are youh
hhhhhhhhhhhhh
iiiiiiiiiiiii
ihow are youi
iiiiiiiiiiiii
jjjjjjjjjjjjj
jhow are youj
jjjjjjjjjjjjj
kkkkkkkkkkkkk
khow are youk
kkkkkkkkkkkkk
lllllllllllll
lhow are youl
lllllllllllll
mmmmmmmmmmmmm
mhow are youm
mmmmmmmmmmmmm
nnnnnnnnnnnnn
nhow are youn
nnnnnnnnnnnnn
ooooooooooooo
ohow are youo
ooooooooooooo
ppppppppppppp
phow are youp
ppppppppppppp
qqqqqqqqqqqqq
qhow are youq
qqqqqqqqqqqqq
rrrrrrrrrrrrr
rhow are your
rrrrrrrrrrrrr
sssssssssssss
show are yous
sssssssssssss
ttttttttttttt
thow are yout
ttttttttttttt
uuuuuuuuuuuuu
uhow are youu
uuuuuuuuuuuuu
vvvvvvvvvvvvv
vhow are youv
vvvvvvvvvvvvv
wwwwwwwwwwwww
whow are youw
wwwwwwwwwwwww
xxxxxxxxxxxxx
xhow are youx
xxxxxxxxxxxxx
yyyyyyyyyyyyy
yhow are youy
yyyyyyyyyyyyy
zzzzzzzzzzzzz
zhow are youz
zzzzzzzzzzzzz
im OKKO mi
paindromeemordniap
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
BBaaLLaaBBaaLLaa
5.总结
使用原型模式的重要点在于两个方面:
1.原型的接口定义
2.原型的管理实现
我们发现,原型模式解耦主要是对于原型模式种的原型类以及管理类,对于这两个类而言,他们的关联范围就是这两个类。
怎么理解,对于原型类,他是操作的提供者,原型类的关联范围就是他自己。
对于管理类,管理类是原型类的操作的使用者,管理类的关联范围是原型类和他自己。
3.无限的扩充
因为原型是个接口,所以具体的原型实例类可以无限的扩充。
只要一个类实现了接口中定义的方法,那么,这个类就是原型的扩充类。
4.框架的解耦
框架的解耦就是依赖以上3点,对于框架内部的类:原型类和管理类,他们不再乎你能扩充多少,或者有多少的其他操作。他们只关心接口中的操作,以及对应的关联范围。
5.普通类的使用
普通类使用框架,也不需要关心框架类的具体实现,普通的扩充类只关心需要实现的接口的方法。
23种设计模式