Posts

Showing posts from September, 2015

Architectural pattern: publisher-subscriber

Image
Introduction This pattern is also know as ’Observer’ (GoF). The mainly difference is that in architectural patterns there is a distinction for what is a components and what is a connectors. Here i want show just a basic working without this distinction. Besides i want show how implement it using the API of java, utility class that simplify the java implementation. What is the problem I have one or more objects (observer or listner or subscriber) that are interested to know when there are some events from another object (subject or publisher) and they want be informed when it happen. When that event happen the subscriber can perform their action that can be different from subscriber to subscriber. How the publisher-subscriber work The first thing to do for the subscribers (or observers) is registering to the publisher (or subject) so they can be notified when a event happen. To do that they just use attach() method of ConcreteSubject. When there is an event (that maybe als...

Concurrency pattern: Producer-consumer

Image
What is the problem Basically this pattern solve the problem of a buffer, in which one or more Producer can push a data and one or more Consumer can pull a data. There are a couple of problems that can arise: the first is catch to ’race condition’, indeed since to the buffer can access more productors and consumers at the same time, can arise problems of inconsistency data. The second problem is catch to ’deadlock’, mean a lock of system. How this pattern work In this pattern, the problem of ’race condition’ arise, for example, if we allow to do, at the same time, push and pull operation. In this case, since both methos changes the internal state of the buffer object, they can read not right data. Generally to avoid ’race condition’ problems on an obbject it’s enough use the ’single threaded execution’ pattern. Single threaded execution Basically, in our case, the ’single threaded execution’ allows to the methos of object of type Buffer, signed as ’guarded’, may be executed b...