Design patterns are pretty boring. Experimenting with design patterns? That's where the fun is!
While messing about with spreadsheet formatting objects (e.g. date, text, numeric), it occurred to me that there might be a Dartier (cooler) solution than what I am currently using:
abstract class CellFormatter { CellFormatter nextHandler; void processRequest(Event e) { if (handleRequest(e)) return; if (nextHandler == null) return; nextHandler.processRequest(e); } // Subclasses handle requests as needed bool _handleRequest(Event e) => false; // ... }In this handler, I am both implementing and connecting the successors. The
nextHandler
property points to the next object in the chain. The processRequest()
method is responsible for sending requests to that next object when the current object opts against handling the request (e.g. when a number formatter sees text in a cell). There is nothing wrong with this approach, but I have a nagging dislike for the naming convention that I have here. One method is called
processRequest()
and the other is _handleRequest()
. "Process" and "handle" both have similar meanings so it takes a little extra noodling here to understand what is happening. Since processRequest()
is public, that is a good indication that it is the public interface to the chain. Less obvious is that processRequest()
is responsible for connecting to the successor whereas the _handleRequest()
is meant solely for subclasses to actually handle the request.The main problem here is the dual nature of
processRequest()
. To the outside world, it is… processing the request, so it is well-named in that respect. Actually looking at the code, however, it serves the different purpose of linking successors. And naming _handleRequest()
as I have only adds to the confusion. This is not horrible, but I can do better.Perhaps renaming
processRequest()
as just process()
will help. To the outside world, it retains a similar connotation. Inside the code, it seems more distinct from _handleRequest()
. Or maybe…I can rename it as
call()
.The beautiful thing about a method named
call()
in Dart is that automatically makes the current class into a function. That means that I can continue sending the request to the chain successor as with nextHandler.call(e)
or I can treat nextHandler
as a function itself, nextHandler(e)
:abstract class CellFormatter { CellFormatter nextHandler; void call(Event e) { if (_handleRequest(e)) return; if (nextHandler == null) return; nextHandler(e); } // ... }And, since the
call()
happens to have the correct signature for event listeners, I can supply the first object in the chain directly to the on-change listener: var textFormat = new TextFormatter();
var dateFormat = new DateFormatter(textFormat);
var numberFormat = new NumberFormatter(dateFormat);
container.onChange.listen(numberFormat);
That reads much better—and it wasn't horrible to begin with! When the container sees a change event, it number formats the appropriate cell. The class code is similarly clear: when called, it either handles the request or calls the next successor. Wonderful!I had thought about moving on from last night's "good enough" chain of responsibility. I am certainly glad I opted to linger a bit longer, this was a nice win. I do wonder if this is the gods' way of telling me to linger more often. Or maybe to
call()
more often. I'm sure one of those is the moral of this story.Play with the code on DartPad: https://dartpad.dartlang.org/f5bbc86a28002987741c.
Day #101
MY HOMEPAGE1
ReplyDeleteMY HOMEPAGE2
MY HOMEPAGE3
MY HOMEPAGE4
MY HOMEPAGE5
google 796
ReplyDeletegoogle 797
google 798
google 799
google 800
google 3113
ReplyDeletegoogle 3114
google 3115
google 3116
google 3117
google 4147
ReplyDeletegoogle 4148
google 4149
google 4150
google 4151
google 4152
google 3502
ReplyDeletegoogle 3503
google 3504
google 3505
google 3506
google 3507
Thanks for this amazing article.
ReplyDeletebackup cameras in New Jersey
residential window tinting bronx new york
car audio patterson new jersey
residential window tinting in new york
This comment has been removed by the author.
ReplyDeletehotmail.com brand continues to be hampered by that history in the minds of many users.
ReplyDeleteHere’s a comparison of the Hotmail look compared to the new Outlook.com design brand continues to be hampered by that history in the minds of many users.
Here’s a comparison of the Hotmail look compared to the new Outlook.com design
facebook.com today unveiled its vision for the future of work: a virtual reality experience called Horizon Workrooms that uses avatars and VR headsets to replicate in-person collaboration.
ReplyDeleteThe tech giant is hoping to ride pandemic-driven tailwinds that have sparked remote work and adoption of VR, particularly for Facebook’s Oculus subsidiary.
Horizon Workrooms works both across virtual reality and the web, and uses features such as digital whiteboards, hand tracking, spatial audio, and more to help colleagues work together, even if they are physically apart. Other companies including Microsoft have unveiled similar experiences recently.
It's both a blessing and a curse that I not only fully understood aol mail what was being said but also remembered my father acting in a similar fashion when I was a kid.
ReplyDeletethe best email is yahoo and rediffmail
ReplyDeleteEven though I mostly use gmail.com for work-related correspondence, I have kept my Yahoo account from the middle of the millennium since I never found a method to completely eliminate spam from it.
ReplyDeletehey guys share information related to e shram card
ReplyDeleteThe Microsoft msn.com mobile app provides access to all of Outlook's advanced capabilities
ReplyDeletefacebook.com Any hot take that News Corp, Yahoo or Daily Mail in Australia comes up with is going to be trash.
ReplyDeleteI, too, use hotmail.com , and in recent weeks, I've been inundated with the most bizarre and puzzling spam imaginable.
ReplyDeleteWhat gives with yahoo mail not recognising my email all of a sudden?
ReplyDeletei just go for hotmail sign up thanks for your blog
ReplyDeleteyour blog is too good share about gmail sign up
ReplyDeleteDavid, you can check my blog on gmail sign in as well. Thank you.
DeleteThe color palette you've chosen is visually appealing and harmonizes perfectly with your brand image. hotmail sign up
ReplyDeleteFantastic blog! Your knowledge and writing style are truly commendable. Speaking of email services, have you tried Hotmail? It's a reliable and user-friendly platform. Check it out at hotmail.com and keep up the excellent content!
ReplyDeleteIt is an unquestionable masterpiece of blogging genius! hotmail
ReplyDeleteGet ready to set out on a remarkable journey that will capture your attention, stoke your imagination, and uplift your spirit. hotmail
ReplyDeleteWonderful blog! Your writing style and depth of knowledge are absolutely admirable. outlook
ReplyDeleteWonderful blog! Your writing abilities and level of understanding are excellent. whatsapp web
ReplyDeleteDive into the world of the Chain of Responsibility pattern and witness how it streamlines your application's flow, creating a robust and maintainable codebase. 🔄🔗🚀 gmail
ReplyDeleteWonderful blog! Your writing abilities and level of understanding are excellent. In relation to email services, You may want to try Gmail. It is a dependable platform with simple navigation. Look it up at gmail
ReplyDeleteYour blog is a never-ending inspiration. I'm appreciative of how it has improved my life.
ReplyDeletehotmail
It's impressive how much study you done for this topic.
ReplyDeleteamazing action
chase offers
I am enjoying free version of Hotmail want to tell you all read the blog of Hotmail and enjoy free Hotmail version.
ReplyDeleteBedava okey oynamak için doğru yer olan klasik okey oyna ile klasik okey, renkli okey, esli okey oynamak çok pratik ve hızlı. Sen de gel ve okey oynamanın tadını çıkart.
ReplyDeleteKoz maça ve ihaleli batak gibi bir çok batak oyunu türünü barındıran canlı batak salonlarımızda hem online batak oyna hem de sohbet et.
Güzel görünümü ve sorunsuz işleyişi ile tavla salonlarımızda klasik tavla oyna tavla kurallarına bağlı kalınarak açtığımız tavla oyunumuza sen de davetlisin.
Your blog is a heavenly observatory of knowledge. Your posts are like looking at the stars through the telescope of your words, revealing constellations of insight. Truly an astronomical experience!
ReplyDeletehotmail
Visited your blog and your recent post is a real intellectual treat. It's like a well-baked idea, rich in flavor and depth. Ready for more servings on your blog menu!
ReplyDeleteDropped on your blog and found your post to be a real intellectual concert. It's like attending a symphony of ideas. Can't wait for more great performances on your blog!
ReplyDeletehotmail login
facebook login says it is moving beyond 2D screens toward “immersive experiences like augmented and virtual reality to help build the next evolution in social technology.”
ReplyDeleteYour post was like a breath of fresh air! Looking forward to uncovering more refreshing perspectives and innovative ideas on whatsapp web.
ReplyDeleteI found many interesting things from this site. It helps me in many ways. Thanks for posting this again. hotmail login
ReplyDeleteThere is something special about this blog. The writings just flow, they are not stiff, and there is always a topic that makes me curious. Even though there is no specific theme, I still feel like everything is connected in a unique way. 99math login
ReplyDeleteI appreciate the post! hotmail sign up continues to offer a solid, no-nonsense email experience. It’s great for users who want a simple, secure platform without being overwhelmed by extra features that aren’t always necessary.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletehey slownikowo
ReplyDeleteis a great way to build a strong linguistic foundation, whether for personal or professional growth.