2008年6月15日星期日

Implementation with one instance for each subclass

  1. The following implementation ensure there is one instance for each SubClass
    Create abstract SuperClass // forbid initiate
    create protected constructor // only access by subclass
    create public access to instance field //
    defien subclassol
    create private constructor // only access by subclass
    override public method to access to instance field //

public abstract SuperClass{

protected SuperClass(){

// interface to subclass

}//:f

public static SuperClass(){

return instance;

}//:f

}//:c

public SubClass1 extends SuperClass{

private static SubClass instance=null;

private SubClass1(){

//prevent initiation

}//:f

public static SuperClass(){

if(instance==null){

instance=new SubClass1();

}//:if

}//:f

}//:c

Implementation With only one SubClass

How can we make Singleton with subClass
The following implementation ensure there is only SubClass instance.
  1. Create abstract SuperClass // forbid initiate


    1. create protected instance field // only access by subclass

    2. create protected constructor // only access by subclass

    3. create public access to instance field //


  2. defien subclassol

    1. create private constructor // only access by subclass

    2. override public method to access to instance field //




public abstract SuperClass{
protected static SuperClass instance=null;
protected SuperClass(){
// interface to subclass
}//:f
public static SuperClass(){
return instance;
}//:f
}//:c
public SubClass1 extends SuperClass{
private SubClass1(){
//prevent initiation
}//:f
public static SuperClass(){
if(instance==null){
instance=new SubClass1();
}//:if
}//:f
}//:c

2008年6月14日星期六

staticMethodSingleton-lazyInitiation

// lazy initiation
final class Singleton{private static Singleton SINGLETON=null;private Singleton(){//prevent initiation outside}//:fpublic static Singleton(){
if (SINGLETON==null){
SINGLETON=new Singleton();
}return SINGLETON;}//:fpublic Singleton clone(){throw new CloneNotSupportedException();}//:f}//:c

staticMethodSingleton-eagerInitiation

// eager initiation
final class Singleton{
private static Singleton SINGLETON=new Singleton()
private Singleton(){
//prevent initiation outside
}//:f
public static Singleton(){
return SINGLETON;
}//:f
public Singleton clone(){
throw new CloneNotSupportedException();
}//:f
}//:c

StaticClassSingleton

//define
final class Singleton{
public static void process{...}
}//:c
//usage
.....
Singleton.process(....);
.....

Singleton

  1. Intent - Ensure a class only has one instance, and provide a global point of access to it
  2. Motivation-
    Sometimes we want just a single instance of a class to exist in the systemé For example, we want just one window manager. Or just one factory for afamily of products.é We need to have that one instance easily accessible And
    we want to ensure that additional instances of the class can not be created

  3. Structure



  1. Implementation

There are two ways to implement Singleton. One is static Class and another is staticmethod.

