Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: A deadly combination

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Outside Chicago, IL
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default A deadly combination

    In my mainframe world I always cautioned my users to avoid coupling new technology with a new application -- calling it a "formula for disaster".

    And that's kind of what this is as I am new to both Java and Patterns. (Run for the hills!). For Java I'm using Oracle's online Java Tutorial and for Patterns I'm using Head First Design Patterns put out by O'Reilly. I'm reasonably familiar with OOP.

    For educational purposes, I'm taking an application I built for my wife in PHP to see if I can re-tool it (in Java using Patterns). Though the strategy pattern seems to fit the bill, I'd like your thoughts.

    This application consists of 4 data-stores: The Student, The Horse, The Tack and The (Lesson) Recap. All four of these require C.R.U.D. (Create, Retrieve, Update, Delete) capabilities. The Horse and The Tack data-stores consist of merely a key and a name. The Student only a key, a name and an enum lesson day (Sunday ... Saturday) The recap, however, contains not only the key, but the data from a unique combination of these tables along with the instructor name, the date, a session identifier, a lesson identifier and a text recap of the lesson.

    What pattern would you use for this and what would you set as your interfaces?

    Regards,

    grNadpa
    Last edited by grNadpa; February 22nd, 2012 at 06:38 PM.


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: A deadly combination

    Alright, so Patterns is JavaScript? (no familiarity with it)

    But I can help you set up your objects/classes

    Basics you probably already know, but its good to use the same terminology so here goes.

    It sounds like what you call a "data-store" is an object, and thus each needs a Class to create it:
    The Student, The Horse, The Tack and The (Lesson) Recap.


    "All four of these require C.R.U.D. (Create, Retrieve, Update, Delete) capabilities."
    These look like they are your methods.
    "Create" will be handled by a "Constructor" This is a class method that has the same name as the class,
    and when you make a new Object of that class the constructor does the work setting its variables.

    E.G.
    public class Horse
    {
     
    	private int key; // all your variables would be here and most likely [B][U]private[/U][/B]
     
    	public Horse() //This is your create method. you could if you REALLY wanted to, make a "create" method that calls this.
    	{
    		key = 7; //obviously more complicated, but the idea is there.
    	}
     
    	public retrieve() //note: first letter of methods are not capitalized. this is ultimately a choice and not syntax, but taught universally.
    	{
    		// does stuff
    	}
     
    	public update()
    	{
    		// does stuff
    	}
     
    	public delete() Note: Java handles garbage collection during run time, so you may not even need a delete method.
    	{
    		// does stuff
    	}

    To elaborate on delete(). methods like this are used in lists and structures.
    But, if you wanted to delete a Horse you wouldn't write the delete method in the Horse Class.
    Instead if you had a list of your Horse Objects, you would delete your specific Horse (call him Sam) from the list.
    If your program has no way to reach Sam ever again, Java garbage collection will handle the rest,
    and Sam's space in memory will eventually get reused when needed.

    Give me some feed back, and I will continue in the direction you need.
    Jonathan

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Outside Chicago, IL
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: A deadly combination

    Hi Jonathan. Thanks for the reply.

    Actually Patterns are OOP design strategies. You may have heard of terms such as factory pattern, listener pattern, decorator pattern among others.

    The Strategy Pattern that I thought might work for my reworked application "defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from the clients that use it." (Eric Freeman et. al., Head First Design Patterns, p. 24).

    Though wikipedia has a nice writeup the strategy pattern, I found this one more helpful: Strategy Pattern - Web Tutorials - avajava.com

    In my case, the family of algorithms is the CRUD algortithms, with the idea that the independent variance are the Horse, Tack, Student and Recap. Or, at least, that was what I was asking.

    I was using the term "data-stores" as a token term. They are, as you expected, on a database: mySQL to be exact, running on a local Apache server I downloaded from xampp.

    Thanks again for your feedback -- and I certainly will study your code.

    Regards,

    grNadpa

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: A deadly combination

    I haven't heard about Patterns terminology till now, gonna take the day to read up on the links you provided.
    I suspect I will not be able to offer any advice until I have familiarized myself with what seems to be a good read.

  5. #5

    Default Re: A deadly combination

    I can see your desire to use the Strategy Pattern to fulfill your need of CRUD; however, do not let Design Patterns entice you into doing something that is, programmatically, a poor decision.

    When implementing CRUD, it is best to tell your specific objects to initiate CRUD operations. If you have a datastore of different logical objects. You should create an interface for CRUD so that all of your data stores contain the CRUD methods.

    The Java datastores, however you choose to implement them, need to handle their own CRUD methods. When manipulating objects, in order to maintain good OOP, you need to tell the object you want to change what it is you want it to do. You should rarely (very rarely) have to delegate functionality to an outside object in order to manipulate another object.

    There are two rules concerning this: the Law of Demeter and Tell, Don't Ask.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  6. The Following User Says Thank You to kenster421 For This Useful Post:

    grNadpa (February 25th, 2012)

  7. #6
    Junior Member
    Join Date
    Feb 2012
    Location
    Outside Chicago, IL
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: A deadly combination

    That's EXACTLY the feedback I was looking for. Thanks.

    grNadpa

Similar Threads

  1. how to display combination of char and interger
    By bsrk315 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2010, 08:09 AM

Tags for this Thread