interface

How to create and implement interfaces for operations that are only sometimes async

旧时模样 提交于 2019-12-12 17:44:31
问题 Let's say I have 100s of classes that implement a common interface with a method "calculate". Some of the classes will execute async (e.g. read a file), and other classes implementing the same interface will execute code that is sync (e.g. adding two numbers). What is a good way to code this, for maintenance and for performance? The posts I read so far, always recommend to make async/await methods bubble up to the callers. So if you have one operation that is async, make the caller async,

Should this property be part of my object's interface?

放肆的年华 提交于 2019-12-12 16:36:54
问题 I have a property called "IsSecureConnection" that is part of my object's interface. This makes sense for most implementations of the interface, however, in some implementations I would like to make the property ReadOnly. Should I omit this property from the object's interface even though it is required by all of the implementations (though slightly different on occasion)? Thanks! 回答1: It really depends on what's most readable for your clients. I can think of a couple of options: 1) The

How am I able to create A List<T> containing an open generic Interface?

爷,独闯天下 提交于 2019-12-12 16:36:07
问题 I have a List which must contain IInteract Objects. But IInteract is a generic interface which requires 2 type arguments. My main idea is iterate through a list of Objects and "Interact" one with another if they didn't interact yet. So i have this object List<IObject> WorldObjects = new List<IObject>(); and this one: private List<IInteract> = new List<IInteract>(); Except I can't compile the last line because IInteract requires 2 type arguments. But I don't know what the arguments are until I

Instantiate Java classes which implements specific Interface using reflection

血红的双手。 提交于 2019-12-12 16:11:02
问题 I am new to Reflection. I have seen some of the questions and tutorials. Let's assume I have one interface which's implemented by 3 classes A,B,C public interface MyInterface { doJob(); } Now using reflection I want to invoke each class Class<?> processor = Class.forName("com.foo.A"); Object myclass = processor.newInstance(); Rather than creating an Object, can't I restrict the whole process to specific type. I want to invoke only MyInterface type classes. If I pass com.foo.A it should create

Exception handling and interfaces in Java

穿精又带淫゛_ 提交于 2019-12-12 15:23:19
问题 I have written an interface and an implementation to it and I can't really decide on the best way to handle the following problem. To make it short, let's say this is the interface: public interface Node { String getNodeName(); Node getChildByName(String name); void addChild(Node node); void removeChild(Node node); } Now if I wanted to handle exceptions, is it a good idea to make the exception as generic as possible or should I add rather specific ones? Let's say the method addChild , should

How can I cast an Interface as its type in c#?

对着背影说爱祢 提交于 2019-12-12 15:18:05
问题 I have a property that returns an interface. During debugging I can break on what was returned and while it is the interface, Visual Studio is smart enough to know the derived type that it actually is. I assume it's using reflection or something. I'm not sure. My question is, can I have that same info available to me at runtime so I can create a variable of the appropriate type and cast the interface as that? Here is what I am saying: IPreDisplay preDisplay = cb.PreDisplay; If preDisplay is a

Read only and write only automatic properties in Interface

天大地大妈咪最大 提交于 2019-12-12 15:11:34
问题 I had read that auto implemented properties cannot be read only or write only. They can only be read-write. However, while learning interfaces I came across foll. code, which creates a read only / write only and read-write type of automatic properties. Is that acceptable? public interface IPointy { // A read-write property in an interface would look like: // retType PropName { get; set; } // while a write-only property in an interface would be: // retType PropName { set; } byte Points { get;

How to extend an interface by keeping the method names?

喜欢而已 提交于 2019-12-12 14:25:22
问题 Given two interfaces: interface I1 { int Foo(); } interface I2 { void Foo(); } And a class: class Test : I1, I2 { int I1.Foo() { Console.WriteLine("I1.Foo"); return default(int); } public void Foo() { Console.WriteLine("I2.Foo"); } } How can I extend the interface I2 with I1 by keeping the methods named Foo ? I tried the following code but it doesn't compile: interface I1 { int Foo(); } interface I2 : I1 { void I2.Foo(); } class Test : I2 { /* same code */ } 回答1: It is unclear in the example

Parameter of type X must support interface Y

一个人想着一个人 提交于 2019-12-12 14:13:52
问题 I have a setup like so : IBuilder = interface(IInvokable) end; IBuilder<T: IBuilder; TOut : TWinControl> = interface(IInvokable) end; TBuilder<T: IBuilder; TOut : TWinControl> = class(TInterfacedObject, IBuilder, IBuilder<T, TOut>) end; TBuilder = class(TBuilder<TBuilder, TWinControl>) end; This kind of structure allows me to build a sugar syntax like so : TBuilder<T : IBuilder; TOut : TWinControl> = class(TInterfacedObject, IBuilder, IBuilder<T, TOut>) function Output : TOut; function Name

Parameterize a generic interface - Create a Dictionary<Type, Interface<T>>?

走远了吗. 提交于 2019-12-12 13:54:15
问题 Sorry the title is confusing, I don't know if I said it right, not sure what this thing is called... Feel free to edit it after you read the question if you want. I'm refactoring my old code when I noticed there are a lot of places where I could use the Strategy pattern. I have an inventory system, and items - There are more than one ways to add an item - Normal way, forceful way, etc. You could also swap items, again, with more than one way - So I figured those are good places to use that