Click here to Skip to main content
15,918,808 members
Home / Discussions / C#
   

C#

 
AnswerRe: Near-static class Pin
Ed.Poore13-Jun-08 20:50
Ed.Poore13-Jun-08 20:50 
GeneralRe: Near-static class Pin
PIEBALDconsult14-Jun-08 5:12
mvePIEBALDconsult14-Jun-08 5:12 
GeneralRe: Near-static class Pin
Ed.Poore14-Jun-08 8:12
Ed.Poore14-Jun-08 8:12 
GeneralRe: Near-static class Pin
PIEBALDconsult14-Jun-08 18:45
mvePIEBALDconsult14-Jun-08 18:45 
GeneralRe: Near-static class Pin
Ed.Poore14-Jun-08 21:18
Ed.Poore14-Jun-08 21:18 
GeneralRe: Near-static class Pin
PIEBALDconsult15-Jun-08 7:45
mvePIEBALDconsult15-Jun-08 7:45 
GeneralRe: Near-static class Pin
Ed.Poore15-Jun-08 9:08
Ed.Poore15-Jun-08 9:08 
GeneralRe: Near-static class Pin
PIEBALDconsult16-Jun-08 11:05
mvePIEBALDconsult16-Jun-08 11:05 
I can't think of any simpler examples so I'll just describe what I'm doing.

I'm writing a generic pool. Each item in the pool gets wrapped and added to a HashSet. The class is therefore a factory of these items as well. So, because it's a factory and there shouldn't be more than one pool of a given type in the appdomain, making the class a singleton seems appropriate.

public class GenericPool<T>
where T : class , new()
{
    private sealed class GenericPoolItem : IGenericPoolItem<T>
    {
        private T item = new T() ;

        // Some other methods
    }

    private static readonly System.Collections.Generic.HashSet<GenericPoolItem> items =
        new System.Collections.Generic.HashSet<GenericPoolItem>() ;

    private static readonly System.Collections.Generic.List<GenericPoolItem> freelist =
        new System.Collections.Generic.List<GenericPoolItem>() ;

    public static IGenericPoolItem<T>
    Reserve
    {
        get
        {
            GenericPoolItem result = null ;

            lock ( items )
            {
                if ( freelist.Count == 0 )
                {
                    result = new GenericPoolItem() ;

                    items.Add ( result ) ;
                }
                else
                {
                    result = freelist [ 0 ] ;

                    freelist.RemoveAt ( 0 ) ;
                }
            }

            result.Reserve() ;

            return ( result ) ;
        }
    }

    // Some other methods
}


There is also an interface to expose only two members of the wrapper; Value and Free.

To use the class(es), one need only call Reserve, use the wrapped item, and then Free the wrapped item.

IGenericPoolItem<Widget> item = GenericPool<Widget>.Reserve ;
 
// do something with item.Value
 
item.Free() ;


(I'll likely rename Reserve or change it to a method; it looks wrong as it is.)


For that I could use a static class, but as mentioned, I want to be able to derive.
Why? Well, one of the particular types I want to pool is StringBuilder (I still have yet to determine whether or not there's any performance benefit of that).

In the above snippet, Widget could be StringBuilder and it will work just fine except I would need to clear the StringBuilder each time I Reserve it. If I derive, I can override (kinda sorta) Reserve to perform the clearing.

public class StringBuilderPool : PIEBALD.Types.GenericPool<System.Text.StringBuilder>
{
    public new static PIEBALD.Types.IGenericPoolItem<System.Text.StringBuilder>
    Reserve
    {
        get
        {
            PIEBALD.Types.IGenericPoolItem<System.Text.StringBuilder> result =
                PIEBALD.Types.GenericPool<System.Text.StringBuilder>.Reserve ;

            result.Value.Length = 0 ;

            return ( result ) ;
        }
    }
}



The classes work as I want. But in order to derive, the base class can't have a private constructor (which is usual for a singleton), the deriving class needs access to the base class' constructor, so I make it protected. But a deriving class could make its constructor public, undoing my efforts.

My solution is to have the constructor throw an exception:

protected GenericPool
(
)
{
    throw ( new System.InvalidOperationException
        ( "Instantiating GenericPool is forbidden." ) ) ;
}


but I would prefer a compile-time error or something less heavy-handed.

I could, of course, just let people instantiate it, but the instances would be fairly useless without instance members... always remembering that probably no one else will use this anyway. Blush | :O
GeneralRe: Near-static class Pin
Ed.Poore16-Jun-08 11:15
Ed.Poore16-Jun-08 11:15 
GeneralRe: Near-static class Pin
PIEBALDconsult16-Jun-08 12:41
mvePIEBALDconsult16-Jun-08 12:41 
GeneralRe: Near-static class Pin
Ed.Poore16-Jun-08 13:21
Ed.Poore16-Jun-08 13:21 
GeneralRe: Near-static class Pin
PIEBALDconsult16-Jun-08 14:01
mvePIEBALDconsult16-Jun-08 14:01 
GeneralRe: Near-static class Pin
Ed.Poore16-Jun-08 14:15
Ed.Poore16-Jun-08 14:15 
GeneralRe: Near-static class Pin
Ed.Poore16-Jun-08 14:17
Ed.Poore16-Jun-08 14:17 
GeneralRe: Near-static class Pin
PIEBALDconsult17-Jun-08 6:24
mvePIEBALDconsult17-Jun-08 6:24 
GeneralRe: Near-static class Pin
Ed.Poore17-Jun-08 6:26
Ed.Poore17-Jun-08 6:26 
AnswerRe: Near-static class Pin
leppie13-Jun-08 21:49
leppie13-Jun-08 21:49 
GeneralRe: Near-static class Pin
PIEBALDconsult14-Jun-08 4:39
mvePIEBALDconsult14-Jun-08 4:39 
AnswerRe: Near-static class Pin
Lutosław14-Jun-08 22:00
Lutosław14-Jun-08 22:00 
GeneralRe: Near-static class Pin
PIEBALDconsult15-Jun-08 7:31
mvePIEBALDconsult15-Jun-08 7:31 
AnswerRe: Near-static class Pin
#realJSOP15-Jun-08 3:49
professional#realJSOP15-Jun-08 3:49 
GeneralRe: Near-static class [modified] Pin
PIEBALDconsult15-Jun-08 7:31
mvePIEBALDconsult15-Jun-08 7:31 
QuestionList of arbitrary objects Pin
Jammer13-Jun-08 11:20
Jammer13-Jun-08 11:20 
AnswerRe: List of arbitrary objects Pin
PIEBALDconsult13-Jun-08 13:01
mvePIEBALDconsult13-Jun-08 13:01 
GeneralRe: List of arbitrary objects Pin
Jammer13-Jun-08 13:24
Jammer13-Jun-08 13:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.