Add method to interface for only one implementation. Like this: So now the Producer can completely control what to do and when to do it, so it's a lot more difficult for the user to misuse.
The flush method passes on the buffered output, causing the buffer to be empty. But if the flush() operation merely ensures that the data is sent at the latest when this method is called, no problem: if the data is already sent, there's nothing more to do. Does Liskov Substitution Principle also apply to classes implementing an interface? If we step back to the design though, it is probably best to avoid too many latitudes like this; there has to be a good reason for allowing certain flexibilities, such as that it is significantly easier for certain variations of the object to be implemented, or there is some useful efficiency gain. What's the difference between a magic wand and a spell. Site design / logo 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. This can be an entirely reasonable design, depending on what flush() is supposed to do. I would call this interface Consumer, not Producer. Can climbing up a tree prevent a creature from being targeted with Magic Missile? If it doesn't, then either we cannot have that kind of test, or the test has to "just know" that certain objects flush inside produce, and others don't, and kind of work outside of the official protocol. Why do the displayed ticks from a Plot of a function not match the ones extracted through Charting`FindTicks in this case? You are putting data into is, it consumes them. Once the test works outside of the protocol, then it's becoming coupled to the implementation details. For example, this is important to ensure that the buffer has a fixed maximum size. How would electric weapons used by mermaids function, if feasible? In others word, if the implementation of a specific class can break the code that is using the interface you are in trouble. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Moreover, implementations of produce may pass on the output, as if they implicitly called flush before returning, in which case the implementation of flush can be a null operation. expecting that produce generates output instantly without calling flush, because it worked in one case with one kind of object.). Tannakian-type reconstruction of etale fundamental group. I'm not a downvoter, but this seems like a step backward - you can only flush output by terminating it? The fact that Java does it, it is not a guarantee. rev2022.7.21.42639. If the users don't read your documentation, or misunderstand what it says, they will misuse it. If clear error handling is desired, it could make sense to mandate that the produce() method must buffer, and that a separate send() method must be called later. It's pretty normal in many cases (including filesystems and networking) to want to flush after a batch of work (and usually only the caller knows what constitutes a "batch"). The Producer can flush as many times as needed in the above. The fact, that somebody does something does not mean it is "OK" per se. "Selected/commanded," "indicated," what's the third word? The protocol can specify deliberate latitudes about which method takes care of certain responsibilities, and those choices will be visible to a client which deviates from the protocol. How to help my players track gold in multiple currencies? In your case the behaviour of flush is entirely in line with the expectation of the interface user. Announcing the Stacks Editor Beta release! If the producer sends data right away, than the flush() method remains empty. Is this one method interface a good response to this interview question? Normally, a flush operation refers to the second kind: data may be buffered and might not have been sent before flush() is called, but the implementation is free to send the data earlier. In this specific case, we cannot have a generic test of the flush function that works for objects of all types. The specification of an object behavior can be given in terms of an interface contract that spans multiple methods; in effect a protocol. Is there a political faction in Russia publicly advocating for an immediate ceasefire? There are other solutions. To learn more, see our tips on writing great answers. If a creature's best food source was 4,000 feet above it, and only rarely fell from that height, how would it evolve to eat that food? Stack Exchange network consists of 180 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. There's two problems I would have with the above: One idea to improve upon this design, is not trying to tell the user how to control the Producer, but give control to it. You've satisfied the most basic requirement for substituability by ensuring that all the derived objects have all the required methods, in a way that is call compatible. One loads all data, and then bulk send it at once after it done, and second sends data right away (so it does not bulk send data, but whenever new data is created, it sends it right away). Is there a PRNG that visits every number exactly once, in a non-trivial bitspace, without repetition, without large memory usage, before it cycles?