Extracting Interface hide specifics of member if that's an interface too

extract interface visual studio
extract method refactoring
move method refactoring
ruby class extraction
extract class refactoring
replace parameter with method
how to create interface in intellij
extract superclass refactoring

I have this class Project which I want to extract an interface out of, since we're going to work with different types of projects. One of the properties of Project however is called Data:

class Project {
    public Data D {get;}
    public void SomeSpecificMethodReferencingData() 
    { 
      D.SomeSpecificMethod(); 
    }
}

class Data {
   public void SomeGenericMethod() { }
   public void SomeSpecificMethod() { }
}

Now, Data needs to be extracted as an interface as well (but only defining the SomeGenericMethod(). I am currently using this:

interface IProject {
    IData D {get;}
}

interface IData {
    void SomeGenericMethod();
}

class Data : IData {
    public void SomeGenericMethod() { }
    public void SomeSpecificMethod() { }
}

class OtherData : IData {
    public void SomeGenericMethod() { }
    public int SomeOtherSpecificMethod(float someArgument) { }
}

class Project : IProject {
    public IData D { get; }
    public void SomeSpecificMethodReferencingData()
    { 
      D.SomeSpecificMethod(); // this does not work!
      (D as Data).SomeSpecificMethod(); // this looks stupid!
    }
}

class OtherProject : IProject {
    public IData D { get; }
    public void SomeOSpecificMethodReferencingOtherData()
    {
       var i = D.SomeOtherSpecificMethod(14.0f); // this does not work!
       var i = (D as OtherData).SomeOtherSpecificMethod(14.0f); // this looks stupid!
    }
}

The problem I run into is that within the Project class, I reference the SomeSpecificMethod. When I extract an interface for Data however, I first have to cast it from IData to be able to reference the specific method. This is not preferrable as the IData ALWAYS is an instance of Data for this Project. When I build my OtherProject I will create an OtherData for it, so to get pairs of xxxProject and xxxData implementations.

Is there a design pattern of some sort to help me building pairs of related and referenced classes? Like the Abstract Factory, but better?

You may use generics for that:

interface IProject<T> where T: IData {
    T D {get;}
}

Now your different projects are this:

class Project : IProject<Data> 
{
    public Data D { get; }
    public void SomeSpecificMethodReferencingData()
    { 
        D.SomeSpecificMethod();  // D is of type Data
    }
}

class OtherProject : IProject<OtherData> {
    public OtherData D { get; }
    public void SomeOSpecificMethodReferencingOtherData()
    {
         D.SomeOtherSpecificMethod(14.0f); // D is of type OtherData
    }
}

Extract Interface, If it's planned to eventually allow use of servers of multiple types, all servers must implement the interface. Good to Know. There's a certain resemblance between  Extracting an interface allows isolating only common interfaces, not common code. In other words, if classes contain Duplicate Code , extracting the interface won’t help you to deduplicate. All the same, this problem can be mitigated by applying Extract Class to move the behavior that contains the duplication to a separate component and

you can have 3 separate interfaces:

interface IData:ISpecificData, IGenericData
    {    
    }
    interface ISpecificData
    {
        void SomeSpecificMethod();
    }
    interface IGenericData
    {
        void SomeGenericMethod();
    }

you can use only one of the interfaces when you need only one of the methods, and use IData when you need both

Extract Method, Solution: Move this code to a separate new method (or function) and replace the old void printOwing() { printBanner(); // Print details. If the variables are declared prior to the code that you're extracting, you will need Switch Statements · Temporary Field · Refused Bequest · Alternative Classes with Different Interfaces. HAL interface definition language or HIDL (pronounced "hide-l") is an interface description language (IDL) to specify the interface between a HAL and its users. It allows specifying types and method calls, collected into interfaces and packages. More broadly, HIDL is a system for communicating between codebases that may be compiled independently.

In class Project you are referencing SomeSpecificMethod of Data class.

You are NOT using anything from IData interface - so using IData in Project class has no sense.

It is possible to do it as in your examples, and it will work - but it is "wrong way".

  (D as Data).SomeSpecificMethod(); // this looks stupid!

It looks stupid - but from other reasons. What if D is not Data, but OtherData ? It will be perfectly correct at compile time, but it will blow up during runtime!

Hiding Behind the Keyboard: Uncovering Covert Communication , Uncovering Covert Communication Methods with Forensic Analysis Brett will be discussed in more detail in Chapter “Mobile Extraction Issues,” so for now, If you have no stored picture, look at your background image on your home screen. user interface (GUI) to get to specific logical data at the swipe of your finger. 1. Here are 35 best free steganography software. These steganography programs let you hide data (textual or files of various formats) in image files as well as in various types of media files. All these steganography software are available free to download for your Windows PC. These have various features, like: hide typed text in images, hide TXT

Metamodeling-driven IP Reuse for SoC Integration and , Extracting interface functions requires worrying about polymorphic function If the submodules of a module are templated, then during the extraction of the module constructs that need to be mined to identify and extract the hidden metadata. Interface Metaphors, Interface Objects, Interface Actions, Interface Icon, Interface Templates Interface Metaphors Concepts from the real world that are used to model a computer system; meant to help user predict actions

Pull members up, push members down, The Pull Members Up refactoring lets you move fields if two classes have the class members to a superclass or an interface, or interface to superinterface. -An interface does not contain any constructors.-All of the methods in an interface are abstract.-An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.-An interface is not extended by a class; it is implemented by a class.-An interface can extend multiple interfaces

Extract interface - Help, copy refactorings · Pull members up, push members down · Remove middleman As a result, an interface will be created containing the specific methods and fields. Here we have a class, and perform Extract Interface refactoring to create an If IntelliJ IDEA shows you a Refactoring Preview in the Find tool window,  Unless you explicitly set a class and its members to false, inherited classes can expose to COM base class members that are invisible in the original class. For example, if you set ClassA to false and do not apply the attribute to its members, the class and its members are invisible to COM. However, if you derive ClassB from ClassA and export ClassB to COM, ClassA members become visible base class members of ClassB.

Comments
  • Can you please show the troublesome code? The problem you are talking about is not clear.
  • If all your instance of IData actually are Data-objects, why does the interface contain only one member of that class? So either make another interface for that member and cast to that, or add the member to IData as well.
  • Did you consider using generics? Like interface IProject<TData> where TData: IData or something similar? Then you could have class Project : IProject<Data> ...
  • You are referencing SomeSpecificMethod. The point is this method is not defined in your interface IData. Only the concrete implementation has this method defined. That's why you have to cast to the implementation to access this method. Add the method SomeSpecificMethod to the interface as well.
  • Have you thought about having in each xxxProject: private xxxData MyD => (xxxData)D; - Also not very beautiful. The other thing I was thinking was: Could there be a conceptual problem, here? Is the logic behind SomeSpecificMethod really a concern of Data or is more specific to how Project should handle Data? So, maybe other patterns should be considered, too? Like strategy pattern, factory, ...
  • @BionicCode because...? From my point of view we need the specific stuff only within the specific project, which knows the type of data it belongs to.
  • @BionicCode That´s not the problem, unless OP wants to use the SomeSpecificMember outside of Project, which he/she does not.
  • @BionicCode No, he is just not aware of Generics. That's why he used IData as member type. And that's why he has that problem in the first place.
  • @BionicCode Obviously OP does not want to pollute IData with stuff specific to Data.
  • @Henri Yes, you have to know the projects Data-type at compile-time then. Or create a non-generic base-interface from which IProject<T> derives from
  • Or (maybe better?) to have 2 interface - IData (with SomeGenericMethod) and ISecificicData (with SomeSpecificMethod) that inherits IData ?
  • And how would that solve the problem of IProject?
  • How would that solve a OtherProject.D.SomeOtherSpecificMethod(float)? Notice the parameter for SomeOtherSpecificMethod.
  • How does that solve OP's problem?
  • "What if D is not Data, but OtherData ? It will be perfectly correct at compile time, but it will blow up during runtime!" I'm aware of that, so ideally I want to use Data as property, but then I'm not implementing the interface anymore (it needs to implement IData since I need the IProject.D.SomeGenericMethod call)