none
How to design mail sending class with abstract class RRS feed

  • Question

  • Why?

    If you want to set the property called To then assign a value to it.  Why would you want to create a new class and override the property?

    Choosing a technology (abstract class or anything else) and then forcing your design to use it is a vey basic error.

    Sunday, September 13, 2015 5:34 PM

Answers

  • The purpose of abstract classes is it to be the basis of a class heirarchy.
    For example, there are 4 different but equalyl useable variants of DBConenction, DBCommand and DataAdapter abstract class.

    You assign variables to instances. Instances exist WAY past the class design step, so I don't quite get why you mix up class definition and use of instances like that.

    Who or what forces you to use abstarct class/override some property? What is your code? How is that a question regarding C#? Error: Insuficient context.

    • Edited by Christopher84 Sunday, September 13, 2015 6:36 PM
    • Marked as answer by Mou_kolkata Thursday, September 17, 2015 9:38 AM
    Sunday, September 13, 2015 6:35 PM

All replies

  • The purpose of abstract classes is it to be the basis of a class heirarchy.
    For example, there are 4 different but equalyl useable variants of DBConenction, DBCommand and DataAdapter abstract class.

    You assign variables to instances. Instances exist WAY past the class design step, so I don't quite get why you mix up class definition and use of instances like that.

    Who or what forces you to use abstarct class/override some property? What is your code? How is that a question regarding C#? Error: Insuficient context.

    • Edited by Christopher84 Sunday, September 13, 2015 6:36 PM
    • Marked as answer by Mou_kolkata Thursday, September 17, 2015 9:38 AM
    Sunday, September 13, 2015 6:35 PM
  • Choosing a technology (abstract class or anything else) and then forcing your design to use it is a vey basic error.

    Your logic is in error, and you don't understand OOP is the bottom line. You keep coming to the C# forum asking the questions when you have architectural design issues and how to use design patterns effectively.

    http://www.dofactory.com/net/design-patterns

    <copied>

    Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects.

    <end>

    Sunday, September 13, 2015 7:18 PM
  • i like to see a real life abstract class example. i like to see where abstract class fit?

    give me few good usage of abstract class with example and code. thanks

    Thursday, September 17, 2015 9:38 AM
  • My software has to be able to connect to a back-end 'email system' in order to send emails. This email system is entirely user-configurable and the user (currently) has the choice between SMTP and Exchange Web Services. But more options may be added in the future.

    I could write code such as (this isn't real code, btw - just pseudo-code).

    if(mode=STMP)
    {
       // Do special SMTP stuff
    }
    elseif (mode=EWS)
    {
       // Do special EWS stuff
    }

    But I would need to add logic like that all over the place within the code base. It would make maintenance a nightmare and if you added another email mode in the future you would need to locate all these places to add another condition.

    Far better to create some abstract object and descend special SMTP and EWS descendents from it (again - this is just pseudo-code);

    abstract class EmailSender
    {
       public string To {get;set;}
       public string Body {get;set;}
       // etc
       public abstract void SendEmail();
    }
    
    class EmailSenderSMTP: EmailSender
    {
       public string To {get;set;}
       public string Body {get;set;}
       // etc
       public override void SendEmail();
    }
    
    class EmailSenderEWS: EmailSender
    {
       public string To {get;set;}
       public string Body {get;set;}
       // etc
       public override void SendEmail();
    }

    NOW, all my email sending stuff for SMTP is in one class and all the stuff for EWS is in one class. If anything needs to change I don't have to hunt all over the code base for it.

    All the calling code can simply create the appropriate object (via a factory method) and not have to worry about the underlying mechanism.

    If I want to add another mode I just create a new descendent of EmailSender and implement. None of the calling code needs to change.

    Does that explain just one possible reason for using abstract classes?

    Thursday, September 17, 2015 9:49 AM