返回信息流刚开始看设计模式…感觉这俩完全就是一样的…工厂方法模式还套了个中间层…[ema8][ema8]
通过『我邮2.0』发布
这是一条镜像帖。来源:北邮人论坛 / soft-design / #46406同步于 2015/11/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
工厂方法模式和策略模式区别在哪里
a472770699
2015/11/23镜像同步10 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
根本没关系吧。
工厂方法模式:
abstract class AbstractSayer {
public void say() {
System.out.println(getWord());
}
abstract String getWord(); // 把构造对象的责任交给子类
}
class HelloSayer extends AbstractSayer {
@Override String getWord() { return "Hello"; }
}
class GoodbyeSayer extends AbstractSayer {
@Override String getWord() { return "Goodbye"; }
}
public class Main { public static void main(String[] args) {
AbstractSayer s1 = new HelloSayer();
AbstractSayer s2 = new GoodbyeSayer();
s1.say();
s2.say();
}}
策略模式
class Sayer {
private SayingStrategy str;
public Sayer(SayingStrategy str) { this.str = str; }
public void say() { str.say(); } // 把做事情的方法交给别的类。
}
interface SayingStratery {
void say();
}
class HelloStrategy {
@Override public void say() { System.out.println("Hello"); }
}
class GoodbyeStrategy {
@Override public void say() { System.out.println("Goodbye"); }
}
public class Main { public static void main(String[] args) {
Sayer s1 = new Sayer(new HelloStrategy());
Sayer s2 = new Sayer(new GoodbyeStrategy());
s1.say();
s2.say();
}}
我看好你呀,在来一个Perl的工厂类吧。
【 在 nuanyangyang 的大作中提到: 】
: 根本没关系吧。
: 工厂方法模式:
: [code=java]
: ...................
#!/usr/bin/perl -w
use strict;
$_='ev
al("seek\040D
ATA,0, 0;");foreach(1..2)
{;}my @camel1hump;my$camel;
my$Camel ;while( ){$_=sprintf("%-6
9s",$_);my@dromedary 1=split(//);if(defined($
_=)){@camel1hum p=split(//);}while(@dromeda
ry1){my$camel1hump=0 ;my$CAMEL=3;if(defined($_=shif
t(@dromedary1 ))&&/\S/){$camel1hump+=1<<$CAMEL;}
$CAMEL--;if(d efined($_=shift(@dromedary1))&&/\S/){
$camel1hump+=1 <<$CAMEL;}$CAMEL--;if(defined($_=shift(
@camel1hump))&&/\S/){$camel1hump+=1<<$CAMEL;}$CAMEL--;if(
defined($_=shift(@camel1hump))&&/\S/){$camel1hump+=1<<$CAME
L;;}$camel.=(split(//,"\040..m`{/J\047\134}L^7FX"))[$camel1h
ump];}$camel.="\n";}@camel1hump=split(/\n/,$camel);foreach(@
camel1hump){chomp;$Camel=$_;tr/LJF7\173\175`\047/\061\062\063
45678/;tr/12345678/JL7F\175\173\047`/;$_=reverse;print"$_\040
$Camel\n";}foreach(@camel1hump){chomp;$Camel=$_;y/LJF7\173\17
5`\047/12345678/;tr/12345678/JL7F\175\173\047`/;$_=reverse;p
rint"\040$_$Camel\n";}#japh-Erudil';;s;\s*;;g;;eval; eval
("seek\040DATA,0,0;");undef$/;$_=;s$\s*$$g;( );;s
;^.*_;;;map{eval"print\"$_\"";}/.{4}/g; __DATA__ \124
\1 50\145\040\165\163\145\040\157\1 46\040\1 41\0
40\143\141 \155\145\1 54\040\1 51\155\ 141
\147\145\0 40\151\156 \040\141 \163\16 3\
157\143\ 151\141\16 4\151\1 57\156
\040\167 \151\164\1 50\040\ 120\1
45\162\ 154\040\15 1\163\ 040\14
1\040\1 64\162\1 41\144 \145\
155\14 1\162\ 153\04 0\157
\146\ 040\11 7\047\ 122\1
45\15 1\154\1 54\171 \040
\046\ 012\101\16 3\16
3\15 7\143\15 1\14
1\16 4\145\163 \054
\040 \111\156\14 3\056
\040\ 125\163\145\14 4\040\
167\1 51\164\1 50\0 40\160\
145\162 \155\151
\163\163 \151\1
57\156\056
# camel code, copywrite 2000 by Stephen B. Jenkins
# The use of a camel image with the topic of Perl
# is a trademark of O'Reilly & Associates, Inc.
# Used with permission.
【 在 FromSixToTen 的大作中提到: 】
: 我看好你呀,在来一个Perl的工厂类吧。
暖神还是棒!
【 在 nuanyangyang 的大作中提到: 】
: [code=perl]
: #!/usr/bin/perl -w
: use strict;
: ...................
这头骆驼不错,我已牵走做成注释的logo。
【 在 nuanyangyang 的大作中提到: 】
: [code=perl]
: #!/usr/bin/perl -w
: use strict;
: ...................
暖神好厉害!
【 在 nuanyangyang 的大作中提到: 】
: [code=perl]
: #!/usr/bin/perl -w
: use strict;
: ...................
哈哈哈。我这有一个。
啊,是注释,不是代码
// ┏ ┓ ┏ ┓
//┏┛ ┻━━━━━┛ ┻┓
//┃ ┃
//┃ ━ ┃
//┃ ┳┛ ┗┳ ┃
//┃ ┃
//┃ ┻ ┃
//┃ ┃
//┗━┓ ┏━━━┛
// ┃ ┃ 神兽保佑
// ┃ ┃ 代码无BUG!
// ┃ ┗━━━━━━━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗━┓ ┓ ┏━━━┳ ┓ ┏━┛
// ┃ ┫ ┫ ┃ ┫ ┫
【 在 nuanyangyang 的大作中提到: 】
:
: 想找一个羊驼形状的代码。