Home > Backend Development > C++ > body text

How to Remove a Friend Declaration Without Compromising Design?

Patricia Arquette
Release: 2024-11-04 13:20:30
Original
933 people have browsed it

How to Remove a Friend Declaration Without Compromising Design?

How to Remove a Friend Declaration While Preserving Design

The usage of the "friend" keyword in object-oriented design can create tight dependencies and hinder maintainability. This article provides a comprehensive approach to remove a friend declaration while preserving the overall design of the system.

Problem Background:

Consider a scenario where two classes, ClassA and ClassAAccessor, have a friend relationship. ClassA represents a shared resource with protected methods, while ClassAAccessor serves as a helper to manage access to this resource. The friend relationship between ClassA and ClassAAccessor allows ClassAAccessor to directly access the protected methods of ClassA.

Design Constraints:

To ensure proper refactoring, several constraints are set:

  • The public interface of ClassAAccessor should remain unchanged.
  • Internal operations of ClassA should remain private.
  • Performance and memory consumption should not be significantly impacted.

Refactoring Steps:

Step 1: Introduce an Abstract Interface

Extract the operations that were previously accessible via the friend relationship into a separate interface called InternalInterface. Refactor the relationship between ClassA and ClassAAccessor to make it dependent on this interface rather than using the friend keyword.

Step 2: Move Operations to Interface

Move the operations from ClassA to the InternalInterface. This eliminates the "call" dependency from ClassAAccessor directly to ClassA.

Step 3: Glue Implementation Together

Create a private member variable in ClassAAccessor that points to an instance of InternalInterface. Introduce a method in ClassA that allows setting this member variable to enable ClassAAccessor to access the required internal operations.

Implementation Example:

<code class="cpp">class ClassAAccessor {
public:
    ClassAAccessor(ClassA&amp; classA);
    void setInternalInterfaceRef(InternalInterface &amp; newValue) {
        internalInterfaceRef = &amp;newValue;
    }
private:  
    InternalInterface* internalInterfaceRef;
};

class ClassA : protected InternalInterface {
public:
    attachAccessor(ClassAAccessor &amp; accessor);
};</code>
Copy after login

Advantages of Refactoring:

  • Eliminates tight dependency between ClassA and ClassAAccessor.
  • Ensures private access to internal operations of ClassA.
  • Provides a more modular and maintainable design.

Disadvantages of Refactoring:

  • Increased complexity in code structure.
  • Potential for slightly increased memory consumption due to the introduction of additional interfaces.
  • Limited UML support for protected generalization relationships.

The above is the detailed content of How to Remove a Friend Declaration Without Compromising Design?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!