英文:
Restrict access to a instance variable to only selected method in that class
问题
我想限制或仅允许一个方法对成员palidromes
和palidromesFIFO
进行更新,该方法名为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<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
}
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论