Remoting references by wrapping them in a MarshalByRefObject? Will it work? - plugins

I'm trying to implement a plugin system using MAF. The objects I want to pass currently aren't serializable though, and even though I'm sure I could make them serializable I don't think it would be very performant.
Two questions:
1) In order for MAF to pass the actual references across, does an object simply need to inherit from MarshalByRefObject or is there more to it than that?
2) Could I wrap my class in an object that inherits from MarshalByRefObject to get the reference across?
EDIT: Obviously the problem itself has little to do with MAF, but I just wanted to include some context in case someone could point me in an altogether direction to go.

1) Yes, it just needs to inherit from MarshalByRefObject, but any public types inside the class also need to be serializable or inherit from MarshalByRefObject as well.
2) No, this just pushes the problem back since the class still needs to be serializable or inherit from MarshalByRefObject.

Related

How to implement a KryoSerializer for an interface where all implementing classes are private?

I'm trying to write a class to implement KryoSerializer so that I can serialize objects for use with Spark. The issue I'm having is that while all of the classes implement a public interface, all of the implementing classes are private. Kryo doesn't seem to want to allow me to define a serializer for either a private out-of-package class or an interface.
The way this issue manifests is that when I attempt to define the KryoSerializer class, I get an error that class [implementation] in package graph cannot be accessed in package [same package].
What I'm hoping someone can help with, is a strategy for solving this issue.
I understand the reasons why Kryo wants to serialize and deserialize concrete objects. But, in this case, since I am defining my own KryoSerializer anyway, it actually would make more sense to define serialization for the interface. Is there a way to trick Kryo into doing the right thing?
(The reason this will work, is that there is a related Object that has functions to take an instance with the interface and write or read from a stream. My serializer would essentially wrap those functions while adding some serialization format version information.)
One possibility I thought of is the reflection trick. When deserializing a class with private/final members, we sometimes use reflection to make the private member accessible and writable, set the value, then set it back to private/final. I'm not sure if its possible to do that for a private class in another packager, but even if it is, it seems rather ugly and inefficient.
Another possibility would be to define new classes that extend the private classes, along with a set of implicits to convert among them. That would also be rather ugly though, for a few reasons, and there are quite a few private classes at issue.
Can anyone suggest an approach? Advise regarding pitfalls I should avoid?

What's the correct way of thinking C# protected accessor in swift?

In c# we have the protected accessor which allows class members to be visible on inherited clases but not for the rest.
In Swift this doesn't exist so I wonder what's a correct approach for something like this:
I want to have a variable (internal behavior) and and a public method using this variable on a base class. This variable will be used also on inherited clases.
Options I see
Forget about base class and implement variable and methods everywhere I need it. WRONG, duplicated code
Implement inheritance by composition. I'd create a class containing common methods and this will be used by composition instead of inheritance. LESS WRONG but still repeating code that could be avoided with inheritance
Implement inheritance and make variable internal on base class. WRONG since exposes things without any justification except allowing visibility on inherited clases.
Implementation Details for Base Class
I want to have a NSOperationQueue instance and and a public method to cancel queued operations. I add new operations to this queue from inherited classes.
In Swift the correct answer is almost always protocols and extensions. It is almost never inheritance. Sometimes Cocoa stands in our way, because there are classes in Cocoa more often than protocols, but the goal is almost always protocols and extensions. Subclassing is our last choice.
Your particular case is confusing because NSOperationQueue already has a public method to cancel queued operations (cancelAllOperations). If you want to protect the queue from outside access (prevent callers from using addOperation directly for instance), then you should put the queue inside another type (i.e. composition), and forward what you want to the queue. More details on the specific problem you're solving would allow us to help suggest other Swift-like solutions.
If in the end you need something that looks like protected or friend, the correct solution is private. Put your subclass or your friend in the same file with the target, and mark the private thing private. Alternately, put the things that need to work together in a framework, and mark the attribute internal. The Swift Blog provides a good explanation of why this is an intentional choice.

Choose between abstract class and interface

Among my two processes' functionality, there is a common function to merge files. I need not going to insist any of the processes to have some methods as interface does. And, also the two processes are independent. So, is it fine I just go with an Abstract class and have the implementation in that abstract class itself? Also I do not need any abstract method.
Inheritance is used when there is IS-A relation between subclass and the base class. I don't think it is the case here. You didn't specify the language, but from your profile I guess you use Java. So if you use an Abstract Class you won't be able to inherit from other, more appropriate class in the future.
Instead of inheritance you can use composition. Which means that you create a regular file merging class which has this method to merge files. And in classes where you want to have this functionality you just instantiate this new file merging class. It lets you inherit from other class in the future.
If you want to inform the world that those classes can merge files (to use polymorphism), and you use Java 8 you can create default method inside an interface and implement this interface without override this default method. But I think composition will be better in this case.

Is there a way for a Scala base class to get informed when all the derived classes have been constructed? (guess not)

First: why on Earth am I asking this?
I'd like to have a working 'invariant' check system with my classes, and this would allow a nice way to make it happen. Each level could provide their invariants (if any) and the base class would execute them at the end of the construction chain.
Similar question on Java: Running a method after the constructor of any derived class
Maybe this will help: Running code after subclass initialization

Pseudo-multiple-inheritance with extension methods on interfaces in C#?

Similar question but not quite the same thing
I was thinking that with extension methods in the same namespace as the interface you could get a similar effect to multiple inheritance in that you don't need to have duplicate code implementing the same interface the same way in 10 different classes.
What are some of the downsides of doing this? I think the pros are pretty obvious, it's the cons that usually come back to bite you later on.
One of the cons I see is that the extension methods can't be virtual, so you need to be sure that you actually do want them implemented the same way for every instance.
The problem that I see with building interface capability via extension methods is that you are no longer actually implementing the interface and so can't use the object as the interface type.
Say I have a method that takes an object of type IBar. If I implement the IBar interface on class Foo via extension methods, then Foo doesn't derive from IBar and can't be used interchangeably with it (Liskov Substitution principle). Sure, I get the behavior that I want added to Foo, but I lose the most important aspect of creating interfaces in the first place -- being able to define an abstract contract that can be implemented in a variety of ways by various classes so that dependent classes need not know about concrete implementations.
If I needed multiple inheritance (and so far I've lived without it) badly enough, I think I'd use composition instead to minimize the amount of code duplication.
A decent way to think about this is that instance methods are something done by the object, while extension methods are something done to the object. I am fairly certain the Framework Design Guidelines say you should implement an instance method whenever possible.
An interface declares "I care about using this functionality, but not how it is accomplished." That leaves implementers the freedom to choose the how. It decouples the intent, a public API, from the mechanism, a class with concrete code.
As this is the main benefit of interfaces, implementing them entirely as extension methods seems to defeat their purpose. Even IEnumerable<T> has an instance method.
Edit: Also, objects are meant to act on the data they contain. Extension methods can only see an object's public API (as they are just static methods); you would have to expose all of an object's state to make it work (an OO no-no).