|
-
May 20th, 2009, 06:51 PM
#1
Oop
I'm having a hard time understanding OOP. If I have two objects, customer and employee I should create a person class which declares fields that are common to both like first, middle, last name then inside the employee class I can declare social security number, hire date, etc. But I also need to track the address of both. Home address for employee and billing and shipping address for customer.
I have seen one example that uses a class call Address that declares stuff like street, city, state, zip. The address class is simply referenced from both employee and customer classes. But I don't think this is so much OOP as it is trying to reduce code duplication by not declaring the address details over and over again within every class. Maybe OOP is supposed to reduce code duplication. An employee is a person but its not an address.
Then I have things like phone number which was a seperate class the class has a couple fields, area code, digits and extension.
Both customer and employee reference the phone number class something like
Code:
PhoneNumber home
PhoneNumber cell
Address shipping
Address billing
But I also have things like email address and website. Email address is used in both employee and customer so should I be making a new class called Email having two fields like, user and domain defined?
I have some stuff that is database specific only like "deleted" that so that when querying the database I can filter out all employees or customers that have been soft deleted but still exist in the database. Each employee or customer has a inique ID assigned to it that is only used to help the database locate the correcrt record during inserts/updates etc. This ID is never shown to the user and really doesn't belong to the employee or customer its just an internal piece of data. So technically with OOP this information is not part of a class and shouldn't be included in the class. Yet it is needed by the program. So where does it fit in? Do I create another class called DataStoreFields to hold this data?
I need to learn stuff for class but it makes my head hurt sometimes. Because I'm not supposed to include fields in the class that do not belong to the class using OOP but sometimes I need fields/data that don't belong to a class. A person walking down the street has a name they might have a phone number and if they are not homeless they have an address, but they don't have a database ID.
I can't get it stright in my head.
Thanks,
- pgrammer
-
May 21st, 2009, 11:53 AM
#2
Re: Oop
Hi,
There you've said quite a lot in that post. Are you studying in a college/school or are you studying on your own? If you're studying in a structured way you will get used to the concepts gradually and you shouldn't have to worry about getting everything right the first time. You got learn to walk before you can run . And generally speaking you probably need to learn to crawl before you walk . Anyway if you take things step by step you will get used to the basic concepts about OOP. I suggest learning from a book if you're not in a class. The advantage of a book is that a lot of thought has gone into it. The beginner's books take things step by step. If you just google around you're likely to find examples that are advanced and may not be easy to understand what is going on and why it was done that way. If you're following a class or a book and it makes your head hurt I suggest you take another class . Just joking...just take it easy. You may not learn anything overnight but if you stick with it you'll get the hang of it. I've been developing for about 12 years. I remember some tough times along the way. And generally it can be difficult whenever you are learning something new (e.g. moving from procedural programming to oop, moving from windows development to web development, etc).
-
May 21st, 2009, 05:25 PM
#3
Re: Oop
I am going to college in a couple months and I will be taking classes on OOP but I have been programming for a while and I want to get some basic stuff down this summer so the class goes easier.
I can program pretty well but I have a hard time getting everything to fit together into a real program.
Could you post an example of how to make the two classes correctly? Just the fields. I don't know if things like address should be in a seperate class or part of the customer/employee classes.
Thanks,
- pgrammer
-
May 21st, 2009, 05:32 PM
#4
Re: Oop
 Originally Posted by pgrammer
