Refactoring With 'Chain of Responsibility'
I've been thinking a lot lately about all of the complex business logic we have to handle at the company I work at. Sadly, we do the best we can in OO terms, much of the logic is dispersed and replicated across different applications.
Recently one of our older purchasing applications need some changes to the logic of starting an order in our system. At the same time, another developer was preparing requirements for allowing a 3rd party system to start an order over the same application. What was two page long method with logic for starting an order, now would have to bear the requirements of two different systems, our own and a 3rd party system.
While working with the monster of logic in this single method, I was thinking how horrible it's going to be to bolt in the 3rd party requirements. The developer working on the 3rd party requirements would have to probably: modify the current method, or choose the favorite practice of others-- cut'n'paste into a new method to handle 3rd party stuff. Both are ugly and don't help maintainability at all.
My solution was to refactor the method to use the chain of responsibility pattern. Basically, the request to start an order is passed through a series of rules until we can start an order. For our internal start order logic, a start order context is created and passed through steps A, B, C, D. For the 3rd party system, the start order context would be passed through E, B, C, F, D. Notice B, C, and D are reused and can be maintained in one spot for both flows.
You may be thinking that those shared steps could have just been represented as methods and coordinated by two different parent methods (one for internal logic, and one for 3rd party). This may work in many cases, but in our case, a rule may fire off a view to the user (such as selecting a ship to address for an order). When a rule would fire a user to a view, that rule would return a flag signaling the chain coordinator that a rule finished the processing for that request. Example, selecting a ship to would then fire the user back into the chain of rules from the start until the whole chain would finish and kick them out of the flow (being able to actually add line items).
It was a simple refactoring. I didn't attack it with hopes of creating a workflow system or anything, just a few simple classes and a couple interfaces. If the logic ever changes or needs to be modified, we can just add another rule to the chain.
Recently one of our older purchasing applications need some changes to the logic of starting an order in our system. At the same time, another developer was preparing requirements for allowing a 3rd party system to start an order over the same application. What was two page long method with logic for starting an order, now would have to bear the requirements of two different systems, our own and a 3rd party system.
While working with the monster of logic in this single method, I was thinking how horrible it's going to be to bolt in the 3rd party requirements. The developer working on the 3rd party requirements would have to probably: modify the current method, or choose the favorite practice of others-- cut'n'paste into a new method to handle 3rd party stuff. Both are ugly and don't help maintainability at all.
My solution was to refactor the method to use the chain of responsibility pattern. Basically, the request to start an order is passed through a series of rules until we can start an order. For our internal start order logic, a start order context is created and passed through steps A, B, C, D. For the 3rd party system, the start order context would be passed through E, B, C, F, D. Notice B, C, and D are reused and can be maintained in one spot for both flows.
You may be thinking that those shared steps could have just been represented as methods and coordinated by two different parent methods (one for internal logic, and one for 3rd party). This may work in many cases, but in our case, a rule may fire off a view to the user (such as selecting a ship to address for an order). When a rule would fire a user to a view, that rule would return a flag signaling the chain coordinator that a rule finished the processing for that request. Example, selecting a ship to would then fire the user back into the chain of rules from the start until the whole chain would finish and kick them out of the flow (being able to actually add line items).
It was a simple refactoring. I didn't attack it with hopes of creating a workflow system or anything, just a few simple classes and a couple interfaces. If the logic ever changes or needs to be modified, we can just add another rule to the chain.

1 Comments:
情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,情趣,情趣,情趣,情趣,A片,視訊聊天室,聊天室,視訊,視訊聊天室,080苗栗人聊天室,上班族聊天室,成人聊天室,中部人聊天室,一夜情聊天室,情色聊天室,視訊交友網a片,a片
免費A片,AV女優,美女視訊,情色交友,免費AV,色情網站,辣妹視訊,美女交友,色情影片,成人影片,成人網站,A片,H漫,18成人,成人圖片,成人漫畫,情色網,日本A片,免費A片下載,性愛
A片,色情,成人,做愛,情色文學,A片下載,色情遊戲,色情影片,色情聊天室,情色電影,免費視訊,免費視訊聊天,免費視訊聊天室,一葉情貼圖片區,情色,情色視訊,免費成人影片,視訊交友,視訊聊天,視訊聊天室,言情小說,愛情小說,AIO,AV片,A漫,avdvd,聊天室,自拍,情色論壇,視訊美女,AV成人網,色情A片,SEX,成人論壇
情趣用品,A片,免費A片,AV女優,美女視訊,情色交友,色情網站,免費AV,辣妹視訊,美女交友,色情影片,成人網站,H漫,18成人,成人圖片,成人漫畫,成人影片,情色網
情趣用品,A片,免費A片,日本A片,A片下載,線上A片,成人電影,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,微風成人區,成人文章,成人影城,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,臺灣情色網,色情,情色電影,色情遊戲,嘟嘟情人色網,麗的色遊戲,情色論壇,色情網站,一葉情貼圖片區,做愛,性愛,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,美女交友,做愛影片
av,情趣用品,a片,成人電影,微風成人,嘟嘟成人網,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,愛情公寓,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,aio,av女優,AV,免費A片,日本a片,美女視訊,辣妹視訊,聊天室,美女交友,成人光碟
情趣用品.A片,情色,情色貼圖,色情聊天室,情色視訊,情色文學,色情小說,情色小說,色情,寄情築園小遊戲,情色電影,色情遊戲,色情網站,聊天室,ut聊天室,豆豆聊天室,美女視訊,辣妹視訊,視訊聊天室,視訊交友網,免費視訊聊天,免費A片,日本a片,a片下載,線上a片,av女優,av,成人電影,成人,成人貼圖,成人交友,成人圖片,18成人,成人小說,成人圖片區,成人文章,成人影城,成人網站,自拍,尋夢園聊天室
By
sexy, at 7:24 AM
Post a Comment
<< Home