Design patterns help

Soldato
Joined
18 Oct 2002
Posts
9,044
Location
London
We're doing design patterns at uni and I'm finding it hard to relate the theory of these patterns into practical use (Java).

I don't find the programming side of things hard, but I do tend to "jump in" (who doesn't?). Unfortunatly for the next assignment I have to work out at least a couple of patterns to use BEFORE (:eek:) writing any code.

So some simple examples would probably help me here.
Don't suppose anyone has used any helpful books on this? Pref. Java related.
Many thanks :)
 
I assume that you've got (and have read) the gang of four's book?

if you could give us an idea of the problem space we might be able to sugest a sutiable pattern(s) for you. some of them (factory, decorator, facade etc) are used in most projects, others (Memento, adaptor) I don't think I've ever used.

ISBN 0-201-63361-2 for THE design patterns book.

Paul
 
I've found that the really, two really common design patterns that I seem to use in almost any program, large or small, are Singleton and Factory. I'd be very surprised if you can't use those sensibly in even a fairly basic project.

Another option might be Flyweight, which is a pretty straightforward one, or Composite if you're dealing with a lot of tree structures :)

The Gamma / Helm / Johnson / Vlissides book is a good place to get started if you haven't already read it, as happytechie suggests.

arty
 
http://www.amazon.co.uk/Enterprise-...1/ref=pd_bbs_sr_3/026-1047415-2945203?ie=UTF8

Arguably the best Design Patterns book out there.

The most common Pattern you'll ever see in every application is the Front Controller. If you don't think you have one, you have, you just don't know it. :)

Common Patterns:

Front Controller (and Page Controller to a lesser extent)
Factory
Registry
Settings Object/Pattern
Singleton

probs loads more but I'm too narked at Nildram to think straight atm.
 
For examples.. (ignoring try {} catch)

A simple singleton:

Code:
class MyClass
{
    private static MyClass instance;

    public static MyClass getInstance()
    {
        if (instance == null) 
        {
            instance = new MyClass();
        }
        return instance;
    }

     // rest of class..
}

Factory using singleton..

Code:
import java.util.*;

class Factory
{
    private static Factory instance;

    public static Factory getInstance()
    {
        if (instance == null)
        {
            instance = new Factory();
        }
        return instance;
    }
    
    // using reflection..
    public Object manufacture(String className)
    {
        Class c = new Class.forName(className);
        return c.newInstance();
    }
}

That's all I cba to type for now :p

There is shedloads of sites out there for patterns.. most have examples in Java, which is nice. :)
 
Back
Top Bottom