PC Review


Reply
Thread Tools Rate Thread

Assume I create a class Customer is it advice to have a separate class to store addresses

 
 
Tony Johansson
Guest
Posts: n/a
 
      24th Jan 2011
Hello!

I read a program that created a Customer class with some basic information
such as name, birthdate telephone and so on and
then used a separate Address class to store the address information in the
Customer class.

I just wonder it that a good approach to create a separate class just for
that address to store the address object in the Customer. I would instead
store the primitive fields such as zip code,street name and city and thing
like that directly
in the customer class without using any separate class for this.

Give me a comment about what you opinion is ?

//Tony


 
Reply With Quote
 
 
 
 
Anders Eriksson
Guest
Posts: n/a
 
      24th Jan 2011
On 2011-01-24 15:34, Tony Johansson wrote:
> Hello!
>
> I read a program that created a Customer class with some basic information
> such as name, birthdate telephone and so on and
> then used a separate Address class to store the address information in the
> Customer class.
>
> I just wonder it that a good approach to create a separate class just for
> that address to store the address object in the Customer. I would instead
> store the primitive fields such as zip code,street name and city and thing
> like that directly
> in the customer class without using any separate class for this.
>
> Give me a comment about what you opinion is ?
>

What if the customer has multiple addresses?

// Anders
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      24th Jan 2011
"Anders Eriksson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>> I read a program that created a Customer class with some basic
>> information
>> such as name, birthdate telephone and so on and
>> then used a separate Address class to store the address information in
>> the
>> Customer class.
>>
>> I just wonder it that a good approach to create a separate class just for
>> that address to store the address object in the Customer. I would instead
>> store the primitive fields such as zip code,street name and city and
>> thing
>> like that directly
>> in the customer class without using any separate class for this.
>>
>> Give me a comment about what you opinion is ?
>>

> What if the customer has multiple addresses?


Exactly. If you want to store both a home address and a work address, I
think a separate address class is an excellent idea. If, however, your
business rules dictate that you will only ever store a single address for a
customer then I say put it in the customer class.


 
Reply With Quote
 
Jeff Gaines
Guest
Posts: n/a
 
      24th Jan 2011
On 24/01/2011 in message <ihk6c2$8ab$(E-Mail Removed)> Jeff
Johnson wrote:

>Exactly. If you want to store both a home address and a work address, I
>think a separate address class is an excellent idea


Building on that you may need to know an address at a certain date so an
address class with a From and To field may be appropriate, as well as a
flag to indicate home address, business address, club address etc...

--
Jeff Gaines Wiltshire UK
There is absolutely no substitute for a genuine lack of preparation
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      25th Jan 2011
On 24-01-2011 10:10, Anders Eriksson wrote:
> On 2011-01-24 15:34, Tony Johansson wrote:
>> I read a program that created a Customer class with some basic
>> information
>> such as name, birthdate telephone and so on and
>> then used a separate Address class to store the address information in
>> the
>> Customer class.
>>
>> I just wonder it that a good approach to create a separate class just for
>> that address to store the address object in the Customer. I would instead
>> store the primitive fields such as zip code,street name and city and
>> thing
>> like that directly
>> in the customer class without using any separate class for this.
>>
>> Give me a comment about what you opinion is ?
>>

> What if the customer has multiple addresses?


That would be a good reason to have a class.

public class Customer
{
private Address home;
private Address work;

or:

public class Customer
{
private IList<Address> addrs;

Arne


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      25th Jan 2011
On 24-01-2011 12:14, Jeff Gaines wrote:
> On 24/01/2011 in message <ihk6c2$8ab$(E-Mail Removed)> Jeff
> Johnson wrote:
>> Exactly. If you want to store both a home address and a work address,
>> I think a separate address class is an excellent idea

>
> Building on that you may need to know an address at a certain date so an
> address class with a From and To field may be appropriate, as well as a
> flag to indicate home address, business address, club address etc...


Good point about the historical aspect.

I am not sure that from and to are really attributes
of Address though.

Maybe:

public enum AddressType { HOME, WORK, OTHER }

public class Address
{

public class Location
{
private DateTime from;
private DateTime to;
private AddressType typ;
private Address addr;

public class Customer
{
private IList<Location> loc;

There are many ways to model this.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      25th Jan 2011
On 24-01-2011 09:34, Tony Johansson wrote:
> I read a program that created a Customer class with some basic information
> such as name, birthdate telephone and so on and
> then used a separate Address class to store the address information in the
> Customer class.
>
> I just wonder it that a good approach to create a separate class just for
> that address to store the address object in the Customer. I would instead
> store the primitive fields such as zip code,street name and city and thing
> like that directly
> in the customer class without using any separate class for this.
>
> Give me a comment about what you opinion is ?


It is definitely possible to argue that an address is
a separate entity.

But whether it is relevant to create a class for it depends on whether
you can reuse the concept in other places than the Customer class.

For a large real world app that sounds very likely to be the case.

For concrete ideas, see some of my replies to other posts in
this thread.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing a class instance from a separate class running in a separate thread titan nyquist Microsoft C# .NET 12 10th Apr 2007 12:27 PM
Is there a structure/class to store meta-info of a class so that I can generate code to a file? A.Neves Microsoft C# .NET 1 12th Apr 2006 06:50 PM
How to have New() in a Base Class create and return an instance of a Derived Class? Joe HM Microsoft VB .NET 4 22nd Nov 2005 09:31 PM
Why I can't create a contructor in a class inherited from Component Model class C.G. Oh Microsoft Dot NET Framework 0 19th Nov 2004 02:09 PM
Activator could not create a instance of the class as the Length of the class name is 36 characters Jack Wright Microsoft C# .NET 0 18th Mar 2004 04:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:45 PM.