• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Design patterns

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please, tell me where to use what design pattern. I studied GoF design pattern and I understand what they are but I'm not quite sure how they are applied.

Thanks.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kaz Yosh:
Please, tell me where to use what design pattern. I studied GoF design pattern and I understand what they are but I'm not quite sure how they are applied.

Thanks.



Here's the patterns I use:
- Model/View/Controller in GUI
- Singleton for the database file access, lock manasger, RMI client and properties among others
- Proxy over the network to the database
- Adapter to add the exceptions to the Data class
- another Adapter to build the Proxy
- Parameterized Factory Method to choose local vs network
- DTO to encapsulate a record

In a previous version of this that was thin client I also used
- Command to pass commands across the network
- DAO to provide database CRUD operations.
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Kaz. For the thin client, I used:

Singleton for the Schema and the Data classes
Business Facade to hide the Data class behind business methods.
Adapter to hide the Business Facade behind remote-enabled methods
MVC for the GUI.

I also hid classes behind interfaces.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just finished coding for data retrieve and now I'm moving towards data locking. The first question I came up with was that how I could implement read-write lock if there are only lock() and unlock(). I was trying to use Adapter patter for this but got nowhere so far. Any suggestion?

Thanks
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Kaz. I hope the following will be useful to you in your project:

Basically, the IO read/write locking and the locking enabled by the lock/unlock methods are two different things. The IO locking is needed to prevent physical data corruption (i.e., at the data file), but the lock/unlock locking is needed to prevent record corruption (i.e., unwanted manipulation of different clients on the same record, which may cause the record to be overwritten by another record, or changed unexpectedly.)

If you are writing and reading records from IO, both of these types of locking are necessary. If you are caching the records at startup, the IO locking may or may not be necessary depending on how you implement the IO.

The IO locking involves allowing only one thread to write to the data file. I would suggest looking into the specs of java.io.RandomAccessFile class for information on how this may be achieved.

