이것은 인터뷰 질문입니다. 그들은 코드를 제공하고 그것을 비판하고보다 OOP 방식으로 재 배열하도록 요청합니다.OOP 원칙을 존중하는 코드 리팩토링
public abstract class Storage{
public static Vector<IStorage> objects = /*...*/; // Create a vector IStorage empty, I do not know how
public Storage(String xmlFile){
ReadFromXMLFile(xmlFile);
objects.addElement((IStorage)this);
}
protected abstract ReadFromXMLFile(String xmlFile);
}
public interface IStorage{
public String getAuthor();
public String getEditor();
public String getName();
public boolean isBook();
public boolean isFood();
}
// Exemple d'implémentation
public class Novel implements IStorage extends Storage{
private String author;
private String editor;
private String name;
private boolean historical;
public Novel(String xmlFile){
super(xmlFile);
}
public String getAuthor(){return author;}
public String getEditor(){return editor;}
public String getName(){return name;}
public boolean isHistorical(){return historical;}
public void setHistorical(boolean b){historical = b;}
public void setAuthor(String a){author = a;}
public void setEditor(String e){editor = e;}
public void setName(String n){name = n;}
public boolean isBook(){return true;}
public boolean isFood(){return false;}
public boolean needRefrigeration(){return false;}
private ReadFromXMLFile(String xmlFile){
// Loads the object from an XML file
// The implementation of this method is not given
}
}
잘못된 OOP 프로그래밍을 감지하지 못했습니다. 이 코드에서 무엇이 잘못되었으며 어떻게 변경할 수 있습니까?
감사합니다. 문제가있는 곳입니다
내가 너무 광범위하게 그것을 닫아야 할 많은 잘못이 있습니다. 5 분 동안 문제에 대해 직접 이야기 할 수있었습니다. – Bohemian
당신이 알아야 할 문제 중 하나는 음식과 소설 모두에 공통된 추상 클래스를 사용한다는 것입니다. 어떤 종류의 합리적인 종류의 시스템에서도 완전히 무관합니다. 결국, 당신은 정말로 음식의 저자 또는 편집자를 얻고 싶습니까? 정적 벡터는 또한 약간의 문제입니다. –
@ 보헤미안 - 그렇지 않은 경우 좋은 인터뷰 질문이 아닙니다. –