Results 1 to 10 of 10

Thread: Under what circumstances I need to create..Interface class

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Under what circumstances I need to create..Interface class

    Being new to OOP's and vb.net, I don't understand exactly the scenario or situation when I should be creating an interface class.

    I am designing a 'Rate' class and I know exactly all the properties and methods of this class . Then am I correct in saying then I should goahead and start writing the implemention code for all these methods. I am assuming I don't have to worry about the Interface class.

    correct?

    thanks

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    interfaces are a thing and classes are other, there are no interface-classes

    the reason for existing interfaces is so that from a interface you can implement plugins(for example) knowing that each one that is inheriting from that interface will have that predefined set of methods/vars
    \m/\m/

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    In my application, I use interfaces in my data access layer. Basically, I have an IDavaProvider interface that defines all the method signatures that my various concerete data access classes need. Now, I have a two datasources: Access and SQL Server

    My SqlDataProvider and AccessDataProvider both implement the IDataProvider, but they are both responsible for adding their own implementation. And lastly, I have a DataProviderFactory class who returns the approriate interface for the dataprovider (which is dynamically set in web.config). Hope that helps and gives you a real-world example.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    I registered myself on your site today..looks good and when will it be active...

    I like the dark colors since they can be seen very clearly..

    Lethal,
    I am trying to get some example code (real world examples) for the INTERFACE. You mentioned that you created an interface at the dataaccess layer. could you kindly send me the interface and how your concrete classes take care of the implementation.
    I am the only programmer working on this assignment and absolutely no training and help. I am trying to get to speed on OOPS and vb.net.

    Thank you so much.

    regards
    [email protected]

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Interfaces are generally used for polymorphying. In other words if you have a set core of functionality that you want to impose on multiple objects then you would set the rules for the functionality as a required interface any object wanting to use the functionality must implement. An object that implements an interface can be cast to that interface type and passed into any function that accepts that interface type. Well anyway that all sounds confusing. Interfaces work like inheritance only the object must provide its own implementation.

    A .NET example is IList. Any class that implements IList or one of its derived/related interfaces (IListSource, IBindingList...) can be bound to a datagrid. That means that IList contains all the properties and methods that the DataGrid needs to display it and work with it properly. Any object that wants to work with the DataGrid needs to met those requirements or Implement the Interface.

    Nevermind I'm not explaining this very well.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436
    Thats good explanation Ed.

    I am trying to decide for my current situation at hand should I be implement an interface or should I go and construct a concrete class. Being new to OOPS, I am having hard time to decide which way/approach I should take.

    I should create a RATE class . A rate is a Loan rate for a specific term . say 15 Year fixed rate is 5.25%. Some times the rate could be more complex than that. And it is called as complex rate. A complex rate is composed of 2 or more simple rates.

    Both complex rate and simple rate have exactly same charecteristics. so I decided to make only one class called as 'RATE' class and complex rate would be have multiple instances of RATE class . I think I don't need to create a interface because the implementation for Complex rate or simple rate is exactly same.
    Except that the complex rate is a combination of 2 or more simple rates. So do you think I am heading in the right way.. I love the OOP's way of thinking..
    If complex rate implementation is different from simple rate, then I think I should create an Interface and then leave the implementation to simple rate and complex rate classess...
    whats your opininion Ed?
    thank you

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It depends on how you plan to code it. If you need or will need to branch them apart then you can use interfaces or an abstract class. If not then don't worry about it can just have the single concrete class. Do you have a reason for them to be two different types of objects in your code? If not just leave them the same. Worst case scenerio is that later you need to branch them at which time you could turn your original single Rate class into the base class for the two new specific classes.

    Generally interfaces are the same as an abstract class only an abstract class passes along structure and functionality and an interface only passes on structure. But the same principles would lead you to use either one. An example along your lines would be (can be used with either abstract/base class or interfaces):

    Say you have a LoanAdvisor object which performs calculations on different rates and it uses the getrate and term properties. now say that there are lots of different types of rates and all of them have at least getrate and term properties but some have more than that. In order for the LoanAdvisor to accept any type of rate object it would use an abstract class or interface that each rate object must use/implement so it can be figured in the LoanAdvisor. Then when you pass in the CarLoanRate, HomeLoadRate, OtherLoanRate objects they basically behind the scenes get cast to either IRate or RateBase and get used by the LoanAdvisor. Otherwise you'd have a whole lot of overloaded methods or pass in the generic object (which wouldn't ensure that it has the getrate and term properties needed).

    So I'd probably so skip the interfaces unless something about them really looks like something you need.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Kudos to Ed...

    thank you my friend....I was struggling to decide whether to use an abstract class/Interface/concrete class for my 'Rate' class. You have helped me to decide to Choose it as concrete class. I have made up my mind and I have every logical reason to design it as concrete class....Lets see
    how things work out and may be I don't have to change it to interface or an abstract class.

    thanks again

    regards

  9. #9
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here's an example of my data access layer. Also, when you registered on my site, your account is automatically activated.

    Code:
    using System;
    using VSForums.Components;
    using VSForums.IDataProvider;
    
    namespace VSForums.IDataProvider {
        /// <summary>
        /// Interface defining standard behavior for a user
        /// </summary>
        public interface IUser {
            /// <summary>
            /// Insert a user into the database
            /// </summary>
            CreateUserStatus InsertUser(UserInfo user);
            /// <summary>
            /// Update a user in the database
            /// </summary>
            void UpdateUser(UserInfo user);
        }
    }
    Code:
    using System;
    using System.Data;
    using VSForums.Components;
    using System.Data.SqlClient;
    using VSForums.IDataProvider;
    using System.Configuration;
    
    namespace VSForums.SqlDataProvider {
        /// <summary>
        /// Concrete user class encapsulating data access logic
        /// </summary>
        public class User : IUser {
            /// <summary>
            /// Private constants
            /// </summary>
            private const string SPROC_REGISTER = "Forum_UsersRegisterUser";
    	/// <summary>
    	/// Insert a new user into the database
    	/// </summary>
    	public CreateUserStatus InsertUser(UserInfo user) {
    	    return (CreateUserStatus)SqlHelper.ExecuteScalar(ConfigurationSettings.AppSettings["SqlConnectionString"], 
    	        SPROC_REGISTER, user.Username, user.Email, user.Password, user.Timezone, user.Skin);
    	}
    	/// <summary>
    	/// Update a user in the database
    	/// </summary>
    	public void UpdateUser(UserInfo user) {
    
    	}
        }
    }
    Code:
    using System;
    using System.Reflection;
    using System.Configuration;
    
    namespace VSForums.DataProviderFactory {
        /// <summary>
        /// Factory implementaion for the User DAL object
        /// </summary>
        public class User : DataProviderFactoryBase {
            /// <summary>
    	/// Obtains an interface reference for the dal
    	/// </summary>
    	/// <returns>DAL interface reference</returns>
    	public static IDataProvider.IUser Instance() {	
    	    return (IDataProvider.IUser)GetObjectFromFactory("User");
    	}
        }
    }
    Code:
    using System;
    using System.Reflection;
    using System.Configuration;
    
    namespace VSForums.DataProviderFactory {
        /// <summary>
        /// DAL factory base class
        /// </summary>
        public class DataProviderFactoryBase {
    	/// <summary>
    	/// Loads and returns the appropriate DAL from the factory
    	/// </summary>
    	/// <param name="className">Name of class to create</param>
    	/// <returns>DAL object</returns>
    	internal static object GetObjectFromFactory(string className) {
    	    /// Look up the DAL implementation we should be using
    	    string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
    	    className = path + "." + className;
    	    // Load the appropriate assembly and class based on the settings in the config file
    	    return Assembly.Load(path).CreateInstance(className);
    	}
        }
    }

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    thank You Lethal

    very kind of you.

    regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width