Hot answers tagged

10

"It depends" Compiler directives can be exceptionally useful tools. They can provide ironclad rules that keep development (debug) acceptable aspects out of production (release) builds. Likewise, an assembly can be made more lightweight by removing tracing instructions once the build is switched to release. On the other hand, they can be abused. I once ...


10

One of the most popular design pattern here on PO is the strategy pattern. And yes, if you build some example code around this pattern, you can demonstrate all the SOLID principles: S = is fulfilled when each strategy subclass is only responsible for exactly one task, and the "context" class does not take responsibilities which belong into the strategy ...


7

Logging is usually implemented with the Chain of responsibility pattern. Of course you can (and I would) combine that with a Facade. I really wouldn't use Listener(s) or Observer(s) myself.


7

You can: Use the features of Observer that you need, and ignore the other features; or Use some other software pattern that more closely matches what you need, or Don't use a pattern at all, and simply write code that solves the problem. Just because the Wikipedia article says that the Observer pattern is designed to notify the observer of state changes ...


6

I think this is the kind of reason WebSockets were created for. If you don't need compatibility with older browsers you might want to use this instead of polling. You should probably use polling as a fallback anyway. I'm not sure how stable the spec/implementations are these days (it was not very stable not so long ago). It seems the latest spec is ...


6

There is no delegate pattern per se. I am going to assume you mean Delegation Pattern. As I understand it, they're the complete reverse of each other and used for different purposes. Generally, with an Observer Pattern, any number of observer objects will listen to an event on a second object and act on the event. The second object has no knowledge of its ...


6

I think you're taking this from a backwards approach. Anti-patterns are a way of naming things that are known to have bad characteristics - they are diseases, not symptoms. It's really the symptoms we care about, even if we haven't named this specific disease yet. The way you should be talking about this is in terms of actual functional defects or deficits ...


6

When implementing the Observer pattern, there are two main approaches to consider: the 'push' model and the 'pull' model. In the 'push' model, the subject (i.e. the Observable) sends the observer on notification all the data it will need. The observer doesn't need to query the subject for information. In the 'pull' model, the subject merely notifies the ...


5

Your idea was already invented 10 years ago by Herb Sutter. See this article: http://www.drdobbs.com/cpp/generalizing-observer/184403873, it contains a full explanation. So the answer is yes, it is an improvement over the original GOF variant of the pattern no, it is not better than every other known implementation of the Observer pattern Also note ...


5

You're looking at things incorrectly. An observer sees that a particular event occurs. It does not impact it, or own it. A delegate handles a particular event, and has ownership of the handler, even if the delegator owns the interface to the event.


5

By using the observer pattern, A needs not know about B or C. If you explicitly had A call B and C then it would necessarily know about B and C. Depending on how you do it, B and C don't even know about A, some D ties them all together. That "lack of knowledge" is at the core of decoupling components.


4

An alternative would be as follows. Observers are notified of the event through the method // Method on all observers. void notify(Event event) All new objects are subscribed to the notification, so the subject, instead of if (subject.eventAlreadyHappened()) { observer.executeStuff(); } else { subject.subscribe(observer); } does simply ...


4

You should use some kind of ajax push (see Comet) to notify the clients. It can eliminate the need for polling, you basically keep an established connection open which can be used to notify the browser of ocurring events. However, this does not work with all browsers. You fall back to "long polling" in such cases.


4

If the two Combatant instances and Weather object are notified as the result of the clock tick count being updated, and not by direct intervention by the Fight object, then it's the Observer pattern. If your Fight object triggers the notifications directly you can use the Command pattern to encapsulate the receivers or have the Fight object send messages ...


4

The logic is riddled with compiler directives that change it's internal behavior based on which .Net project it happens to be compiled in. Compiler directives aren't the problem, they're just the means used to create the alleged problem. You'd probably have the same objection if you had to comment out and uncomment various blocks of code based on what ...


4

