|
-
Aug 18th, 2010, 10:46 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] What to implement in this case
Hi guys, I'm kinda having a brain freeze here thinking about this.
Simple best practice question here I would say.
I have a class to create lets call it "Contract" and it has a type property that can be "Service" or "Product"
If I instantiate a Contract of Service type, it should have a start date and and end date. These two dates should be mandatory but only if the instantiate Contract is a Service.
What are the different ways to implement that ? I thought about using the Factory design pattern so if you ask the factory of a Service type Contract you need to provide the two dates but I kinda find it overkill for just two different cases (Service and Product) Any Ideas of other efficient ways of doing that? I Just can't believe there wouldn't be something else.
Thanks for any suggestion.
Last edited by stlaural; Aug 18th, 2010 at 12:19 PM.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Aug 18th, 2010, 09:36 PM
#2
Re: What to implement in this case
You could provide multiple constructors and the constructor you use determines the type. If you don't specify the dates then type Product is used and if you do specify the dates then the type Service is used.
That said, does it really make sense for these to be the same class? If the dates don't relate to products at all then maybe you should have a Contract class that is inherited by a ServiceContract class that then adds the extra properties.
-
Aug 19th, 2010, 05:37 AM
#3
Thread Starter
Fanatic Member
Re: What to implement in this case
Hey JMC, Thanks for answer.
I did thought about using multiple constructor but as you said it didn't seem very appropriate.
I finally ended up creating a small factory that can return a Product or a Service which are two seperate classe that inherits from the interface IItem. And the class Contract now accepts IItem to add new Items to it.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Aug 19th, 2010, 09:32 AM
#4
Re: [RESOLVED] What to implement in this case
Why an interface, in this case? This seems like a place for simple inheritance. Are there really no functions that are implemented the same between the two, and which would therefore be suitable in a base class?
My usual boring signature: Nothing
 
-
Aug 19th, 2010, 09:46 AM
#5
Thread Starter
Fanatic Member
Re: [RESOLVED] What to implement in this case
 Originally Posted by Shaggy Hiker
Why an interface, in this case? This seems like a place for simple inheritance. Are there really no functions that are implemented the same between the two, and which would therefore be suitable in a base class?
Yes there are functions that are implemented the same between the two. The only difference is that if the Item is to be a Product then it doesn't need a start and an end date, while if its a Service then these two dates are mandatory.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Aug 19th, 2010, 02:34 PM
#6
Re: [RESOLVED] What to implement in this case
So you could have the base class be Item, and both the Product and the Service class could inherit Item. The common functionality would be in Item. Actually, from your description, ALL the functionality would be in Item. If that is the case, then the base class could be Product, and the Service class could derive from Product. The Service constructor would take the two start dates as arguments. Therefore, to create an instance of Service, you would HAVE to supply the dates, but to create an instance of Product, you would not. Since every object can be cast to an instance of its base, your Contract class could take an instance of Product, and you would be able to pass it either a Product or a Service and it would work fine. If you then needed to know the specific type, you'd have to use GetType to determine that, but the same would be true using an interface.
I'm not opposed to the interface route, either. It's a good solution, too, and I know of no reason why my suggestion would be better in any way, other than it might be a bit easier to implement. The only problem with the interface route would be that if you wanted to change how method X worked for both of them, you'd have to make the change to both classes, whereas using inheritance you would only need to make the change to one place. That's nothing but a housekeeping improvement, though, and would have zero impact on runtime. The only reason I suggested the other route is to give you other options to consider in case you had overlooked them.
My usual boring signature: Nothing
 
-
Aug 19th, 2010, 03:00 PM
#7
Thread Starter
Fanatic Member
Re: [RESOLVED] What to implement in this case
 Originally Posted by Shaggy Hiker
So you could have the base class be Item, and both the Product and the Service class could inherit Item. The common functionality would be in Item. Actually, from your description, ALL the functionality would be in Item. If that is the case, then the base class could be Product, and the Service class could derive from Product. The Service constructor would take the two start dates as arguments. Therefore, to create an instance of Service, you would HAVE to supply the dates, but to create an instance of Product, you would not. Since every object can be cast to an instance of its base, your Contract class could take an instance of Product, and you would be able to pass it either a Product or a Service and it would work fine. If you then needed to know the specific type, you'd have to use GetType to determine that, but the same would be true using an interface.
I'm not opposed to the interface route, either. It's a good solution, too, and I know of no reason why my suggestion would be better in any way, other than it might be a bit easier to implement. The only problem with the interface route would be that if you wanted to change how method X worked for both of them, you'd have to make the change to both classes, whereas using inheritance you would only need to make the change to one place. That's nothing but a housekeeping improvement, though, and would have zero impact on runtime. The only reason I suggested the other route is to give you other options to consider in case you had overlooked them.
Thanks a lot Shaggy, it does give me another option to consider.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
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
|