Jump to content

How to prevent other's from creating instances from in interface?


Recommended Posts

I just noticed that I can drag an interface onto block diagram and create an instance of the interface.  But for most of the cases, I want the interface to be abstract.  Is there any way to prevent users from creating instance from the interface I provided?

image.png.87dfa7c801d8f3f30603255fc68b8709.png

Link to comment

No you cannot. 

Your simplified example, if the interface is truly abstract, is equivalent to trying to prevent someone from dropping a "empty string constant" on the block diagram. 

Fundamentally though, it would prevent you from using the direct cast to exercise the override, as shown in the bottom example.


image.png.da189c191bb874755effadee90e6a047.png

 

The only restriction is that one cannot create a DVR of the interface from outside the interface itself.
I have to admit that I have not investigated the consequence of such a pattern in the context of interfaces...

image.png.cc7330564ffa66d2721ec33873ba8bbe.png
 

  • Like 1
Link to comment
11 hours ago, Francois Normandin said:

The only restriction is that one cannot create a DVR of the interface from outside the interface itself.

The restriction on DVRs is the same for interfaces as for classes: it allows for constructor/destructor notation for by-reference entities. Please don't go hyper excited and think "Oh, this is the tool I should have been using all along." By-reference designs are fragile and prone to several classes of errors that by-value code cannot create. Stick to the wires where possible; use references only when nothing else serves! 🙂 Details here.

  • Like 2
Link to comment
14 hours ago, Q^Q said:

I just noticed that I can drag an interface onto block diagram and create an instance of the interface.  But for most of the cases, I want the interface to be abstract.  Is there any way to prevent users from creating instance from the interface I provided?

As Francois noted, no, you cannot prevent it. The default instances are useful in many sentinel and error handling situations -- think like NaN in floating point. The defaults can also serve a role similar to "null" but without all the dangers of open references. Details about design decisions of interfaces can be found here.

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.