Multi-channel distribution

Source: Internet
Author: User

What is multi-channel distribution:

Java only supports single-path distribution, that is, if the operation to be executed contains more than one unknown object of type,

The dynamic binding mechanism can only process one type in the process,

If you need to process two types, you can use multiple channels for distribution,

To use two-way distribution, you must have two methods:

The first method call determines the first unknown type,

The second method call determines the second unknown type


Interface-based implementation


Item

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:52:42 */public interface Item {ResultCode compete(Item item);ResultCode eval(AA aa);ResultCode eval(BB bb);ResultCode eval(CC cc);}

AA

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:51:57 */public class AA implements Item {@Overridepublic ResultCode compete(Item item) {return item.eval(this);}@Overridepublic ResultCode eval(AA aa) {System.out.print("AA vs AA :");return ResultCode.EQ;}@Overridepublic ResultCode eval(BB bb) {System.out.print("AA vs BB :");return ResultCode.GT;}@Overridepublic ResultCode eval(CC cc) {System.out.print("AA vs CC :");return ResultCode.LT;}}

Bb

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:52:09 */public class BB implements Item {@Overridepublic ResultCode compete(Item item) {return item.eval(this);}@Overridepublic ResultCode eval(AA aa) {System.out.print("BB vs AA :");return ResultCode.LT;}@Overridepublic ResultCode eval(BB bb) {System.out.print("BB vs BB :");return ResultCode.EQ;}@Overridepublic ResultCode eval(CC cc) {System.out.print("BB vs CC :");return ResultCode.LT;}}

CC

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:52:28 */public class CC implements Item {@Overridepublic ResultCode compete(Item item) {return item.eval(this);}@Overridepublic ResultCode eval(AA aa) {System.out.print("CC vs AA :");return ResultCode.GT;}@Overridepublic ResultCode eval(BB bb) {System.out.print("CC vs BB :");return ResultCode.GT;}@Overridepublic ResultCode eval(CC cc) {System.out.print("CC vs CC :");return ResultCode.EQ;}}

Resultcode

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:51:19 */public enum ResultCode {GT,EQ,LT}

Multidisptest

package com.demo.multiple;/** * @author wobendiankun *2014-10-29 下午09:50:22 */public class MultiDispTest {/** * @param args */public static void main(String[] args) {Item item1=new BB();Item item2=new AA();ResultCode result=item1.compete(item2);System.out.println("\t"+result);}}

Running result:

AA vs BB :GT

When item1.compete (item2); is executed, it is determined that the first object Item1 is BB,

In compete of the BB class, item. eval (this) is executed again to determine that the type of the second object item is aa,

The method of AA is found by using the overload method:

@Overridepublic ResultCode eval(BB bb) {System.out.print("AA vs BB :");return ResultCode.GT;}



Implementation Based on enumeration:




Item2

package com.demo.multiple.enums;import com.demo.multiple.Item;import com.demo.multiple.ResultCode;import com.demo.multiple.enums.ItemComparator.ItemCode;/** * @author wobendiankun *2014-10-29 下午09:53:15 * @param <T> */public interface Item2<T extends Item2<T>> {ResultCode compete(T code);}

Itemcomparator

package com.demo.multiple.enums;import com.demo.multiple.Item;import com.demo.multiple.ResultCode;import static com.demo.multiple.ResultCode.*;/** * @author wobendiankun *2014-10-29 下午09:53:26 */public class ItemComparator {enum ItemCode implements Item2<ItemCode>{AA(EQ,GT,LT),BB(LT,EQ,LT),CC(GT,GT,EQ);private ResultCode item1;private ResultCode item2;private ResultCode item3;private ItemCode(ResultCode item1, ResultCode item2, ResultCode item3) {this.item1 = item1;this.item2 = item2;this.item3 = item3;}/* (non-Javadoc) * @see com.demo.multiple.enums.Item2#compete(com.demo.multiple.Item) */@Overridepublic ResultCode compete(ItemCode code) {switch (code) {case AA:return item1;case BB:return item2;case CC:return item3;}return null;}}public static void main(String[] args) {Item2 item1=ItemCode.AA;Item2 item2=ItemCode.CC;System.out.println("AA vs CC :"+item1.compete(item2));}}

Running result:

AA vs CC :LT

A new record is created when the instance is created, and when the compete () method is executed:

@Overridepublic ResultCode compete(ItemCode code) {switch (code) {case AA:return item1;case BB:return item2;case CC:return item3;}return null;}

Query the content of a record:

Field AA ---> Item1

Field BB --> item2

Field CC --> item3

Instance initialization, and corresponding records are created

AA (EQ, GT, LT), BB (LT, EQ, LT), CC (GT, GT, eq)



Multi-channel distribution

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.