public interface IMoudle {
String getName();
void add(IMoudle moudle);
void remove(IMoudle moudle);
}
public class Moudle implements IMoudle {
private String name;
private List<IMoudle> childrenMoudleList;
public Moudle(int price, String name) {
this.name = name;
childrenMoudleList = new ArrayList<>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void add(IMoudle moudle) {
childrenMoudleList.add(moudle);
}
@Override
public void remove(IMoudle moudle) {
childrenMoudleList.remove(moudle);
}
public List<IMoudle> getChildrenMoudleList() {
return childrenMoudleList;
}
public void setChildrenMoudleList(List<IMoudle> childrenMoudleList) {
this.childrenMoudleList = childrenMoudleList;
}
}
public class Battery extends Moudle {
public Battery(int price, String name) {
super(price,name);
}
}
public class CPU extends Moudle {
public CPU(int price, String name) {
super(price,name);
}
}
public class Phone extends Moudle {
public Phone(int price, String name) {
super(price,name);
}
}
public class PhoneMaker {
private static class PhoneMakerDelegate extends PhoneMaker{
public static PhoneMaker maker=new PhoneMakerDelegate();
}
private PhoneMaker() {
}
public static PhoneMaker instance() {
return PhoneMakerDelegate.maker;
}
public IMoudle getMyPhone(String cpuName,Integer batteryCap){
IMoudle phone=new Phone(100,"Iphone");
IMoudle cpu=new CPU(300,cpuName);
IMoudle battery=new Battery(batteryCap,"battery");
phone.add(cpu);
phone.add(battery);
return phone;
}
}
public class App {
public static void main(String[] args) {
IMoudle phone=PhoneMaker.instance().getMyPhone("A8",5000);
System.out.println(JSON.toJSONString(phone));
}
}