当抽象很多,具体的也很多,就需要将抽象和具体进行分离,用聚合连接抽象和具体
比如礼物这个抽象类,他有可以吃的礼物、可以玩的礼物...很多种类,他的具体类也有很多,比如可以吃的礼物有鱼、肉...,可以玩的礼物有五子棋、象棋...,这样就很有必要将抽象和具体分离,他们各自去发展。 礼物类:
public abstract class Gift {
GiftImpl gitfImpl;
}
抽象的礼物:
public class EatGift extends Gift{
public EatGift(GiftImpl gitfImpl) {
this.gitfImpl = gitfImpl;
}
}
public class PlayGift extends Gift{
public PlayGift(GiftImpl gift) {
this.gitfImpl = gift;
}
}
具体的礼物:
public class Fish extends GiftImpl {
}
public class Gobang extends GiftImpl {
}
使用:
public class Test1 {
public static void main(String[] args) {
PlayGift playGift = new PlayGift(new Gobang());
}
}