class instances ..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ma740988

    #1

    class instances ..


    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.

  • mlimber

    #2
    Re: class instances ..

    ma740988 wrote:[color=blue]
    > 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.[/color]

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

    Cheers! --M

    Comment

    • ma740988

      #3
      Re: class instances ..

      [color=blue]
      > See the Mediator pattern in _Design Patterns_ by Gamma et al. It has
      > sample code.
      >
      > Cheers! --M[/color]

      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(Clien t* client)
      {
      m_client = client;
      }


      void Use()
      {
      // Use this resource
      // DoSomethingInte resting()
      }
      void AsyncNotificati on();
      };


      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::Async Notification()
      {
      m_client->Notify();
      }


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

      //RunEventLoopFor ever();



      }

      Comment

      Working...