473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

A Simple/beginner question on Generics & OOPs design

I am new to programming and trying to work on a simple console app. I am making a simple Library Management system where the user/admin can manage members and item.
Attaching the menu so it's clear to understand what I am saying.


So far I have only looked at Item and Member Management (option 1 and 2)
Few question I had:
1. In terms of the design approach I am taking.
One class to hold Member details, one for Item details - just instance vars and getters/setters
One class called LibraryManagement, which hold most of the main functionality. From here members and items will be added, edited, deleted based on user input.
One class called Menu, which only hold the Main, member and Item menu for code clarity.
====Does this seem like a reasonable design approach? ====

2. So far I have completed the Manage Member functionality. I realise that the Item Management works exactly the same way as Member management. Only diff is that a newly created item object gets added to List<Item> as opposed to member being added to List<Member> .
It seems like a waste to repeat everything and just replace List<Member> with List<Item> and Member with Item object.
I am trying to find out a way so I can use the same method to add, edit and delete details from the List and only use a generic method where the type is detected at runtime.

A snippet of some relevant code:
class LibraryManagement
{
....
Member mem;
List<Member> memberList = new List<Member>();
....
//Right now for example my method is:
public void AddMember(Member member, List<Member> listOfMembers)
{ listOfMembers.Add(member);}
//which gets called after the appropriate object is created:
public void manageMember()
{
.....
mem = new Member(id, name, address, email);
AddValues(mem, memberList);
....
}
...
}

If and how can I convert it into something generic, something like this:
addDetails<T, U>(t memberItem, U list){{ list.Add(memberItem);} //where U is a genric List and T is just any Type(in my case an Object)
and be able to call the method this way:
AddDetails(mem, memberList)
AddDetails(item, itemList)
Dec 18 '08 #1
2 3493
nukefusion
221 Expert 100+
Hello,
In terms of design approach it seems fine, although I'm not a guru on OO design or anything (theory is great up until a point, then you just need to get some work done). With my own work I always take a fairly low-level approach which I've found to work well. Some of the main things I do are:
  • Try and model my solution on real world equivalents (so like in your example, I'd have a Member class to represent a Member, an Item class to represent an Item, etc, etc)
  • If I have to scroll more than about a page to see my entire class, then I start thinking it's getting too big and I should try and split out the funtionality a bit. I try to keep things easy to maintain.
  • Try and generalize where possible, making use of inheritance, etc. So, using your example, if Member and Item are treated in almost exactly the same way I would split the common functionaility into a base class or interface.

The third point is kind of relevant to the second thing you mention as it's probably the easiest way to eliminate code repetition. I would probably start by creating a base class (or interface, whatever is most appropriate) that has the shared functionality of both Member and Item - let's call it LibraryEntity.
Then you could write a method to do the actual management or processing of Items and Members on LibraryEntity objects instead, which you could call like so:

Expand|Select|Wrap|Line Numbers
  1. private void ManageEntity(LibraryEntity entity)
  2. {
  3.     // Do stuff to entity here (both member and library are entity objects)
  4.     // so now we only need to write this code once
  5.     // more code lines
  6.     // etc etc
  7. }
  8.  
  9. public void AddMember(Member member)
  10. {
  11.     ManageEntity(member);
  12.     memberList.Add(member);
  13. }
  14.  
  15. public void AddItem(Item item)
  16. {
  17.     ManageEntity(item);
  18.     itemList.Add(item);
  19. }
  20.  
Dec 18 '08 #2
Thanks a lot. It took me some time to get my head around what you were saying as I am so new to this.I have been racking my brains for 2 days as I had taken a slightly different approach earlier and could not seem to disconnect from it but finally I think I have got it and I can see the benefits in working this way. The OOP theories that I have studied so far seems so much practical now :) Thanks again.
Dec 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: 3rdshiftcoder | last post by:
Hi- I'd like to use this class from a book but am getting a compiler error. On the line private List < String > list; p.932 Chapter 19 found in the code below in the eclipse editor it is...
13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
5
by: Jacky Luk | last post by:
import java.awt.Graphics; public class printtest extends java.applet.Applet { public void init() { } public void paint (Graphics g) {
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have some problems with Crystal Reports (Version 10.2, Runtime 2.0). In Section3 I have added a OLE Object (Bitmap). Now when I open the report in my code I would like to set this...
3
by: Anders Borum | last post by:
Hello, I've worked on an API for quite some time and have (on several occasions) tried to introduce generics at the core abstract level of business objects (especially a hierarchical node). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.