Remove the Middleman object

Source: Internet
Author: User

concept: the "removal of the Middleman object" in this article refers to the removal of classes in the middle that do not play any other role, allowing the related two classes to interact directly.

text: Sometimes in our code there will be some "ghost class", design mode master Fowler called them "middleman" class, "Middleman" class in addition to invoke other objects do not do anything, so the "middleman" class does not exist, we can remove them from the code, This allows the two classes of the interaction to be directly correlated.

As the following code shows, theConsumer class wants to get accountdataprovider data, but the middle is involved in a Accountmanager class that doesn't have any role to associate, so we should remove it.

usingLosTechies.DaysOfRefactoring.PullUpField.After;namespacelostechies.daysofrefactoring.samplecode.removemiddleman.before{Public classConsumer{ PublicAccountmanagerAccountmanager {Get;Set; } PublicConsumer (AccountmanagerAccountmanager) {Accountmanager = Accountmanager; }Public voidGet (intID) {Account account = Accountmanager.getaccount (ID); }    }Public classAccountmanager{ PublicAccountdataproviderDataprovider {Get;Set; } PublicAccountmanager (AccountdataproviderDataprovider) {dataprovider = Dataprovider; } PublicAccount Getaccount (intID) {returnDataprovider.getaccount (ID); }    }Public classAccountdataprovider{ PublicAccount Getaccount (intID) {//Get Account}    }}

The refactored code is shown below,  and
usingLosTechies.DaysOfRefactoring.PullUpField.After;namespacelostechies.daysofrefactoring.samplecode.removemiddleman.after{Public classConsumer{ PublicAccountdataproviderAccountdataprovider {Get;Set; } PublicConsumer (AccountdataproviderDataprovider) {accountdataprovider = Dataprovider; }Public voidGet (intID) {Account account = Accountdataprovider.getaccount (ID); }    }Public classAccountdataprovider{ PublicAccount Getaccount (intID) {//Get Account}    }}

Summary: "Removal of man-in-the-middle objects" can be useful in many cases, especially in code that misuse design patterns, where adapter patterns and proxy patterns in design patterns are associated with each other in the middle class, which is reasonable because the intermediate classes do a lot of things, and the intermediate classes that have no effect should be removed.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Remove the Middleman object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.