CS FundamentalsHardProduct-company design interview
Code contains `order.getCustomer().getAddress().getCity().toUpperCase()`. Which principle does it most directly violate, and why does it matter?
- A. Single Responsibility; the Order class does too much
- B. Law of Demeter; the caller depends on the internal structure of three other classes, so a change to any of them breaks it
- C. Liskov Substitution; the chain may return a subtype
- D. Interface Segregation; Customer has too many getters
Correct answer: B. Law of Demeter; the caller depends on the internal structure of three other classes, so a change to any of them breaks it
Demeter ('talk only to your immediate friends') warns against reaching through objects. The fix is to ask the nearer object for what you need, e.g. order.shippingCity(), which reduces coupling.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A module reads a raw record, cleans it, and passes the cleaned record to a parser whose output is passed to a validator - each step's…
- Match each scenario with its coupling type. P: a module passes a flag telling the callee which of several operations to perform. Q: a…
- A module 'Startup' opens the log file, loads configuration, connects to the database and clears the cache. These tasks are unrelated…
- A UI component subscribes to a long-lived event bus in its constructor but never unsubscribes. Screens using it are opened and closed many…
- In GRASP terms, an Order aggregates its LineItems and has the data needed to initialise them. Which class should create LineItem objects…
- Assertion (A): Classes that call a Singleton through its global getInstance() are harder to unit-test. Reason (R): The dependency is…