Results 1 to 14 of 14

Thread: Class Design

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved Class Design

    Need help with class design. I've got a DataType Class and I need help on making a Collection of this class. Kinda new to Java to please understand. Thanks.

    This is DataType class
    VB Code:
    1. import java.util.*;
    2.  
    3. public class DataType{
    4.     String sqltype,
    5.         javatype,
    6.         csharptype,
    7.         vbtype;
    8.     public DataType(){
    9.     }
    10.     public DataType(String sqltype,
    11.         String javatype,
    12.         String csharptype,
    13.         String vbtype){
    14.         this.sqltype=sqltype;
    15.         this.javatype=javatype;
    16.         this.csharptype=csharptype;
    17.         this.vbtype=vbtype;
    18.     }
    19. . . .
    20. }
    Now I want to make a collection of that class. Something like
    VB Code:
    1. import java.util.*;
    2. public class DataTypes extends [i]IDontKnow[/i]{
    3.     public DataTypes(){}
    4. }
    where I can return something of type DataType and a public function add for adding of type DataType. Please help. Any recommendations and links for tutorials are greatly appreciated. Thank you.
    Last edited by nebulom; Mar 9th, 2005 at 09:27 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Class Design

    Im not too good with design issues but i will try and help. You would probably have to implement the Collection interface and provide implementation for all of the methods that are contained within or extend one of the concrete classes such as TreeSet or ArrayList. As for which one to do i couldn't give you a suggestion. I just use the concrete classes for most of my programming.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Class Design

    Thanks Dilenger but can you give some codes. I don't know much on Java. Or perhaps... a link or something. Thank you very much.

    Btw, why did you give up being a mod? You could have been with NoteMe. You two can be a mod.

  4. #4
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    so do you want a collection to take data types? or make the datatype class a collection?

    Which ever it is, just make that class implement the Collection interface, and implement the methods, its hard to say exactly how to do this without understanding what you want to achieve, the methods on Collection are pretty well spec'd int he Java doc....


    Do you actually want to make this class a Collection? or simply have the functionality of the collection class, and therefore make all the methods return DataType rather than Object.....


    Andy

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Class Design

    Yeah i was confused about what he wanted to do also.

    Posted by nebulom

    Btw, why did you give up being a mod? You could have been with NoteMe. You two can be a mod.
    I originally decided to quit programming and try somthing else but i since decided to just do it for fun and not place any unrealistic expectations on it. Plus i am moving from new jersey to tennessee in three weeks so with all of the stuf i have to get in order before the move and then after my time would be limited.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Class Design

    Thanks guys.

    I just wanna make the DataTypes a collection of DataType class. Something like this on C#
    VB Code:
    1. using System;
    2.  
    3. namespace dtgen_0_0_3
    4. {
    5.     /// <summary>
    6.     /// Summary description for DataTypes.
    7.     /// </summary>
    8.     public class DataTypes:System.Collections.CollectionBase
    9.     {
    10.         public DataTypes()
    11.         {
    12.             //
    13.             // TODO: Add constructor logic here
    14.             //
    15.             this.Add(new DataType("bigint","Int64","Integer"));
    16.             this.Add(new DataType("binary","byte[]","Byte()"));
    17.             this.Add(new DataType("bit","bool","Boolean"));
    18.             this.Add(new DataType("char","string","String"));
    19.             this.Add(new DataType("datetime","DateTime","DateTime"));
    20.             this.Add(new DataType("decimal","decimal","Decimal"));
    21.             this.Add(new DataType("float","float","Double"));
    22.             this.Add(new DataType("image","byte[]","Byte()"));
    23.             this.Add(new DataType("int","int","Integer"));
    24.             this.Add(new DataType("money","decimal","Decimal"));
    25.             this.Add(new DataType("nchar","string","String"));
    26.             this.Add(new DataType("ntext","string","String"));
    27.             this.Add(new DataType("numeric","decimal","Decimal"));
    28.             this.Add(new DataType("nvarchar","string","String"));
    29.             this.Add(new DataType("real","Single","Single"));
    30.             this.Add(new DataType("smalldatetime","DateTime","DateTime"));
    31.             this.Add(new DataType("smallint","short","Short"));
    32.             this.Add(new DataType("smallmoney","decimal","Decimal"));
    33.             this.Add(new DataType("sql_variant","object","Object"));
    34.             this.Add(new DataType("text","string","String"));
    35.             this.Add(new DataType("timestamp","DateTime","DateTime"));
    36.             this.Add(new DataType("tinyint","byte","Byte"));
    37.             this.Add(new DataType("uniqueidentifier","Guid","Guid"));
    38.             this.Add(new DataType("varbinary","byte[]","Byte()"));
    39.             this.Add(new DataType("varchar","string","String"));
    40.         }
    41.  
    42.         public DataType this[int i]
    43.         {
    44.             get{ return (DataType)this.InnerList[i];}
    45.         }
    46.  
    47.         public void Add(DataType v)
    48.         {
    49.             this.InnerList.Add(v);
    50.         }
    51.     }
    52. }
    How do I convert this to Java? Thanks.

  7. #7
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    Ok so you want a collection of data types, this is pretty easy to do, you can either write a class and implement the COollection interface, or you can hi jack the method you require from teh collection interface abd make them retrun something sensible instaed of an object.

    Personally if you don't need the full compatibility with the collection interface i'd just implement all the collection methods and change the return types from Object to Data type, that way you won't have to do any visible vasting, i.e.
    Code:
     DataType d = (DataType)collection.get(i);
    I think .Net has the luxury of generics (correct me if im wrong) which means you can create a collection of a certina type, therefore all return values will be of that type, unfortunatley Java 1.4 which is what i'm using dosen't have that, i'd have to upgrade to 1.5 whci i'm not allowed to do until the product development manager deicdes we can release with it!

    Ok below is a kind of implementation of a collection class.
    Code:
     public class DataTypeCollection{
     
     private ArrayList list = new ArrayList();
     
     public void add(DataType type){
     	this.list.add(type);
     }
     
     public void remove(DataType type){
     	this.list.remove(type);
     }
     
     public DataType get(int index){
     	return (DataType)this.list.get(index);
     }
     
     public int size(){
     	return this.list.size();
     }
     
     public boolean contains(DataType dataType){
     	return this.ist.contains(dataType);
     }
     
     }
    now that isn't a full implementation but you see what i'm doing, onstead of returning Objkects which is what any class implementing Collections will do i'm returning a DataType and encapsulating all my casting.....

    This would achieve what you wanted to do, if you wanted full Collections compatibility you could write another Wrapper class around this which would implement teh Collection interface properly.... if you don't need this then the class i've semi written above will remove the headache of casting etc...


    does that make sense? let me know i've been vague or missed the point and i'll try again...

    Andy

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Class Design

    Here I've got
    VB Code:
    1. import java.util.*;
    2. public class DataTypes{
    3.     ArrayList list=new ArrayList();
    4.  
    5.     public DataTypes(){
    6.         this.list.add(new DataType("int","int","int","Integer"));
    7.         this.list.add(new DataType("bigint","int","Int64","Integer"));
    8. . . .
    9.     }
    10.  
    11.     public void remove(DataType v){
    12.         this.list.remove(v);
    13.     }
    14.  
    15.     public DataType get(int i){
    16.         return (DataType)this.list.get(i);
    17.     }
    18.  
    19.     public int size(){
    20.         return this.list.size();
    21.     }
    22.  
    23.     public boolean contains(DataType v){
    24.         return this.list.contains(v);
    25.     }
    26. }
    Thanks a lot. But Andy, it says,
    VB Code:
    1. Note: DataTypes.java uses unchecked or unsafe operations.
    2. Note: Recompile with -Xlint:unchecked for details.
    I'm sorry to ask you what's this thing. Furthermore, I want to access the class like this
    VB Code:
    1. foreach(DataType in DataTypes) . . .
    Can I do this?

    Again, thanks a lot. And sorry of the bother.

  9. #9
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    ah hah! your using Java 5, or 1.5 or whatever they decided to call it aren't you?

    ok, its should be straightforward then as you can use generics...

    Code:
      ArrayList<DataType> list = new ArrayList<DataType>();
      list.add(new DataType(args....));
     DataType type = list.get(0);

    and that should be it, you can then use the Java 1.5 things to loop around
    the collection i.e.

    Code:
       for(DataType d: list) {
           //Now you can use d, and it will be the current element in the list!
       }
    that what you needed?

  10. #10
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    the unchecked error you get is because i wrote the first snippet thinking you were on 1.4, now when we cast the object rfomthe array list into a DataType we don't check to see if the object is in fact a data type.... in theory we should actually wrap a try catch round it and deal with the problem if its now a datatype....

    However if your using 1.5 you can use generics and it will all be done for you!

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Class Design

    Quote Originally Posted by Andy_Hollywood
    that what you needed?


    Yes, that's it. Thank you very much. I've got this so far.
    VB Code:
    1. import java.util.*;
    2.  
    3. public class DataType{
    4.     String sqltype,
    5.         javatype,
    6.         csharptype,
    7.         vbtype;
    8.  
    9.     public DataType(){
    10.     }
    11.  
    12.     public DataType(String sqltype,
    13.         String javatype,
    14.         String csharptype,
    15.         String vbtype){
    16.         this.sqltype=sqltype;
    17.         this.javatype=javatype;
    18.         this.csharptype=csharptype;
    19.         this.vbtype=vbtype;
    20.     }
    21.  
    22.     public ArrayList<DataType> PossibleDataTypes(){
    23.         ArrayList<DataType> list=new ArrayList<DataType>();
    24.         list.add(new DataType("int","int","int","Integer"));
    25. . . .
    26.         return list;
    27.     }
    28. }
    Feel free to comment guys as I do not know much about this stuff. Thank you.

  12. #12
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    The only thing i would change, but this is me, is where you specify the java data type i wouldn;t pass in a string, i'd pass in the class, i.e. Integer.class, or String.class, also where you pass in the SQL type i wouldn't pass in a String i'd pass in the int representation of the type from java.sql.Types, i.e. Types.NUMBER, Types.VARCHAR etc..... at least that way you remove the dependancy on the user being able to spell the data types correctly and remove capitalisatin issues etc.....

    apart from that, glad it works

    Andy

  13. #13
    Addicted Member
    Join Date
    May 2001
    Location
    UK
    Posts
    222

    Re: Class Design

    oh yeah ... you might want to seperate out the data type collection from the data type class, i noticed when you call possible data types you recreate the list everytime, might be a little nicer if it created one and kept it around, rather than recreating the same informatio nagain and again.

    You could have a public static method on DataType which was getList() which returned a static List of data types to you, then you could add and remove from this in a static block e.g.:



    Code:
      
      public class DataType {
      
      	protected static List<DataType> dataTypes = new ArrayList<DataType>();
      
      	public static List<DataType> getPossibleDataTypes(){
      		return DataType.dataTypes;
      	}
      
      	static{
      		DataType.getPossibleDataTypes().add(new DataType(..string..));
      		DataType.getPossibleDataTypes().add(new DataType(..integer..));
      		DataType.getPossibleDataTypes().add(new DataType(..byte..));
      	}
      
      	/**
      	 * Original Data Type class
      	 */
      	String sqltype,
     		javatype,
     		csharptype,
     		vbtype;
     
     	public DataType(){}
     
     	public DataType(String sqltype,
     		String javatype,
     		String csharptype,
     		String vbtype){
     		this.sqltype=sqltype;
     		this.javatype=javatype;
     		this.csharptype=csharptype;
     		this.vbtype=vbtype;
             } }

    This way you maintain you data type class, but you have a collection of possible data types availbale in the data type class.... you can staically initialise as many data types as you want, but then you also have the ability to add and remove from teh possible data type list from elsewhere because your exposing the list.....

    Make sense?

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Class Design

    w00t
    Thanks.

    This is just used for my tier generator. The flow is get the information_schema.tables from a certain database and substitute the datatype from sql to java, c# and vb.net. Also to generate classes for the said languages. That's just the reason I made it as a string to write it on a code by code basis.

    Say, I have a Customer table, it generates Customer.java providing the variable under the class are the fields from the Customer table. So, just replacing the types by string is (I think) ok for me now.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width