2008年6月15日星期日

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

没有评论: