java ???????????1??
???????????? ???????[ 2013/8/5 10:46:21 ] ????????
????????????????y????.
?????????????????仯?????????????????????????????????????????????????????????????????????????
?????????Duck?е?fly??????quack?????????
public interface Flyable{
public void fly();
}
public interface Quackable{
public void quack();
}
??Duck?????????
public class Duck{
public void swim(){ //???
System.out.println(" ???");
}
public abstract void display(); /*???????????????????? ??????????*/
}
??MallardDuck??RedheadDuck??DisabledDuck ????д?????
//??
public class MallardDuck extends Duck implements Flyable??Quackable{
public void display(){
System.out.println("???????...");
}
public void fly(){
//???÷???
}
public void quack(){
//???÷???
}
}
//????
public class RedheadDuck extends Duck implements Flyable??Quackable{
public void display(){
System.out.println("?????????...");
}
public void fly(){
//???÷???
}
public void quack(){
//???÷???
}
}
//?з?? ????Quackable????в?????
public class DisabledDuck extends Duck implements Quackable{
public void display(){
System.out.println("?з???????...");
}
public void quack(){
//???÷???
}
}
????>>>>>>????:
???????:
??????????????????????????????????????
????????:
????Flyable?? Quackable??????????????????????????????л?????????? Flyable????????Java???????????????????????????????????á?
??????????????????????????????:
??????е???:?ù??????????????.??????????.
??????е????:??????.??????????????·???????????У???д??????
????????????????????????????????д????????.
??????????????????????????.
??????????:??????????????????.
????????????????л?????????????.
???????????:??????????????????.???????μ???????????.
????------------------------- strategy(??????) -------------------------
???????????????????
???????????????????????????????仯????????????????????????У????????????????
???????????п???????仯???????????????????????????Щ??????仯??????????? -->important.
????????????????“?仯????仯?????”?????????????????????????Duck?????????"fly"??????????
??????“quack”??????????????????????????????????????????????“?????”????????????“??
??????”??????????????“????”??
????????д????????FlyBehavior(???????)??QuackBehavior???е??????.
public interface FlyBehavior{
public void fly();
}
public interface QuackBehavior{
public void quack();
}
??????????Щ???FlyBehavior?????????
public class FlyWithWings implements FlyBehavior{
public void fly(){
//??????????г??????????????
}
}
public class FlyNoWay implements FlyBehavior{
public void fly(){
//???????????????
}
}
???QuackBehavior????????????
public class Quack implements QuackBehavior{
public void quack(){
//???????е????
}
}
public class Squeak implements QuackBehavior{
public void quack(){
//??????е????
}
}
public class MuteQuack implements QuackBehavior{
public void quack(){
//???????????????
}
}
?????????:
???????????????????÷??к?????е???????????????????????Щ??????????????????????????????Щ?μ????????????????е??????????????“???”????????????????
?????????????Duck ???????
public class Duck{ --------->????????У??????????????????????????.
FlyBehavior flyBehavior;//???
QuackBehavior quackBehavior;//???
public Duck(){}
public abstract void display();
public void swim(){
//???????????
}
public void performFly(){
flyBehavior.fly(); -->????????????????????????????????????????.
}
public void performQuack(){
quackBehavior.quack();();
}
}
????????MallardDuck???????
????----->???????????????'??'??'??'?????????????????????'??'??'??'?????????
public class MallardDuck extends Duck{
public MallardDuck {
flyBehavior = new FlyWithWings ();
quackBehavior = new Quack();
//???MallardDuck ?????Duck?????о???flyBehavior ??quackBehavior ???????}
public void display(){
//???
}
}
??????????????????????????У?????????????????
??????????????????????????flyBehavior ??quackBehavior ???????д??????????????????????????????
?????????????Duck?м?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11