/**
* 子系统1
* 广告检查系统
* 完成广告字数计算
*/
public class CheckWord {
int amount;
String ad;
public CheckWord (String ad){
this.ad = ad;
}
/**
* 计算广告字数,按字数收费
* @return
*/
public int setAmout( ){
System.out.println("计算广告字数");
amount = ad.length();
return amount;
}
}
2)Charge
/**
* 子系统2
* 计算广告价格
*/
public class Charge {
CheckWord word;
public int price;
public Charge(CheckWord word){
this.word = word;
}
public int giveCharge( ){
//计算广告价格
System.out.println("计算价格");
price = word.amount*100;
return price;
}
}
3)TypeSetting
/**
* 子系统3
* 广告排版
*/
public class TypeSetting {
private String ad;
public TypeSetting(String ad){
this.ad = ad;
}
public void typeSetting(){
System.out.println("广告排版");
}
}
4)外观类
/**
* 外观类
* 包含子系统中全部或部分类。
* 这个类替代子系统和用户类打交道,
* 所有用户想要完成的功能由这个类进行调用,
* 避免了用户类和子系统过多的依赖。
*/
public class AdFacade {
private CheckWord checkWord;
private Charge charge;
private TypeSetting typeSetting;
public AdFacade(String ad){
this.checkWord = new CheckWord(ad);
this.charge = new Charge(checkWord);
this.typeSetting = new TypeSetting(ad);
}
/**
* 完成用户发布广告的任务
*/
public void finishAdTask(){
checkWord.setAmout();
charge.giveCharge();
typeSetting.typeSetting();
}
}
5)测试类
public class Main {
public static void main(String args[]){
String ad ="hello world";
AdFacade adFacade = new AdFacade(ad);
adFacade.finishAdTask();
}
}
public class Main {
public static void main(String args[]){
String ad ="hello world";
AdFacade adFacade = new AdFacade(ad);
adFacade.finishAdTask();
}
}