Whether C++ or JAVA, a Notification to the observer can come along with the information of what is changed. The same methods notifyObservers(Object arg) can also be used in C++ as well. Generally, the issue will remain is that there could be multiple subjects dispatching to one or multiple observers and hence, the class arg cannot be hard coded. Usually, ...


4

The two interfaces are actually part of Reactive Extensions (Rx for short), you should use that library pretty much whenever you want to use them. The interfaces are technically in mscrolib, not in any of the Rx assemblies. I think this is to ease interoperability: this way, libraries like TPL Dataflow can provide members that work with those interfaces, ...


4

You are quite close to answering your own question. :) In the Observable/Observer pattern (note the flip), there are three things to bear in mind: Generally, the notification of the change, i.e. 'payload', is in the observable. The observable exists. The observers must be known to the existing observable (or else they have nothing to observe on). By ...


4

Actually, I think you've got it backwards. At the low level of the internet, there is no such thing as polling. Everything on the internet actually pushes. Internet messages are made of packets, which are basically bunches of electrons travelling down wires. From the computer's perspective, packets simply show up either as electrons coming down a wire or ...


3

That's a question of several trade-offs. Trade-offs: flexibility (in terms of having n > 1 delegates/observers) cost of sending a message resilience (ability to sustain unavailability of delegate/observers) ease of use Delegate pattern: not very flexible - adding more than 1 delegate is not possible (implies some form of "multi-delegate" i.e. ...


3

Your design doesn't really violate the Observer pattern, because the pattern does not specify how subscribers get registered with the publisher. But you are violating the Single Responsibility Principle for those middle panels, because they are given the added responsibility of helping in getting the message tray subscribed. A better design would be to use ...


3

You could solve this issue by making Point an immutable object. Immutable objects are objects with read-only properties which are set at creation and can not change during the lifetime of the object. This might seem limiting at first, but it greatly simplifies code because you have less effect-at-a-distance. When you need to change a position, you would ...


3

Microsoft Access(2010+) have data macros which are like triggers, but I don't think they run unless an Access application is running or if an ODBC connection can recognize them. You may not have the ability to add these. It's not too much to have a service pole the database. As far as your performance concerns, the frequency may need to be less than you ...


3

The idea of the Observer pattern is to allow an object to register for updates from another object, without the other object having to know about the actual type of the observer. In statically typed languages such as Java or C++, this is done by having the class of the object that needs to register for updates, inherit from the Observer class or implement ...


3

Sure, these things exist. D-BUS comes to mind, or any number of messaging frameworks (RabbitMQ , AMQP, ZeroMQ, etc). They're all variations on a similar theme. Increased latency compared to directly-connected peers, and increased complexity for complex topologies. The biggest one I can think of is the ability to extend to multiple WANs and scale to massive ...


3

No, having the View observe the Model (or perform other read actions on the Model) is a valid implementation of the MVC pattern. There are two main ways that the MVC pattern is typically implemented and they differ mostly in how the information from the Model gets to the View. In the first form, all information passes through the Controller, who is made ...


3

There's a nice video from Apple's WWDC about this subject. Solution is simple: Stored somewhere is the truth. The truth is not a number, it is a pair (number, currency). If a user enters a number into a Yen field, the truth is changed to X Yen. When a dollar field is notified, it doesn't change the truth to dollars. It displays the amount as best as it can,...


3

This doesn't look like a reasonable place to apply the Observer Pattern. What I do notice is that all of your examples reference local state, so I assume they are part of some sort of Meeting object. Consequently, the code public void SetMeetingDate(DateTime date) { if (!MeetingRoom.AllEmployeesAreReady) { throw new ApplicationException("...


3

You should go with your first solution. Your first solution is better because it's simpler (but maybe show the user a message instead of throwing an exception if it seems more appropriate). Head First Design Patterns says it best: First of all, when you design, solve things in the simplest way possible. Your goal should be simplicity, not "how can I ...



Only top voted, non community-wiki answers of a minimum length are eligible