-3

In my project I have two classes: Article and News. Some fields in them are same. For example: Title, Text, Keyword, MemberID, Date.

I created an interface and put same field in that. Is it a correct?

interface ITextContext
{
    public int ID { get; set; }
    public int Title { get; set; }
    public string Text { get; set; }
    public DateTime Date { get; set; }
    List<Keyword> Keywords;

}

public class Article:ITextContext
{
    public int ArticleID { get; set; }
    public bool IsReady { get; set; }
}

public class NewsArchive:ITextContext
{
    public int NewsArchiveID { get; set; }
}
Codor
  • 17,447
  • 9
  • 29
  • 56
job 1393
  • 21
  • 1
  • 7
  • 1
    A [base class](http://stackoverflow.com/questions/15583896/how-to-decide-between-an-interface-or-base-class-for-an-new-implementation) would probably be more appropriate here, unless the behaviour of the members is going to be different between the `Archive` and `NewsArchive` classes – James Thorpe Dec 10 '14 at 12:08
  • That is partly correct, you will need to implement those interface members in each class. The interface is more of an instruction to the rest of the code that the class will contain the members defined in the interface. – gmiley Dec 10 '14 at 12:09
  • if change iterface to Base Class is it a correct? – job 1393 Dec 10 '14 at 12:09
  • Did you try to build this??? Because it won't build. Try it first! – L-Four Dec 10 '14 at 12:13
  • @L-Three . after your Comment Build it and get some error. :) – job 1393 Dec 10 '14 at 12:15
  • 1
    Of course. This is a basic principle of OO that if you inherit from an interface that you should implement it or make your class abstract. You don't need an interface, you need a base class. – L-Four Dec 10 '14 at 12:16

3 Answers3

0

That's OK, assuming you don't want to share any implementation details between the classes. For example, if you wanted to add a method to TextContext that would be used by both Article and NewsArchive, you'd want to inherit from a common base class instead:

public class TextContext
{
    public int ID { get; set; }
    public int Title { get; set; }
    public string Text { get; set; }
    public DateTime Date { get; set; }
    List<Keyword> Keywords;    

    public string SomeMethod()
    {
        return string.Format("{0}\r\n{1}", Title, Text);
    }
}

public class Article : TextContext
{
   ...
}
Alan
  • 2,962
  • 2
  • 15
  • 18
0

In the current implementation, the properties defined in ITextContext would have to be actually implemented in Article and NewsArchive to compile. This would be valid, but would not result in code reuse, which, on the other hand, is not the purpose of an interface.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • if use base class instead interface ,is it be based on design principles and oo? – job 1393 Dec 10 '14 at 12:27
  • This is subject to discussion and cannot be definitely answered. On one hand, an interface cannot share implementation. On the other hand, code reuse is not the primary purpose of inheritance. Sharing implementation via a base class is possible and valid in my opinion. – Codor Dec 10 '14 at 12:31
0

If you need to share only events, indexers, methods, and properties without implementation you should use Interfaces.

If you need to share some implementation you can use abstract classes in the same way you are doing with interface (An abstract class cannot be instantiated)

public abstract class TextContext
{
    public int ID { get; set; }
    public int Title { get; set; }
    public string Text { get; set; }
    public DateTime Date { get; set; }
    List<Keyword> Keywords;

   public int PlusOne(int a){
       return a+1;
   }

}

public class Article:TextContext
{
    public int ArticleID { get; set; }
    public bool IsReady { get; set; }
}

public class NewsArchive:TextContext
{
    public int NewsArchiveID { get; set; }
}

now when you initialize a new Article or NewsArchive you see fields,methods.. of the base class.

faby
  • 7,394
  • 3
  • 27
  • 44