The lock-unlock locking is best illustrated by an example of its use in a business method - such as bookRoom. One would lock a record, then perform the necessary manipulations, then unlock the record. In the meanwhile, no other thread would be able to manipulate this record (if you are reading/writing from the IO file directly, without cache, this record would be safe from other threads writing to its location in the datafile.) The lock-unlock locking is necessary because some business operations will require a multi-step operation on a record; and when multi-threading is considered, it is necessary to prevent other threads from modifying the record by cutting in between the steps of the original thread. (This assumes that the Data class is thread-safe, allowing a manipulation upon a record to be viewed as an atomic one; so the concern is that nothing happens to the record in between manipulations [calling of the Data class methods] by the same thread.)
[ November 16, 2004: Message edited by: Anton Golovin ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kaz,

Originally posted by Kaz Yosh:
Please, tell me where to use what design pattern. I studied GoF design pattern and I understand what they are but I'm not quite sure how they are applied.

Thanks.



One thing you need to understand is that design patterns are tools to help you. They may be tools to help you understand other peoples designs, they may be tools to help you describe your design to others, and they may be tools to help you think of solutions to your problems.

However there is no requirement to explicitly use design patterns. Just because someone else used a particular pattern does not mean that you must use it. Or just because you have used a particular pattern that no-one else seems to be using does not mean that you are wrong. Or if you cannot spot the patterns you are using does not make you a bad programmer.

There are two things I think you should do be doing at the moment:
  • Go through the GOF book, looking at the generic description of any given pattern, and then think whether this matches the way you have tried to write part of your code. You could try describing your code to us here, and asking whether this does match a particular pattern or not.
  • Go through the descriptions provided by others of where they have used patterns, and see whether you can see how the pattern could be applied there. You don't have to agree with it's usage, the purpose is just to try and apply your knowledge to see if you can understand how others are using their patterns. For example, Peter mentioned that he used the Proxy pattern to provide network access to the database - can you see how that works? If you don't understand how it might work, feel free to ask questions (I would recommend being specific in saying that you want to understand how the pattern can be applied, otherwise you will probably get unrelated solutions to the same problem). I would recommend doing this step second, as it may affect your way of thinking about the first step.


  • This project provides an excellent opportunity to learn and discuss patterns, as everyone here is dealing with common concepts and very similar solutions, which lends itself to discussing how common patterns can be applied.

    Regards, Andrew
     
    Kaz Yosh
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the replies, Peter, Anton and Andrew

    I want to ask you guys more specific questions but I can't cause I'm really confused with all the aspects that I have to implement. So I'll try to explain what I've done so far and what I'm trying to do next. I'll ask question along the way. In my Data class, which implements DB interface, I use RandomAccessFile to retrieve data from a flat file provided by Sun. I made RandomAccessFile static and made Data class constructor private so that I can make sure there should be only one RandomAccessFile. I use createInstance() public method to create this object if and only if there's no instance available, otherwise, just return the reference. As Anton mentioned, I noticed that this isn't enough to protect RandomAccessFile under muti threaded environment. Next thing I want to do is to store the data retrieved from a file to somewhere so I can reuse it later. I'm thinking about making an inner class called Contractor, by the way, I'm doing Bogditt and Scaper, within Data class and use Vector or ArrayList to store contractor objects. My first question is that if I should put this contractor class outside the Data class. I know in the later time, I'll have to make data class serializable in order to transfer it over the network. Could this be a bad if I put a contractor class within the data class? Another question for anton, you said you hid classes behind interfaces. What does this mean and how did you do that?

    Thanks in advance.

    [ November 17, 2004: Message edited by: Kaz Yosh ]
    [ November 17, 2004: Message edited by: Kaz Yosh ]
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Kaz,

    I want to ask you guys more specific questions but I can't cause I'm really confused with all the aspects that I have to implement. So I'll try to explain what I've done so far and what I'm trying to do next.



    Sounds like a good way to go about this.

    In my Data class, which implements DB interface, I use RandomAccessFile to retrieve data from a flat file provided by Sun.



    Sounds familiar

    I made RandomAccessFile static and made Data class constructor private so that I can make sure there should be only one RandomAccessFile.



    Take a look at the GoF book description of Singleton pattern, in particular, the Intent: "Ensure a class only has one instance, and provide a global point of access to it." So it looks like you are already using patterns

    I use createInstance() public method to create this object if and only if there's no instance available, otherwise, just return the reference.



    This is standard functionality from within the Singleton pattern, however the usual naming convention to have a getInstance() method rather than a createInstance() method. It just helps the users of your class to realise that they are all using the one instance.

    You might want to look at the Sun article "When is a Singleton not a Singleton?" for common mistakes / issues when implementing the Singleton pattern.

    As Anton mentioned, I noticed that this isn't enough to protect RandomAccessFile under muti threaded environment. Next thing I want to do is to store the data retrieved from a file to somewhere so I can reuse it later. I'm thinking about making an inner class called Contractor, by the way, I'm doing Bogditt and Scaper, within Data class and use Vector or ArrayList to store contractor objects. My first question is that if I should put this contractor class outside the Data class.



    It depends on what you want to do with this Contractor class. If it is only going to be used within Data class, then you might want to have it as an internal class. But if there is the potential that one day you might want to use it external to your Data class, then you should probably consider making it external.

    I know in the later time, I'll have to make data class serializable in order to transfer it over the network.



    Data class provided access to your data. All you really want is the data to be sent over the network - not the entire file access class. So Data class would not normally be made serializable, nor would it be sent over the network.

    Regards, Andrew
     
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What is GOF book?
    I am also a beginer of design patterns and reading "The design patterns" by James W. Cooper( one of reading materials that this forum recommands). Since I have never use any patterns except mvc I want to take a look examples as many as possible, especially those I consider to use.

    If you guys give me, "Bible" book(I mean exact name or website, it will really help me.

    Thanks
     
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    What is GOF book?



    You can find the GoF book here
    .

    Matt
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Matt Sheehan.:


    You can find the GoF book here
    .

    Matt



    The same book, and also through Amazon: Design Patterns. The reason for the different link style is because if you click on the link I gave, then purchase through Amazon, Amazon will give JavaRanch a referrer's fee. And that will help pay to keep JavaRanch running (it is not free to run, even though we do not charge anyone to use it).

    This is considered by many to be the book on Design Patterns, and it is commonly refered to as the "Gang of Four" book (or GoF book), because of the four authors: Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides.

    On the downside, it is a very dry book, and the examples are in C++. Many people just learning design patterns find that other books are easier to read, especially if they are written using a computer language the reader already knows.

    You might also want to check out "Head First Design Patterns". If you have read other books by Kathy Sierra and Bert Bates you will appreciate this.

    The best advice is to download a couple of chapters (of any or all books) where possible, and see if reading them makes sense to you. If the individual chapters make sense, then consider buying the book. There is no value on buying "the" book, only to find that you cannot understand the way the author presents information. (The other problem with buying "the" book is that it is all subjective - what I consider "the" book may not be the same as what anyone else considers to be "the" book).

    Regards, Andrew
     
    Kaz Yosh
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the reply again,

    I felt little confident and got confused again in the last couple of minutes.

    You said,

    Data class provided access to your data. All you really want is the data to be sent over the network - not the entire file access class. So Data class would not normally be made serializable, nor would it be sent over the network.



    If the Data class handles the data, does that mean the Data class represents data? How could I send data over the network without data class? I don't understand how I could send the data.
     
    Andrew Monkhouse
    author and jackaroo
    Posts: 12200
    280
    Mac IntelliJ IDE Firefox Browser Oracle C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Kaz,

    The Data class is the class which Sun requires you to build - it provides all access to the data from the file also provided by Sun. The Data class might call file access methods directly, or it might be a Fa�ade to other classes. But in either case, you do not want to run the Data class' methods on the client machine - they must be run on the server.

    You might chose to run the Data class' methods remotely (RMI: Remote Method Invocation) from the client, but in that case, they are still running on the server - not on the client. (The most common alternative to running the Data class' methods remotely is to run business methods on the server remotely, and have those business methods call the Data class' methods.).

    Since you will be running the Data class' methods remotely, there is no need for the Data class itself to be serializable - there is no need for the entire class to be sent from the server to the client. All you need to do is ensure that the parameters and return values of the remote methods are serializable. By lucky coincedence, all the parameters and return values of the provided interface are serializable.

    If you decide to call business methods on the server, then it will be up to you to ensure that your parameters and return values are serializable - so if you decided to return instances of the Contractor class from your business method, then the Contractor class would need to be serializable.

    Does this help?

    Regards, Andrew
     
    Ranch Hand
    Posts: 181
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Kaz Yosh:
    In my Data class, which implements DB interface, I use RandomAccessFile to retrieve data from a flat file provided by Sun. I made RandomAccessFile static and made Data class constructor private so that I can make sure there should be only one RandomAccessFile. I use createInstance() public method to create this object if and only if there's no instance available, otherwise, just return the reference.



    Hey Kaz, I too have the B&S project. When you are making this singleton, in your getInstance method, are you testing to see if the Data object is null or the RAF is null? And what are you returning from the getInstance()? A Data object or a RAF object? Thanks!
     
    Kaz Yosh
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Daniel,

    First I made Data class singleton by making Data class instance variable static but later I changed to only RandomAccessFile singleton. The reason why I did this was that only RandomAccessFile should be protected. I might be wrong. What do you think?

    -kaz
     
    I just had the craziest dream. This tiny ad was in it.
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic