dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Looking for a suitable design pattern...

  1. #1
    Join Date
    Apr 2004
    Posts
    123

    Looking for a suitable design pattern...

    Hi,

    I'm looking for a specific design pattern or a solution which can solve my problem..

    I have a base class with 3 derived classes.Let us say A,B,C are derived classes and Base is the base class.

    For all the 3 derived classes i need to fetch data from a server,where A and B are operating upon the same set of data...while C is operating on a different set of data.

    In the base class Base i have made the data retrievel method as virtual,so that intrested dervied classes can implement their own retrievel and operations upon their set of data.

    Problem is since A and B operate upon the same set of data...i need to retrieve data only once for both the subclasses...and the datastructure which stores the data should be shared between the instances of A & B,wherin C is free to retrieve and operate independently.

    Any pointers on how exactly should i implement it or which design pattern should i use???

    Time being let us assume that all instances are singleton.

    Thanks in advance,
    Mmx

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Looking for a suitable design pattern...

    Make 'D' inherit from 'Base' and implement the store/retrieve method, then make 'A' and 'B' inherit from that.

    'C' can inherit dirctly from 'Base'.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Apr 2004
    Posts
    123

    Re: Looking for a suitable design pattern...

    Well..that is where exactly the problem is...

    Find the code & comments below..

    Code:
    class Program
        {
            static void Main(string[] args)
            {
                Base derived1 = new Derived1();
                derived1.GetData();
    
                Base derived2 = new Derived2();
    
                //I cant do this...as the retrievel is a costly operation 
                //and the string dataretrieved should be shared between
                //derived2 and derived1 instances...
               // derived2.GetData();
    
                Base derived3 = new Derived3();
                derived3.GetData();
    
                Console.Read();
            }
        }
    
        public class Base
        {
            public string dataRetrieved = string.Empty;
    
            public virtual void GetData()
            {
                dataRetrieved = "Base get data called";
                Console.WriteLine(dataRetrieved);
            }      
        }
    
    
        public class Derived3:Base
        {
            public override void GetData()
            {
                dataRetrieved = "Derived3 getdata called";
                Console.WriteLine(dataRetrieved);
            }
          
        }
    
    
        public class Intermediate : Base
        {     
            public override void GetData()
            {
                dataRetrieved = "Intermediate getdata called";
                Console.WriteLine(dataRetrieved);
            }     
        }
    
    
        public class Derived1 : Intermediate
        {
            public override string ToString()
            {
                return "Derived1";
            }
        }
    
    
        public class Derived2 : Intermediate
        {
            public override string ToString()
            {
                return "Derived2";
            }
        }
    As evident from the code,i can't call getdata for derived2 instance,since it's a lengthy op and the data is same for both derived1 and derived2...once derived1.getdata is called the string value should be shared by both derived1 instance and derived2 instance.I know i can pass the value to the constructor of derived2...is there any better solution other than that..I read some where about Indirection...how to apply it over here???
    Last edited by mmx_nexus; April 16th, 2008 at 07:53 AM.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Looking for a suitable design pattern...

    Code:
        public class Intermediate : Base
        {
            private static string data = string.Empty;
    
            protected string Data
            {
                get { return data; }
            }
            public override void GetData()
            {
                if (data == string.Empty)
                {
                   // do the expensive data retrieval here
                   // this means it will only happen once
                }
                dataRetrieved = "Intermediate getdata called";
                Console.WriteLine(dataRetrieved);
            }     
        }
    
    
        public class Derived1 : Intermediate
        {
            public override string ToString()
            {
                return "Derived1";
            }
        }
    
    
        public class Derived2 : Intermediate
        {
            public override string ToString()
            {
                return "Derived2";
            }
        }
    Essentially you modify the Intermediate class to only get and maintain one copy of the data. The classes derived from Intermediate would only get the data once. At other times the call would be redundant.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Looking for a suitable design pattern...

    Or split data retrieval and data processing into separate classes.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)