限制对类中的实例变量仅允许特定方法访问

huangapple go评论72阅读模式
英文:

Restrict access to a instance variable to only selected method in that class

问题

我想限制或仅允许一个方法对成员palidromespalidromesFIFO进行更新,该方法名为addPalindromeWord,不允许其他任何方法对其进行更新。是否有办法实现这样的限制?

目标是在处理向集合添加任何新值时实现模块化。必须遵循一些基本逻辑。我不希望看到任何其他方法以抽象的方式直接影响集合。

static class MyPalindromes{
    private Set<String> palidromes;
    private Deque<String> palidromesFIFO;
    private int maxLenOfCache;
    public MyPalindromes(int maxCachingLength) {
        palidromes = new TreeSet<>(new MyComparator()); 
        palidromesFIFO = new ArrayDeque<String>();
        maxLenOfCache = maxCachingLength;
    }
    
    public void addPalindromeWord(String word) {
        palidromes.add(word);
        
        if (palidromesFIFO.size() == maxLenOfCache && !palidromesFIFO.isEmpty()) {
            palidromesFIFO.removeFirst();
        }
        if (maxLenOfCache > 0) {
            palidromesFIFO.addLast(word);
        }
    }
    
    public void filterAndAddOnlyPalindromes(List<String> words) {
        List<String> validWords = new ArrayList<>();
        for (String w : words) {
            if (isPalindrome(w)) {
                validWords.add(w);
            }
        }
        this.addPalindromeWords(validWords);  
    }
    public void addPalindromeWords(List<String> words) {
        for (String word : words) {
           // palidromes.add(validWords);   //<< DISALLOW this direct update to the set 
           this.addPalindromeWord(word); // << ONLY allow update through addPalindromeWord method
        }
    }
}
英文:

I want to restrict or limit update to members palidromes and palidromesFIFO to only one method, say addPalindromeWord, and don't let any other methods to update it. Is there a way to do that?

Target is to achieve modularity in handling the addition of any new value to the collections. Some basic logic has to be followed. I would not like to see any other method DIRECTLY affecting the collections in abstract manner.

static class MyPalindromes{
	private Set&lt;String&gt; palidromes;
	private Deque&lt;String&gt; palidromesFIFO;
	private int maxLenOfCache;
	public MyPalindromes(int maxCachingLength) {
		palidromes = new TreeSet&lt;&gt;(new MyComparator()); 
		palidromesFIFO = new ArrayDeque&lt;String&gt;();
		maxLenOfCache = maxCachingLength;
	}
	
	public void addPalindromeWord(String word) {
		palidromes.add(word);
		
		if (palidromesFIFO.size() == maxLenOfCache &amp;&amp; !palidromesFIFO.isEmpty()) {
			palidromesFIFO.removeFirst();
		}
		if (maxLenOfCache &gt; 0) {
			palidromesFIFO.addLast(word);
		}
	}
	
	public void filterAndAddOnlyPalindromes(List&lt;String&gt; words) {
		List&lt;String&gt; validWords = new ArrayList&lt;&gt;();
		for (String w : words) {
			if (isPalindrome(w)) {
				validWords.add(w);
			}
		}
		this.addPalindromeWords(validWords);  
	}
	public void addPalindromeWords(List&lt;String&gt; words) {
		for (String word : words) {
           // palidromes.add(validWords);   //&lt;&lt; DISALLOW this direct update to the set 
           this.addPalindromeWord(word); // &lt;&lt; ONLY allow update through addPalindromeWord method
        }
	}
}

答案1

得分: 1

你可以将你的集合嵌套在一个专用(可选的内部)class中,该class只有这些方法是公共的。然后外部类无法访问私有字段和方法,你的Collection就不会被其他方式修改。

但是,你还必须确保Collection不被返回,否则开发人员可以直接修改它。通过在内部类中委托任何纯读取集合的操作来实现此目标。

如果你将这个做得足够通用,你只需要一堆类来处理这个。实际上,围绕它构建一个小型库是一件不错的事情。

英文:

You can 'nest' your collection in a dedicated (optionally inner) class that only has these methods public. Then the outer class cannot access the private fields and methods, and your Collection is save from modification in other ways.

You must however also ensure, that the Collection is not returned, otherwise developers could modify it directly. Do this by delegating any purely reading operations from the collection in the inner class.

If you do this 'generic enough', you only need a bunch of classes to handle this. It's actually a nice thing to build a tiny library around.

huangapple
  • 本文由 发表于 2020年10月8日 15:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64257840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定