将抽象和实现分离,提高了系统的灵活性,使系统进行分层,从而产生更好的系统结构。
public abstract class Corp2 {
private Product product;
public Corp2(Product product){
this.product = product;
}
public void makeMoney(){
this.product.beProducted();
this.product.beSelled();
}
}
public abstract class Product {
public abstract void beProducted();
public abstract void beSelled();
}
public class HouseCorp extends Corp2{
public HouseCorp(House house){
super(house);
}
public void makeMoney(){
super.makeMoney();
System.out.println("房地产公司赚大钱了...");
}
}
public class House extends Product{
@Override
public void beProducted() {
System.out.println("生产出的房子是这样的...");
}
@Override
public void beSelled() {
System.out.println("生产出的房子卖出了...");
}
}
//山寨公司
public class CopycatCorp extends Corp2{
public CopycatCorp(Product product) {
super(product);
}
public void makeMoney(){
super.makeMoney();
System.out.println("我赚钱...");
}
}
public class Ipod extends Product{
@Override
public void beProducted() {
System.out.println("产出的iPod 是这样的...");
}
@Override
public void beSelled() {
System.out.println("生产出的iPod卖出去了...");
}
}
public class Client2 {
Corp2 hc = null;
House house = null;
public void main() {
house = new House(); //产品
Ipod i = new Ipod();
hc = new HouseCorp(house);//公司需要产品
hc.makeMoney(); // 执行
}
public static void main(String[] args) {
new Client2().main();
}
}