Click here to Skip to main content
15,891,749 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: Base class method access VS. abstract class Method access Pin
Richard MacCutchan9-Jan-14 4:43
mveRichard MacCutchan9-Jan-14 4:43 
AnswerRe: Base class method access VS. abstract class Method access Pin
Shameel10-Jan-14 3:36
professionalShameel10-Jan-14 3:36 
GeneralRe: Base class method access VS. abstract class Method access Pin
netfed12-Jan-14 0:51
netfed12-Jan-14 0:51 
GeneralRe: Base class method access VS. abstract class Method access Pin
Ron Beyer12-Jan-14 4:33
professionalRon Beyer12-Jan-14 4:33 
GeneralRe: Base class method access VS. abstract class Method access Pin
netfed12-Jan-14 6:17
netfed12-Jan-14 6:17 
GeneralRe: Base class method access VS. abstract class Method access Pin
Ron Beyer12-Jan-14 6:40
professionalRon Beyer12-Jan-14 6:40 
GeneralRe: Base class method access VS. abstract class Method access Pin
Shameel12-Jan-14 4:54
professionalShameel12-Jan-14 4:54 
AnswerRe: Base class method access VS. abstract class Method access Pin
Keld Ølykke9-Jan-14 14:33
Keld Ølykke9-Jan-14 14:33 
Hi,

1) With an abstract class A you can define re-usable implementation for derived classes. This is a way of removing duplicate code in multiple derived classes.

2) With an abstract class you can defer implementation to derived classes e.g. defining abtract methods or properties. Why would you do that? Well you can call the abtract definition from implementation in the abstract class.

3) With an abstract class you can let implementation code be extended via overrides

4) With an abstract class you can declare collections of the abstract class, but add derived classes to the collection.


I will try to make an example to illustrate above features:

C#
abstract class A
{
  // 2) A doesn't store the label content - a derived must
  abstract string Label {get;};

  string ToLabel()
  {
    // 1) Implementation centralized - not in all derived classes
    return this.Label;
  }

  // 3) implementation that can be extended or even replaced
  virtual void Writeline()
  {
    Console.Out.Writeline();
  }
}

class X : A
{
  // 2) A says I must do this - I at least choose the content
  override string Label{ get{ return "I am X!"; }}
  
}

class Y : A
{
  // 2) A says I must do this - I at least choose the content
  override string Label{ get{ return "Me is Y!"; }}

  // 3) optionally extending implementation in abstract class
  override void Writeline()
  {
     Console.Out.Writeline(">>>"); // extra before base
     base.Writeline();
     Console.Out.Writeline("<<<"); // extra after base
  }
}

void SomeCode()
{
  // 4) Polymorphism - declaring a collection of abstract classes but adding instances of derived classes
  List<A> as = new List<A>();
  as.Add(new X());
  as.Add(new Y());
  foreach(A a in as) // 4 - accessing abstract implementation
  {
    Console.Out.Writeline(a.ToLabel); // 4 - no branching on implementation e.g. typeof(X) or typeof(Y)
  }
}


Lots of patterns make use of abstract classes e.g. http://en.wikipedia.org/wiki/Abstract_factory_pattern[^] and http://en.wikipedia.org/wiki/Composite_pattern[^].

Try play around with it and the different ways of calling up or down between abstract and concrete classes. It takes some getting used to and some redesign.
Some rules I try to stick to stay sane:
- (abstract class) only declare fields as private
- (abstract class) store a field for each non-abstract property (if a state)
- (abstract class) use protected properties/methods to serve derived classes (hidden from the public)
- (abstract class) always expect virtual methods/properties to be called by derived classes
- (derived class) always call base in overriden virtual method/property

I hope it helps.

Kind Regards,
Keld Ølykke
GeneralRe: Base class method access VS. abstract class Method access Pin
netfed12-Jan-14 1:16
netfed12-Jan-14 1:16 
GeneralRe: Base class method access VS. abstract class Method access Pin
Keld Ølykke12-Jan-14 8:06
Keld Ølykke12-Jan-14 8:06 
QuestionImplementation of Loose Coupling Pattern Pin
User 905247816-Dec-13 14:41
User 905247816-Dec-13 14:41 
AnswerRe: Implementation of Loose Coupling Pattern Pin
Antonio Ripa27-Dec-13 2:25
professionalAntonio Ripa27-Dec-13 2:25 
GeneralRe: Implementation of Loose Coupling Pattern Pin
jschell27-Dec-13 12:57
jschell27-Dec-13 12:57 
GeneralRe: Implementation of Loose Coupling Pattern Pin
Kornfeld Eliyahu Peter4-Jan-14 23:13
professionalKornfeld Eliyahu Peter4-Jan-14 23:13 
AnswerRe: Implementation of Loose Coupling Pattern Pin
Keld Ølykke1-Jan-14 10:02
Keld Ølykke1-Jan-14 10:02 
QuestionWhich is better architecture, also for security reasons Pin
nstk29-Nov-13 5:33
nstk29-Nov-13 5:33 
AnswerRe: Which is better architecture, also for security reasons Pin
Mycroft Holmes29-Nov-13 12:45
professionalMycroft Holmes29-Nov-13 12:45 
AnswerRe: Which is better architecture, also for security reasons Pin
jschell2-Dec-13 10:11
jschell2-Dec-13 10:11 
AnswerRe: Which is better architecture, also for security reasons Pin
Shameel6-Jan-14 4:02
professionalShameel6-Jan-14 4:02 
QuestionWeb browser issue Pin
iqtechways24-Nov-13 20:53
professionaliqtechways24-Nov-13 20:53 
AnswerRe: Web browser issue Pin
Pete O'Hanlon24-Nov-13 21:22
mvePete O'Hanlon24-Nov-13 21:22 
AnswerRe: Web browser issue Pin
Vishal Pand3y27-Nov-13 0:39
Vishal Pand3y27-Nov-13 0:39 
Questionfirst step of my project Pin
srinivas ruttala7-Nov-13 22:50
srinivas ruttala7-Nov-13 22:50 
AnswerRe: first step of my project Pin
Simon_Whale7-Nov-13 23:52
Simon_Whale7-Nov-13 23:52 
GeneralRe: first step of my project Pin
WuRunZhe22-Nov-13 16:25
WuRunZhe22-Nov-13 16:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.