Re: Proper design of classes
- From: Ahmed Salako <AhmedSalako@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 24 Jul 2008 03:32:01 -0700
"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>();
.
- References:
- RE: Proper design of classes
- From: Ahmed Salako
- Re: Proper design of classes
- From: Leon Jollans
- RE: Proper design of classes
- Prev by Date: Re: Vista development, the do's and don't???
- Next by Date: Re: Getting information from IEnumerable
- Previous by thread: Re: Proper design of classes
- Next by thread: Re: Proper design of classes
- Index(es):
Relevant Pages
|