限制对类方法的访问
我有一个类 A,它具有公共方法,并由在不同应用程序中实现的 100 个其他类使用。现在我想将这些公共方法设置为私有方法,以便没有新的类访问它们,但我希望现有的客户端类仍然可以访问它们。 但我什至不想碰那些客户类,因为所有者很少允许他们的类出现任何波动。
我检查了
C++:有吗一种限制对某些类的某些方法的访问而不暴露其他私有成员的方法?
但所有(并非所有)都要求更改客户端代码。客户端代码不应更改。
一种直接的方法是让所有 N 类成为朋友,但我不太愿意这样做。是否有任何模式或可接受的技术(请不要破解)来实现此访问限制? 谢谢您,如果这是重复的,我深表歉意。
I have a class A which has public methods and used by 100 other classes implemented in different applications. Now I want to make those public methods as private so that no new classes access them, but I want the existing client classes to still access them.
But I don't want to even touch those client classes , because the owners seldom allow even any ripple in their classes.
I checked
Can I access private members from outside the class without using friends?
friend class with limited access
But all ( not all really ) demand a change in the client's code. The client code should not change.
One straight forward way is to make all those N classes friends , But I am somewhat not comfortable doing that. Is there any pattern or an acceptable technique ( not a hack please ) to achieve this access restriction?
Thank you and I apologize if this is a duplicate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++ 中的类被成为
友元
,以表明类之间存在特殊的强耦合。friend
的这种使用实际上增强了封装性,而不是像流行的感觉那样破坏它。怎么办?
如果没有友谊,向其他类公开功能的唯一非黑客方法是提供
public
、get
和set
成员函数,这在事实上打破了封装,因为所有类(甚至那些不需要的类)现在都可以访问这些方法,因此成员增加了潜在破坏类数据的风险。回到您的情况,如果您有 100 个类需要访问该特定类,那么您已经通过将这些方法设置为
public
来进行正确的设计。现在尝试使这些方法对未来的类私有
是一种尝试破解您现有的设计,您的设计不支持它。将现有类设为
friend
并不完全符合上述标准,因此对于该场景来说不是一个好的选择。但是,鉴于这种情况,您没有其他方法可以实现此目的。将现有类设为
friend
并授予它们特殊访问权限似乎是唯一的方法。这仍然很糟糕,因为只能访问少数方法的 100 个类现在可以访问整个类。Classes in C++ are made
friend
s in order to indicate an special intentional strong coupling between classes. This use offriend
infact enhances Encapsulation rather than break it as maybe the popular feeling.How?
Without friendship the only non-hack way to expose the functionality to other class would be to provide
public
,get
andset
member functions,this in fact breaks encapsulation because all classes(even those who don't need to) now have access to these methods and hence the members increasing the risk of potentially breaking down the class data.Back to your situation, If you have a 100 classes which need access to this particular class, then you already had the right design in-place by having those methods as
public
. Now trying to make those methodsprivate
to future classes is a trying to hack your existing design, Your design does not support it.Making the existing classes as
friend
s does not ideally fit in the above mentioned criteria and hence is not a good choice for the scenario.However, given the situation there is no other way in which you can implement this. Making the existing classes as
friend
and granting them the special access seems the only way. This is still bad because the 100 classes which only had access to the few methods will now have access to your entire class.我认为你可以提取
A
类的接口(让它成为IA
)并让A
实现IA
>。您根本不应该在IA
中定义这些公共方法。然后,旧代码将继续使用
A
并可以访问A
公共方法,而新代码将使用受限接口,该代码将通过某些 Fabric 接收。当然,如果您需要(复制)构造类或类似的东西,这可能是无法实现的,但我现在不能在不知道类的用法的情况下说出来。
此外,由于
虚拟
函数,您会获得一些开销I think you can extract an interface of the
A
class (let it beIA
) and makeA
to implementIA
. You should not define those public methods inIA
at all.Then, old code will continue using
A
and will have access toA
public methods, while new code will use restricted interface, that code would receive through some fabric .Of cause, this can be unimplementable, if you need to (copy-)construct class, or smth like this, but I can't say it now without knowing the usage of class.
Also, you get a little overhead due to
virtual
functions