面向对象编程:向方法/组件传递同一对象多引用是否为最佳实践?
First off, these are really thoughtful questions—they get to the heart of code readability, intent clarity, and design robustness, which are all key to maintainable software. Let’s break them down one by one.
1. Is passing multiple references to the same object into a method good practice?
Short answer: It depends entirely on the semantics of the method’s parameters and your explicit intent.
- If the method’s parameters represent distinct roles or responsibilities (e.g.,
processRequest(User requester, User approver)), passing the same object to both is okay only if that’s a valid business scenario (like a user approving their own request). But you should always add a comment explaining this choice—otherwise, future developers reading the code will wonder if it’s a bug (did you mean to pass two different users?). - If the method’s parameters are redundant by design (e.g.,
updateData(Data data1, Data data2)where the method only uses one of them, or expects them to be identical), this is not good practice. It’s a sign of poor method design—you’re forcing callers to do unnecessary work, and the signature lies about what the method actually needs. Refactor the method to take a single parameter instead. - If you’re doing it as a temporary workaround (e.g., a third-party method requires two parameters but you only have one relevant object), wrap it in a helper method with a clear name (like
processSelfApprovedRequest(User user)) so the workaround is encapsulated and obvious.
2. In composition patterns, is passing the same instance as separate parameters to a component class the best practice? (And how does it compare to other approaches?)
Again, this boils down to intent clarity and whether your code aligns with the expectations of the composition pattern.
Let’s ground this in a concrete example: Suppose you have a CompositeWidget (a composite in the pattern) with a method addChildWidgets(Widget header, Widget footer), and you want to use the same TextWidget instance for both header and footer.
Why passing the same instance might be problematic:
- Semantic confusion: The method signature implies
headerandfooterare distinct parts of the composite. Passing the same instance makes it unclear whether this is intentional (you want the same content in both places) or a mistake. - Unexpected side effects: If the composite modifies the state of the passed widget (e.g., changing its font size), both "header" and "footer" will reflect that change—this might be what you want, but it’s not obvious from the method call.
A better alternative (if reuse is your intent):
Refactor the method to make the reuse explicit. For example:
- Change the method to accept a single widget and specify where it should be used:
void addSharedWidget(Widget sharedWidget, WidgetPosition... positions) - Or create a helper method in the composite class that handles the reuse internally:
void addIdenticalHeaderAndFooter(Widget widget) { this.header = widget; this.footer = widget; }
Which approach is better?
If your goal is to reuse the same instance across multiple roles in the composite, the explicit helper method or parameterized position approach is far better. It eliminates ambiguity, makes your intent clear at a glance, and prevents accidental misuse (like passing different instances when you meant to reuse one).
Only pass the same instance as separate parameters if:
- The method’s semantics explicitly allow it (e.g., a
mergeLists(List a, List b)method that works fine whenaandbare the same list), and - You’ve documented this behavior clearly.
内容的提问来源于stack exchange,提问作者Lucas B

