Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6056149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:20:39+00:00 2026-05-23T08:20:39+00:00

Using interfaces won’t work because I want a single implementation. Using this solution would

  • 0

Using interfaces won’t work because I want a single implementation. Using this solution would end in a lot of redundant code because I plan on having quite a few sub classes (composition vs inheritance). I’ve decided that a problem-specific design solution is what I’m looking for, and I can’t think of anything elegant.

Basically I want classes to have separate properties, and for those properties to be attached at design time to any sub class I choose. Say, I have class ‘ninja’. I would like to be able to make arbitrary sub classes such as ‘grayNinja’ where a gray ninja will always have a sword and throwing stars. Then possibly ‘redNinja’ who will always have a sword and a cape. Obviously swords, stars, and capes will each have their own implementation – and this is where I can’t use interfaces. The closest solution I could find was the decorator pattern, but I don’t want that functionality at runtime. Is the best solution an offshoot of that? Where inside the Black Ninja class constructor, I pass it through the constructors of sword and throwingStar? (those being abstract classes)

haven’t coded in a while and reading hasn’t gotten me too far – forgive me if the answer is simple.

Edit: Answered my own question. I can’t mark it as ‘answer’ until tomorrow. Please let me know if there’s a problem with it that I didn’t catch. All the reading this problem forced me to do has been awesome. Learned quite a bit.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:20:40+00:00Added an answer on May 23, 2026 at 8:20 am

    Alright so mix-ins through extension methods are going to be my preferred route. I couldn’t figure out how to use dynamic proxies in vb.net (seemed to require libraries with lots of documentation that didn’t cover specifically what I needed). Dynamic proxies also seems to be a bit dirtier of a solution than using extension methods. Composition would have been what I defaulted to if the previous two didn’t work.

    So one problem with extension methods, is that the code gets a little dirtier if you want to hold variables. Not much though. Another problem is that all the extension methods must be defined in modules, so the code might look a little goofy to a new eye. I will solve this by defining my interface and module with the corresponding extension method in the same file.

    finally, here’s some sample vb.net code if you don’t want to see a full fledged example through the link.

    Imports System.Runtime.CompilerServices 'for extension methods
    
    Public Interface ISword
    End Interface
    Public Interface IThrowingStar
    End Interface
    
    Module ExtensionMethods
    
        <Extension()>
        Public Sub swingSword(ByVal hasASword As ISword)
            Console.WriteLine("Sword has been swung")
        End Sub
    
        <Extension()>
        Public Sub throwStar(ByVal hasAStar As IThrowingStar)
            Console.WriteLine("Star has been thrown")
        End Sub
    
    End Module
    
    Public Class RedNinja
        Inherits Ninja
        Implements IThrowingStar, ISword
    
        Public Sub New()
        End Sub
    
    End Class
    
    Public MustInherit Class Ninja
    
        private curHealth as Integer
    
        Public Sub New()
            curHealth = 100
        End Sub
    
        Public Function getHP() As Integer
            Return curHealth
        End Function
    
    End Class
    
    Module Module1
    
        Sub main()
    
            Console.WriteLine("Type any character to continue.")
            Console.ReadKey()
    
            Dim a As New RedNinja
            a.swingSword() 'prints "Sword has been swung"
            a.throwStar()  'prints "Star has been thrown"
    
            Console.WriteLine("End of program - Type any key to exit")
            Console.ReadKey()
    
        End Sub
    End Module
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I won't describe the project details or the framework I am using because the
Using interfaces is a very easy way to remove dependencies, but what happens when
We're using interfaces to represent entity classes in our domain model. We have concrete
Is there a runtime performance penalty when using interfaces (abstract base classes) in C++?
Is okay to break all dependencies using interfaces just to make a class testable?
I am trying to implement my persitent classes using interfaces. I have created the
Using online interfaces to a version control system is a nice way to have
I have previously written user interfaces using .NET and Windows Forms. I'm about to
I have seen arguments for using explicit interfaces as a method of locking a
What is the difference between using the Runnable and Callable interfaces when designing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.