I have a class library that i have built that consists of a number of classes that inherit from a single base class.
there is a virtual method defined on the base class called Process which essentially will process the content of the class structure into the application.
There is messaging going between different components (servers) of the application, and they are passing and receiving messages of the base class type, and then calling Process()
Where i have gotten stuck is that i need to be able to override the Process() method in the other parts of the application, i.e. in different assemblies without deriving another type from it.
From what i have read this seems not possible, so looking at if using interfaces would solve the issue?
EDIT:
What i failed to mention in the question is the each derived class of the base class needs to have a different implementation of Process() depending on the functionality required by that class.
For example:
class First : BaseClass
{
override void Process()
{
//do something
}
}
class Second : BaseClass
{
override void Process()
{
//do something else
}
}
the code in the override process methods need to be in different assemblies, due to the fact that this code will be referencing other external sources based on the class it belongs to that i do not want to propagate across the entire application.
Just for clarification i do have access and can change the library class if required.
I have been doing some further reading and it would appear using delegates could solve this issue.. thoughts?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Interfaces, specifically the Strategy Pattern would solve your issue.
public interface IProcessStrategy
{
void Process();
}
public class ProcessService
{
public void DoProcess( IProcessStrategy strategy )
{
...whatever
strategy.Process();
...whatever else
}
}
By separating the processing to its own abstraction, you allow creating multiple strategies and technically it doesn't matter if a strategy is defined in the same or a different assembly.
Update: I guess I finally know what you are up to and my suggestion is to have an interface you can inject but still have a default implementation.
public interface IProcessStrategy
{
void Process();
}
public class BaseClass
{
private IProcessStrategy _strategy;
public void Process()
{
// this is where the real "override" happens
if ( _strategy == null )
{
// default implementation
...
}
else
// overridden
_strategy.Process();
}
public void OverrideWith( IProcessStrategy strategy )
{
this._strategy = strategy;
}
The way you use it:
BaseClass f = new BaseClass();
f.Process(); // uses the default implementation
and somewhere else
BaseClass f = new BaseClass();
IProcessStrategy p = new First();
// override
f.OverrideWith( p );
f.Process(); // overridden with a new implementation
Note then that the overridden implementation, the First
can be defined anywhere, including a different assembly, it just has to implement the common interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With