Could you post an example of how to make the two classes correctly? Just the fields. I don't know if things like address should be in a seperate class or part of the customer/employee classes.
There really isn't any 'correct' way of doing it as it all depends on what you are trying to achieve. For example, it's different whether there can be multiple addresses associated with a person. Say you have a home address and a work address. You might want to split the address into a separate class and have a reference to an Address object for home and another for work. If you only have one address, you might just want to put these fields into the employee object. Another approach (for multiple address) is for the employee class to contain a list of addresses (and each Address object has a field which describes what it is (i.e. home, work, summer home, etc.).
So the bottom line is that it is up to you how you structure things.
-
May 21st, 2009, 06:16 PM
#5
Re: Oop
Arjay is right. The 'right' thing to do will vary from one situation to another. Don't try to over-engineer what you are doing. Just do what seems best now. Once you've completed it you will be able to see commonalities or places where you can 're-use' the code through object oriented techniques. Alternatively you can post your classes here on Codeguru and people can give you some suggestions. Someone can give you a 'solution' now. But it would be much better for you to have a go yourself first. That way you'll get a better understand about what we try to achieve through object oriented programming.
-
May 21st, 2009, 10:34 PM
#6
Re: Oop
Thanks I think I do try to over engineer stuff. I do have a solution but its on my home computer and I get internet from the library so I wasn't able to post my code. I will try to get it on a disk and bring it to the library next time so you can see it.
Your suggestions helped so far, thanks.
Thanks,
- pgrammer
-
May 22nd, 2009, 11:41 AM
#7
Re: Oop
 Originally Posted by pgrammer
Thanks I think I do try to over engineer stuff. I do have a solution but its on my home computer and I get internet from the library so I wasn't able to post my code. I will try to get it on a disk and bring it to the library next time so you can see it.
Your suggestions helped so far, thanks.
Basically all your questions additional belongs to a theme called : Best practices or also Design patterns
Depending on how you will use your classes in which way your program is structured you will need different designs of your class.
This BTW is a very interesting theme and you will have lots of fun reading books about C# Design patterns and this will answer you, why there is not simple one way to do a 'simple' person, adress, or employees class. Maybe you want to create an abstract base class wich already contains some methods like read from / write to database /delete / soft delete methods and overwrite them when needed.
The question is not if you should devide your email adress into user and domain field, teh question is simple does it support any useful actions ?
Maybe better have an email class which tests email using Regex in the set Property and throws an error when the check fails. Maybe you know a reason to devide the email adress, I do not in the first short sight. The same is for Database ID. A Person class describes a person but it isn't the person. If this class should keep the database in it or not ? In all my programs it does, because as long as its a person stored in my database the ID is unique to that person in the same way as you may have a life-insurance Number which is unique to you. Simple that's the connection. So why not. There are no 'ethical' aspects that a man is no number. The only reason to take some data out of a class may be of a technical aspect, for example in classes of a 'flightweight' pattern. So all together:
Top in design decisions IMHO is how the class is to be used, in which pattern(s) it should be useful, reuseability of code is a big point. I hope this gives you additional viewpoints so you get some more ideas about it.
Last edited by JonnyPoet; May 22nd, 2009 at 11:55 AM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 22nd, 2009, 06:06 PM
#8
Re: Oop
Thanks all your suggestions help. I still don't have a sample to post because I haven't been to the library yet. I'm using my cell phone to post to code guru. I think I have enough information now to figure out what I need to do. I'll get something working now and then I can fix it later when I take my OOP classes at college in a couple month.
Thanks for the help.
Thanks,
- pgrammer
-
May 23rd, 2009, 04:18 AM
#9
Re: Oop
I may think that C# isn't a language created to be named a OOP handler because it is a framework that already wrap the OOP concept around for java-friendly programmers and offer them pathways to be with Microsoft's products. I don't read your post carefully because it is long and am afraid of long posts due to its internal messages that might be mainly to wake me up at midnight.
I would start to think of your problem from abstract Personal Information class that has Customer and Employee as its derived chidren. All the Address stuff can be wrap in a struct declared as public section. This is what I love in C#, as things seem easier for beginners of this language (I too am a C# beginner) when it comes to nested class/struct. IDE also provides an intelisense that updates where and what you are allowed to link to or type down. You may also need to supply several helper functions to update the the input information
PHP Code:
public abstract class Personal_Information
{
// declare what needs to resuse
// include update functions
}
public class Customer:Personal_Information
{
// reuse what should be reuse
//add in what you need to
}
hi,,,
-
May 23rd, 2009, 04:50 AM
#10
Re: Oop
 Originally Posted by Khiem
... I don't read your post carefully because it is long and am afraid of long posts due to its internal messages that might be mainly to wake me up at midnight.

Maybe you wake up BECAUSE you dont read them carefully ?? 
 Originally Posted by Khiem
I would start to think of your problem from abstract Personal Information class that has Customer and Employee as its derived chidren.
Agreed already suggested this too ( But this comes because you dont read posts carefully as you yourself already told )
 Originally Posted by Khiem
All the Address stuff can be wrap in a struct declared as public section. This is what I love in C#, as things seem easier for beginners of this language (I too am a C# beginner) when it comes to nested class/struct.
You can do this, its a very interesting construction as it has reference qualities for the class and valuetype qualities for the adress then. But I personally would not use a struct I would use an abstract class for the adresses too. Classes are reference types, while structs are valuetypes, and I cannot really see a reason in this case to use that sort of design ?
Adresses may be very different in there structure, depending on countries where they are from. And maybe you want to have some common methods for them like testing the postal Order number fits to the adress if its a database connected class. But this all are design decisions. So someone may want separated classes, someone likes to have them nested. Some others are nesting structs into classes. mostly deends on what you want to do
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 23rd, 2009, 12:46 PM
#11
Re: Oop
So, derived classes do not have to use all of the properties inside its base class? Is it a correct design if I make a base class with several basic properties that will be reused, but some properties may not be used in every derived class?
Code:
Person Class
First Name
Middle Name
Last Name
Date of Birth
Code:
Employee : Person
Base.FirstName
Base.MiddleName
Base.LastName
Base.DateOfBirth
HireDate
SocialSecurityNumber
Code:
Customer : Person
Base.FirstName
Base.MiddleName
Base.LastName
CustomerID
LastOrderDate
See DateOfBirth is part of the person class, but I really don't need to track DateOfBirth of a Customer, so I just don't use that field. Is this a good design? Or does a derived class need to use all of the base classes properties/functions to be correct design?
Thanks,
- pgrammer
-
May 23rd, 2009, 01:57 PM
#12
Re: Oop
 Originally Posted by pgrammer
So, derived classes do not have to use all of the properties inside its base class? Is it a correct design if I make a base class with several basic properties that will be reused, but some properties may not be used in every derived class?
A property already done in the base class exists in any derived class.
You do it the other way round. Only that fields that are common to all derived classes are members of the base class. Members only specific to a destinated purpose as your date of birth are part of the derived class.
BTW the code you show is not C# because in C# referencing tho the baseclass is not done this way a) base has only lowercase letters no uppercase.
b) you are missing the braces , public, private, protected keywords
c) its absolutly unnecessary to referende the base class.
d) Class itself is not defined as you wrote it.
e) your fieldnames are not allowed to have spaces in between
f) the fieldnames re not declared correctly
...
It should look like
Code:
public abstract class Person{
private int _ID; // a customer as well as an employee may have an ID
private string _firstName;
private string _middleName;
private string _lastName;
private DateTime _birthDay; // birthday is also needed in a customers file ( at least in Austria you can only sue a person to pay his depts when you know the customers birtday date )
// the constructor (could be done or not- depending what you want to design
public Person(){}
// Properties
public str*ng FirstName{
get { return _firstName;}
set { _firstName = value;}
}
// all the other properties in the same way
}
public class Employee: Person{
private DateTime _hireDate;
private string _socialSecurityNo;
// Constructor
public Employee():base(){ }
// Properties we only need to add the new Properties
// all others are already in the base class
public DateTime HireDate{
get{ return _hireDate;}
set{ _hireDate = value;}
}
public SocialSecurityNo{
get{ return _socialSecurityNo;}
set{ _socialSecurityNo = value;}
}
}
// and the Customer is like
public class Customer :Person{
private CreditInfo _creditable;
//Constructor
public Customer():base(){}
// Properties
public CreditInfo CreditAble{
get{ return _creditable; }
set { _creditable = value;}
}
}
// public class CreditInfo{
....
}
Sorry I dont know the exact name for the fact if a person is allowed to get credit or if he has to pay cash, so I named this creditable ( able to get credit )
One to one translated from german it would be CreditWorthy but that sounds really strange to me 
The Lastorderdate is not to be found here. It is changing after every new order so it is fetched from the database Orders Table when needed. But thats the point where design decisions begin.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
May 25th, 2009, 08:40 PM
#13
Re: Oop
Thanks JonnyPoet for your clearification and explanation,
I like what you post, I am sorry for my previous suggestion to OP of the struct use that is defined differently from C++, and I also find it is not good to be abused in the class design. If you are a C++ programmer you might find it similar at first but not really! The struct defined in C# is not NULL except you have to add in its Nullability. So whatever you want to use, either a struct or a class you still have to invoke its constructor at first as in
Code:
Type_Struct obj=new Type_Struct([are]);
Type_Class obj=new Type_Class (arg);
Then I wonder why you should bother a struct in place of a class while you may still want to have a set and a get as in the above example ?
hi,,,
-
May 26th, 2009, 03:33 AM
#14
Re: Oop
 Originally Posted by Khiem
Then I wonder why you should bother a struct in place of a class while you may still want to have a set and a get as in the above example ?...
Hmmm I havn't suggested this in Post#10. Maybe you misunderstood me. I wrote
 Originally Posted by JonnyPoet
You can do this, its a very interesting construction.... But I personally would not use a struct I would use an abstract class for the adresses too. Classes are reference types, while structs are valuetypes, and I cannot really see a reason in this case to use that sort of design ?
I cannot say its impossible or unusual to do this, because there are lots of cases where we may have structs as Properties of classes like using a Point, a Size Property... This are structs and they are used in C# as Properties in the same way as other Valuetypes. So it was my way to slightly disagree with using such a construction, without getting in major discussions about it.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|