Package Mediator. MyTest; // rent an abstract class lodger {int maxmoney; int minmoney; String name; public abstract void rented (); public abstract void statisticsHouse ();}
Package Mediator. MyTest; // The landlord abstract class landlord {int money; String name; public abstract void rent (); public abstract void statisticsPerson ();}
Package Mediator. myTest; class landlordperson extends landlord {Mediator mt; public landlordperson (String name, int money, Mediator mt) {this. mt = mt; this. name = name; this. money = money; mt. collectinLandlord (this) ;}@ Overridepublic void rent () {System. out. println ("the landlord needs the rental fund" + money);} @ Overridepublic void statisticsPerson () {mt. statisticsPerson (this );}}
Package Mediator. myTest; class lodgerperson extends lodger {Mediator mt; public lodgerperson (String name, int minmoney, int maxmoney, Mediator mt) {this. mt = mt; this. name = name; this. maxmoney = maxmoney; this. minmoney = minmoney; mt. collectinLodger (this) ;}@ Overridepublic void rented () {System. out. println ("the rent required by the tenant is" + minmoney + "Yuan to" + maxmoney + "Left and Right");} @ Overridepublic void statisticsHouse () {mt. statisticsHouse (this );}}
Intermediary Abstraction
Package Mediator. myTest; abstract class Mediator {public Mediator (String name) {System. out. println ("intermediary company:" + name);} // collect tenant public abstract void collectinLodger (lodger ld); // collect the landlord public abstract void collectinLandlord (landlord ll ); // count the public void statisticsHouse (lodger ld) of the House that meets the requirements of the specified tenant; // count the public abstract void statisticsPerson (landlord ll) of the tenant that meets the requirements of the specified landlord );}
Package mediator. mytest; import Java. util. arraylist; Class mediatora extends mediator {arraylist <lodger> lodgers = new arraylist <lodger> (); arraylist <landlord> landlords = new arraylist <landlord> (); Public mediatora () {super ("mediatora") ;}@ overridepublic void collectinlandlord (Landlord LL) {landlords. add (LL) ;}@ overridepublic void collectinlodger (lodger LD) {lodgers. add (LD) ;}@ overridepublic void statisticshouse (lodger LD) {system. out. print ("Meet lease" + LD. name + "required houses:"); For (Landlord landlord: landlords) {If (landlord. money <= lD. maxmoney & landlord. money> = lD. minmoney) {system. out. print (landlord. name + ":" + landlord. money + ";") ;}}@ overridepublic void statisticsperson (Landlord LL) {system. out. print ("Meet the landlord" + ll. name + "required tenants:"); For (lodger: lodgers) {If (LL. money <= lodger. maxmoney & ll. money> = lodger. minmoney) {system. out. print (LL. name + ":" + ll. money + ";");}}}}
Test method:
Package Mediator. myTest; import java. util. arrayList; public class MediatorTest {/*** (Mediator) java Implementation of the intermediary mode ** introduction of the intermediary to the house to rent out of the house by the landlord ** @ param args */public static void main (String [] args) {MediatorA ma = new MediatorA (); landlordperson lp1 = new landlordperson ("Landlord 1", 200, ma); landlordperson lp2 = new landlordperson ("Landlord 2", 400, ma); landlordperson lp3 = new landlordperson ("Landlord 3", 800, ma); lodgerperson ld1 = new lodgerperson ("Tenant 1", 100,300, ma ); lodgerperson ld2 = new lodgerperson ("tenant 2", 0,100, ma); lodgerperson ld3 = new lodgerperson ("Tenant 3", 400,800, ma ); lodgerperson ld4 = new lodgerperson ("Tenant 4", ma); ld1.statisticsHouse (); ld3.statisticsHouse (); System. out. println (); lp1.statisticsPerson ();}}