面向对象C++点唱机设计代码评审及面试关注点咨询
Context
I'm preparing for interviews and working through problems in Cracking the Coding Interview. While I know the book has answers, the object-oriented design section's explanations are vague, and I'm unsure if my jukebox implementation is correct. I'd like a review of my code to identify optimizations, get a rating, and understand what interviewers focus on for such problems. My code is below:
Provided Code
#include <iostream> #include <vector> #include <queue> class ListToPlay { std::queue<const Song&> queue_to_play_; const Speaker& speaker; public: void Play(); void AddSong(const Song& song); void RemoveSong(int index); void Clean(); const std::queue<const Song&>& getQueueToPlay() const; }; class Song { std::ostream data_; std::string name_; public: const std::ostream getData() const; const std::string getName() const; void setData(std::ostream data); void setName(std::string name); }; class CD { std::vector<Song> sangs_; // Typo: should be songs_ std::string name_; std::string author_; public: const std::vector<Song>& getSongs() const; const std::string& getName() const; const std::string& getAuthor() const; void addSong(Song song); void setName(std::string name); void setAuthor(std::string author); }; class Grafics { // Typo: should be Graphics const ListToPlay& list_to_play_; const std::vector<CD>& cd_vector_; public: void setCDVector(const std::vector<CD>& cd_vector_); void setListToPlay(const ListToPlay& list_to_play); void ClickedPlay(); void ClickedStop(); void ClickedClean(); void ClickedAddToPlayList(int index); void ClickedSelectCD(); }; class Speaker { public: void PlaySound(Song song); }; class Jukebox { public: Grafics grafics_; Speaker speaker_; std::vector<CD> cd_vector_; std::vector<ListToPlay> list_to_play_; Jukebox(Grafics grafics, Speaker speaker, std::vector<CD> cd_vector) : grafics_(grafics), speaker_(speaker), cd_vector_(cd_vector) { ; } }; int main() { ; }
Key Optimizations & Fixes
- Fix Typographical Errors:
sangs_in theCDclass should besongs_Graficsclass name should beGraphics(spelling mistake)
- Address Reference & Ownership Issues:
std::queue<const Song&>is invalid because queues can't store references directly—this will cause undefined behavior if the referencedSonggoes out of scope. Usestd::shared_ptr<const Song>instead to manage object ownership safely.- The
const Speaker&inListToPlaylacks a constructor to initialize it—this will trigger a compilation error. Add a constructor toListToPlaythat accepts aSpeaker&and initializes the member.
- Improve Encapsulation:
- Public member variables in
Jukebox(grafics_,speaker_, etc.) should be private to enforce controlled access through public methods. getQueueToPlay()exposes the internal queue directly—break encapsulation. Instead, provide limited methods likeIsQueueEmpty()orGetNextSong()to interact with the queue without exposing its state.
- Public member variables in
- Align with Single Responsibility Principle:
ListToPlaymanages the queue and holds aSpeakerreference for playback—this mixes two responsibilities. LetJukeboxor a dedicatedPlayerclass coordinate the queue and speaker instead.Graphicsholds references to external objects that could become invalid if the originals are modified/destroyed. Use observers or shared pointers to maintain safe, valid connections.
- Fix Data Type Misuse:
Songusesstd::ostream data_to store audio content—this is incorrect (ostream is for output, not storage). Usestd::vector<char>orstd::stringto hold the song's actual data.getData()returnsconst std::ostream, which doesn't make sense for retrieving stored content—update this to return your chosen audio data type.
Interviewer's Core Focus Areas for OOD Questions
- Single Responsibility Principle: Each class should have one clear, well-defined job.
- Encapsulation: Hiding internal state and exposing only necessary, controlled methods to prevent unintended side effects.
- Object Relationships: How classes interact (composition, dependency injection, inheritance) and whether these relationships make logical sense.
- Error Handling & Edge Cases: How the design handles empty queues, invalid selections, or failed playback operations.
- Extensibility: Can the design easily support new features like shuffle mode, repeat, or streaming songs instead of CDs?
- Clarity & Naming: Correct spelling, meaningful class/method names, and self-documenting code that's easy to follow.
Rating: 5/10
Your design has the right high-level components (Song, CD, Play Queue, Speaker, UI) that align with a jukebox's core functionality. However, it has critical compilation issues, violates key OOP principles, and lacks attention to detail around data types and ownership. Fixing the immediate errors and aligning with best practices would significantly improve its quality.
内容的提问来源于stack exchange,提问作者wojtek

