class instances ..

Discussion in 'C++' started by ma740988, Dec 9, 2005.

  1. ma740988

    ma740988 Guest

    Consider the case where I'm dealing with a vendor and a customer. The
    customer initiates a request to the vendor (really a multithreaded
    application hence semaphores are involved - in this case the customer
    gives a semaphore to vendor). The vendor upon receipt of the semaphore
    will do some 'processing'. When complete will do the same. ie. give a
    semaphore to the customer. So now:

    class vendor;
    class customer {
    vendor* ven;
    public:
    explicit customer ( vendor& ven_) : ven(ven_)
    };

    class vendor {
    customer *ptr_cust;
    public:
    explicit vendor() : ptr_cust(0)
    { ptr_cust = new customer (*this); } //<- scary object not fully
    constructed
    ~vendor() throw() { delete ptr_cust;
    ptr_cust = 0; //<- just in case }
    };

    For cases where both classes need to access member functions within
    each other. My thougths - in this case surrounds a higher level class
    - call it 'notify'. Both customer and vendor will register their
    objects with notify. This is where I'm 'stuck'. Not sure how to
    achive this. Source example would be helpful

    Thanks in advance.
    ma740988, Dec 9, 2005
    #1
    1. Advertising

  2. ma740988

    mlimber Guest

    ma740988 wrote:
    > Consider the case where I'm dealing with a vendor and a customer. The
    > customer initiates a request to the vendor (really a multithreaded
    > application hence semaphores are involved - in this case the customer
    > gives a semaphore to vendor). The vendor upon receipt of the semaphore
    > will do some 'processing'. When complete will do the same. ie. give a
    > semaphore to the customer. So now:
    >
    > class vendor;
    > class customer {
    > vendor* ven;
    > public:
    > explicit customer ( vendor& ven_) : ven(ven_)
    > };
    >
    > class vendor {
    > customer *ptr_cust;
    > public:
    > explicit vendor() : ptr_cust(0)
    > { ptr_cust = new customer (*this); } //<- scary object not fully
    > constructed
    > ~vendor() throw() { delete ptr_cust;
    > ptr_cust = 0; //<- just in case }
    > };
    >
    > For cases where both classes need to access member functions within
    > each other. My thougths - in this case surrounds a higher level class
    > - call it 'notify'. Both customer and vendor will register their
    > objects with notify. This is where I'm 'stuck'. Not sure how to
    > achive this. Source example would be helpful
    >
    > Thanks in advance.


    See the Mediator pattern in _Design Patterns_ by Gamma et al. It has
    sample code.

    Cheers! --M
    mlimber, Dec 9, 2005
    #2
    1. Advertising

  3. ma740988

    ma740988 Guest


    > See the Mediator pattern in _Design Patterns_ by Gamma et al. It has
    > sample code.
    >
    > Cheers! --M


    Of all the texts I possess, I don't think I have that one but will
    check. First I'll start with the web.

    The snippet below reflects an approach provided by a John Carson. A
    nicety but I trying to peruse the alternatives.


    class Client;
    class Resource
    {
    Client* m_client;
    public:
    void SetClient(Client* client)
    {
    m_client = client;
    }


    void Use()
    {
    // Use this resource
    // DoSomethingInteresting()
    }
    void AsyncNotification();
    };


    class Client
    {
    Resource* m_resource;
    public:
    Client(Resource* res) : m_resource(res)
    {
    m_resource->SetClient(this);
    }
    void Execute()
    {
    m_resource->Use();
    }
    void Notify()
    {
    // Handle notification
    // m_resource->Whatever()
    }
    };


    void Resource::AsyncNotification()
    {
    m_client->Notify();
    }


    int main()
    {
    Resource resource; // Define the resource
    Client client(&resource); // Configure the client to own the
    resource
    client.Execute(); // Execute operation, which in turn uses the
    resource

    //RunEventLoopForever();



    }
    ma740988, Dec 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. david

    multiple class instances

    david, Nov 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    morganm1
    Apr 16, 2004
  2. PJ6
    Replies:
    3
    Views:
    376
  3. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  4. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  5. Replies:
    8
    Views:
    459
    James Stroud
    Jan 29, 2009
Loading...

Share This Page