1Z0-829 Utilizing Java Object-Oriented Approach Practice Question
You are designing a logging framework for a distributed application. The framework must support multiple output destinations (console, file, network socket) and allow clients to dynamically add new destinations at runtime without modifying existing code. Currently, the application uses a single static Logger class with methods like logToConsole(String msg) and logToFile(String msg). The team needs to refactor the code to adhere to the Open/Closed Principle and support extensibility. After reviewing the requirements, you propose using a combination of an interface and a strategy pattern. However, a senior developer argues that using a simple enum with abstract methods would be sufficient. Which course of action best adheres to object-oriented design principles and allows the most flexibility for future extensions?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Refactor Logger to be an interface, and implement concrete destination classes (ConsoleLogger, FileLogger, SocketLogger). Use dependency injection to pass the desired destination to the Logger client.
Refactors Logger to be an interface and uses dependency injection, allowing clients to pass any implementation. This adheres to the Open/Closed Principle because new destinations can be added by creating new classes implementing Logger without modifying existing code. Option C (using an enum with abstract methods) violates OCP because adding a new destination requires modifying the enum. Option A (keeping static methods and adding new ones) also requires modification of the Logger class. Option D (abstract class with subclasses and factory) still requires modifying the factory when adding new destinations, reducing flexibility.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Keep the existing static methods but add a new method for each destination as needed. Use conditional logic to select the destination at runtime.
Why it's wrong here
This requires modifying the Logger class for each new destination, violating Open/Closed Principle and leading to code duplication.
- ✓
Refactor Logger to be an interface, and implement concrete destination classes (ConsoleLogger, FileLogger, SocketLogger). Use dependency injection to pass the desired destination to the Logger client.
Why this is correct
This follows the strategy pattern and Open/Closed Principle, allowing new destinations to be added without modifying existing code.
- ✗
Create an enum LogDestination with values CONSOLE, FILE, SOCKET, each overriding an abstract log(String) method. The Logger class uses a static method that accepts a LogDestination and calls its log method.
Why it's wrong here
Adding a new destination requires modifying the enum, which violates Open/Closed Principle.
- ✗
Refactor Logger to be an abstract class with an abstract log method, and create subclasses for each destination. Use a factory method to instantiate the correct subclass.
Why it's wrong here
Although better than B or C, adding a new destination still requires modifying the factory, and abstract classes are less flexible than interfaces for multiple inheritance of type.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.