Java – stay away from children: protected fields cannot be deleted from inheritance

In the spirit of well - designed OO, a category I have extended has marked that one of these areas has been protected The class also generously provided a public setter, but there was no getter

I'm using a base class to extend this class, and the base class is extended by several children How can I restrict access to protected variables from my children while still manipulating it privately and setting it publicly?

See the following example:

public abstract class ThirdPartyClass {
  protected Map propertyMap;

  public void setPropertyMap(Map propertyMap){
    this.propertyMap= propertyMap;
  }

  // Other methods that use propertyMap.
}

public abstract class MyBaseClass extends ThirdPartyClass{
// Accessor methods for entries in propertyMap.
  public getFoo(){
    propertyMap.get("Foo");
  }

  public getBar(){
    propertyMap.get("Bar");
  }

 // etc...
}

public class OneOfManyChildren extends MyBaseClass {
// Should only access propertyMap via methods in MyBaseClass.
}

I have found that I can revoke access by creating the field private final in mybaseclass However, this also prevents the use of setters provided by superclasses

I can circumvent this limitation by using the following "smart", but it will also lead to the maintenance of two copies of the same map and the O (n) operation of copying each element

public abstract class MyBaseClass extends ThirdPartyClass{

  private final Map propertyMap = new HashMap(); // Revokes access for children.

  /** Sets parent & grandparent maps. */
  @Override
  public final void setPropertyMap(Map propertyMap){
    super.setPropertyMap(propertyMap);
    this.propertyMap.clear();
    this.propertyMap.putAll(propertyMap);
  }
}

Is there a better way to achieve this goal?

Note: This is just an example of a real problem: how to restrict access to protected fields without retaining multiple copies?

Note: I also know that if the field is private first and has protected visitors, then this will be a non problem Sadly, I can't control it

Note: is-a relationship (inheritance) is required

Note: this can be easily applied to any collection, dto or complex object

Metaphors that misunderstand this question:

This is similar to grandparents having a cookie jar. They can make all family members and anyone else in their family (protected) Parents with children enter the house, and for their own reasons, they want to prevent their children from entering the cookie jar in case of nausea Instead, children should ask their parents about chocolate biscuits and see them magically appear; It's also sugar cookies or Oreos They never need to know that cookies are stored in the same jar, or even a jar (black box) This can be easily done if the jar belongs to the parents, if the grandparents can be persuaded to put away the cookies, or if the grandparents themselves do not need access If two identical cans are not created and maintained, how can children's access be restricted while parents and children are unhindered grandparent?

Solution

This may not be possible for you, but what if you can derive an interface from thirdpartyclass and let thirdpartyclass implement it?

Then, we delegate the private member thirdpartyclassimpl to implement the interface, and make mybaseclass act as a decorator

Namely

public interface ThirdParty ...


public class ThirdPartyClass implements ThirdParty



public class MyBaseClass implements ThirdParty {

    private ThirdParty decorated = new ThirdPartyClass();



 public class SubclassOne extends MyBaseClass....

wait

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>