  • The static class way
    declares the singleton class as final and all its methods are declared static, meaning that the class cannot be extended. One advantage of the final class approach is that you can use the class name directly to call the its method. However, you have to reprogram when you would like to drop the restrictions of Singleton status.
  • The static method way
    The key to creating a singleton is to prevent the client programmer from having any way to create an object except the ways you provide.
    You must make all constructors private, and you must create at least one constructor to prevent the compiler from synthesizing a default constructor for you (which it will create using package access).
    In Java , an object can be created cloning. The cloning is able to be prevented by making the class final. prevents cloning. However, if you’re inheriting from a class hierarchy that has already overridden clone( ) -a spublic and implemented Cloneable, you have to override clone( ) and throw a CloneNotSupportedException as described Actually, this isn’t precisely true, because even inthe above situation someone could still use reflection to invoke clone( ) [[is this true? clone( ) is still protected so I’m not so sure. If it is true, you’d have to throwCloneNotSupportedException as the only way to guarantee un-cloneability.

At this point , you can use the following template to create a singleton

1 define the final class Singleton

2 define a static Singleton Type member

3 private the default constructor to prevent initiate

4 create an static method allow client to get the static member.

5 override clone() to throw CloneNotSupportedException

There are two way in Singleton Class to initiate the static member. Eager way and lazy way.

Lazy way
can create the member on demand . However there could be more than one instance if two or more thread can instance() concurrently.

Eager way
can solve this problem to ensure there is only one instance in the program no matter how many threads call the instance().

Another method is sychronize the instance() but with much more cost

Reference

[1]http://www.patterndepot.com/put/8/Creational_Patterns.htm

Behavior Pattern

Behavioral Pattern is to design objects that handle particular types of actions within a program. They encapsulate processes that you want to perform, such as interpreting a language,fulfilling a request, moving through a sequence (as in an iterator), or implementing an algorithm.

Structural Pattern

Structural Pattern is used to design objects to satisfy particular project constraints. It abstract the connection between components of system so that the changes in the system don’t require changes to those connections

2008年6月13日星期五

Creational Pattern

In java, the simplest way to create an instance of an object is by using the new operator.

       Fred = new Fred(); //instance of Fred class 

However, this simple way couples the way to create an object with your program. It can not satisfy the variation of exact nature of object creation from time to time in the program in many cases. Creational Pattern abstract the creation process to make your program more flexible and general, because your program will not depend on how objects are created and arranged.
It isolate the details of object creation so your code isn’t dependent on what types of objects there are and thus doesn’t have to be changed when you add a new type of object.
The following are individual Creational Patterns which solve different problems when you create objects in a program .
The Singleton Pattern is intent to ensure a class only has one instance, and provide a global point of accessto it.

The Factory Method provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
The Abstract Factory Method provides an interface to create and return one of several families of related objects.
The Builder Pattern separates the construction of a complex object from its representation, so that several different representations can be created depending on the needs of the program.
The Prototype Pattern starts with an initialized and instantiated class and copies or clones it to make new instances rather than creating new instances.

PatternResource

Pattern Course
UML Tutorial
Design Pattern

Design Patterns are mid-level abstractions that
generally focus on the interaction among various
components or objects


Architectural Patterns are abstracted another level up
from design patterns, and often focus on integration
of component tiers


  1. Design Patterns
    1. Creational Patterns
      1. AbstractFactory
        Example
      2. Builder
        Example
      3. factory
        Example
      4. Prototype
        Example
      5. Singleton
        Example
      6. Composite
        Example

    2. Patterns for Organization of Work
      1. observer
      2. Chain of Responsibility
        Example
      3. Mediator
        Example

    3. Access Control Patterns
      1. Proxy
        Example

      2. facade

      3. Iterator

    4. Service Variation Patterns
      1. Bridge
        Example
      2. State
        Example
      3. Strategy
      4. Template


    5. Service Extension Patterns
      1. Decorator
        Example
      2. visitor
        Example

    6. Object Management Patterns
      1. Command
      2. memento
        Example

      3. flyweight

        Example
      4. Interperter
        Example


    7. Adaptation Patterns
      1. Adapter
        Example

    8. Communication Patterns
      1. Forwarder-Receiver
      2. Client-Dispatcher-Server


  2. Architectural Patterns
    1. Structural Patterns
      1. Layers
      2. Pipes and Filters
      3. Blackboard Architecture

    2. Patterns for Distribution
      1. Broker
      2. Message Queues

    3. Patterns for Interactive Systems
      1. Model-
        View-Architecture (borderline)

        Example

      2. Presentation-Abstraction-Control

    4. Adaptable Systems
      1. Microkernel
        and Reflection
      2. Reflection

    5. Frameworks and Patterns
      1. Idea of frameworks
      2. Patterns for flexibility
      3. Achieving benefits of frameworks
      4. Failures of frameworks

    6. Analysis Patterns
      1. Reuse of models
      2. Achieving generality


Design Pattern

A System consists of many components which are connected and collaborate to work together to finish a mission. When a program is designed to emulate the system, a lot of tasks of program design fall into 3 broad categories: creation of the components, connections of components and emulate the behaviors of these componnets. In order to separate fixed stuff from the changes among these tasks , Creational Pattern, Structural Pattern and Behavior Pattern are applied respectively.