Re: Proper design of classes



"his is a bit like the interface approach offered
earlier, but it doesn't require you to implement each interface in your
customer class"

The most response i have seen so far is i go with the class approach, and
none of them seem to provide detailed explanation about how its right.

Hi i appreciate your concern to solving the problem, but you seem to be
talking out of the problem scope. Is it wise to have different kinds of
class(es) for this scenario, if you must understand clearly, with interface,
you can stub a single instance of an object to multiple interfaces, so far
that object meets the contract. Interface is not a class, and its a very
powerful programming construct for dynamic polymorhism like this problem
area.


Can we all take a simple look at the .NET collection base classes and
interface,. I will give a very simple eye opener scenario here; and i hope
those who want to learn will learn :

1] IEnumerable is the base.
2] ICollection inherits from IEnumerable
3] IList inherits IEnumerable and ICollection
4] ArrayList implements from IList, ICollection, IEnumerable


Now my friends you can use an ArrayList as any of the following :

IEnumerable en = new ArrayList();
IList il = new ArrayList();
ICollection col = new ArrayList();

you have a single instance of ArrayList acting as different interfaces.

You can borrow these approach(es) for the problem at hand, i am not forcing
anyone, to embrace this superb approach but i am just concerned by how most
people tend to leave a cleaner approach for plumbing.

Follow this example again:


public interface IPrivateCustomer
{
string PrivateName { get; set; }
string PrivateAddress { get; set; }
}

public interface IBusinessCustomer
{
string CompanyName { get; set; }
}

public interface ICustomer : IPrivateCustomer, IBusinessCustomer
{
string FirstName { get; set; }
string LastName { get; set; }
}

public class ConcreteCustomer : ICustomer
{
public ConcreteCustomer()
{

}

public T GetCustomer<T>()
{
return ((T)(object)this);
}

public string FirstName
{
get
{
return "First Name";
}
set
{

}
}

public string LastName
{
get
{
return "Last Name";
}
set
{

}
}

public string PrivateName
{
get
{
return "Private Name";
}
set
{

}
}

public string PrivateAddress
{
get
{
return "Private Address";
}
set
{

}
}

public string CompanyName
{
get
{
return "Company Name";
}
set
{

}
}
}


Then with this example you can do any of the following :

ConcreteCustomer customer = new ConcreteCustomer();

IPrivateCustomer privateCustomer =
customer.GetCustomer<IPrivateCustomer>();
IBusinessCustomer businessCustomer =
customer.GetCustomer<IBusinessCustomer>();
ICustomer iCustomer = customer.GetCustomer<ICustomer>();


.



Relevant Pages

  • Re: Do you ever use reflection instead of OO?
    ... It's easy in Java to pass interfaces without actually implementing that interface on any object. ... Htmlizer(new HtmlTableIface() { ... public class MyHtmlTableEntry { ... public String toString() { ...
    (comp.lang.java.programmer)
  • Re: interface & abstract class variables
    ... and an interface. ... if you have a concrete class that extends an abstract ... variables have to the concrete classes depends on what you want to do ... public String otherStuff() { ...
    (comp.lang.java.programmer)
  • FUNDAMENTAL QUESTION 2:
    ... instance of the class with member variable that holds "Gary2". ... I would have expected that tc1 would have been an interface ... public String getName(); ... public String getName() ...
    (comp.lang.java.programmer)
  • RE: Newbie Qn - Interfaces as parameters and members
    ... "Mark R. Dawson" wrote: ... > Hi Steve, ... > concrete class that implements the interface, you don't care what type the ... >> public string sToLocal ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Abstract Base Classes vs Interfaces?
    ... an abstract class is the only choice. ... You can take any class and derive from it adding in an interface ... > doesn't have to be derived from any particular base class. ... I think ICollection is a good example of the ambiguity between ...
    (microsoft.public.dotnet.languages.vb)