Archive for the 'Software' Category

 

Design Patterns – Strategy pattern (Behavioral) basic example..

May 08, 2007 in Engineering, Software

A possible usage scenario without thinking ‘Strategy’ ..


// Example without using the Strategy pattern

public class WithOutStrategy { 

    public void doSomething(int theSwitch) { 

        if ( theSwitch == 0 ) { 
            AClass ac = new AClass(); 
            ac.doit(); 
        } 

        if ( theSwitch == 1 ) { 
            BClass bc = new BClass(); 
            bc.doit(); 
        } 

    } 

    public static void main(String[] args) { 

        WithOutStrategy sb = new WithOutStrategy(); 
        sb.doSomething(0); 
        sb.doSomething(1); 
    } 

} 

class AClass { 

    public void doit() { 
        System.out.println(">> doit A"); 
    } 

} 

class BClass { 

    public void doit() { 
        System.out.println(">> doit B"); 
    } 

}

And a better way of doing it with ‘Strategy’..


// Example using Strategy

public class WithStrategy { 

    public void doSomething(OClass oc) { 
        oc.doit(); 
    } 

    public static void main(String[] args) { 

        WithStrategy ows = new WithStrategy(); 
        OClass oc = null; 

        oc = new AClass(); 
        ows.doSomething(oc); 

        oc = new BClass(); 
        ows.doSomething(oc); 
    } 

} 

// Options class 

abstract class OClass { 
    public abstract void doit(); 
} 

class AClass extends OClass { 

    public void doit() { 
        System.out.println(">> doit A"); 
    } 

} 

class BClass extends OClass { 

    public void doit() { 
        System.out.println(">> doit B"); 
    } 

}

Notes (Part 2) from HeadFirst Java..

May 08, 2005 in Books, Java, Software




  • — Some methods of class Object
    equals(), getClass(), hashCode(), toString().
  • — A way of thinking about superclasses vs interfaces:
    Superclasses implement types, interfaces implement roles.
  • — About the Constructor
    — It is used to initialize the state of a new object.
    — If none is written, the compiler will create a default one (with no args.)
    — If one is written (any one) the compiler will not supply the default one.
    .. ergo ..
    — If a no-arg one is wanted and there is at least another one, the default
    one must be written.
    — It is good to always supply a default one with default values.
    — Overloaded ones must have different arguments (just like any other method).
    — Argument list concept includes order and/or type of arguments.
    — If the nature of the class is such that the object MUST be initialized
    (such as the Color class), do not write a default one.
  • — Every constructor can call super() or this() but never both.
  • — To prevent a class from being instantiated into objects,
    mark the constructor ‘private’. For example, many ‘utility’
    classes have only ‘static’ methods and they should not be
    instantiated.
  • — Finals..
    — A final variable cannot have its value changed.
    — A final method cannot be overriden.
    — A final class cannot be extended.
  • — Serialization..
    — Write object basic pattern:

    .. class SomeClass implements Serializable ..
    SomeClass sc = new SomeClass();
    try {
      FileOutputStream fs = new FileOutputStream("sc.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fs);
      oos.writeObject(sc);
      oos.close();
    }
    catch(Exception exc) {
      ...
    }
    

    — If a variable in a ‘Serializable’ class cannot (or should not)
    be serialized, it must be marked ‘transient’.

    — Read object(s) basic pattern:

    FileInputStream fis = new FileInputStream("foo.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object one = ois.readObject();
    Object two = ois.readObject();
    ...
    SomeClass sc = (SomeClass) one;
    SomeOtherClass soc = (SomeOtherClass) two;
    ...
    ois.close();
    

    — Static variables are not serialized.

    — Class changes (versions) may break serialization.

  • — Java new IO (nio) features
    — IO performance improvements by using
    native IO facilities.
    — Direct control of buffers.
    — Non-blocking IO capabilities.
    — Existing ‘File[Input|Output]Stream classes
    use NIO internally. Some NIO features may be
    accessed via the ‘channels’.