中介模式(Mediator) 中介樣本 java實現

來源:互聯網
上載者:User
package Mediator.MyTest;//租房者abstract class lodger {int maxmoney;int minmoney;String name;public abstract void rented();public abstract void statisticsHouse();}

package Mediator.MyTest;//  房東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("房東需要的出租金為"+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("房客需要租金為" + minmoney + "元到" + maxmoney + "左右的");}@Overridepublic void statisticsHouse() {mt.statisticsHouse(this);}}

中介的抽象方法

package Mediator.MyTest;abstract class Mediator{public Mediator(String name){System.out.println("中介公司:"+name);}//收集租房者public abstract void collectinLodger(lodger ld);//收集房東public abstract void collectinLandlord(landlord ll);//統計符合指定租房者要求的房子public abstract void statisticsHouse(lodger ld);//統計符合指定房東要求的房客public abstract void statisticsPerson(landlord ll);}

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("滿足租房者"+ld.name+"要求的房子有:");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("滿足房東"+ll.name+"要求的房客有:");for(lodger  lodger:lodgers){if(ll.money<=lodger.maxmoney&&ll.money>=lodger.minmoney){System.out.print(ll.name+":"+ll.money+";");}}}}

測試方法:

package Mediator.MyTest;import java.util.ArrayList;public class MediatorTest {/** * (Mediator)中介者模式的Java實現 *  * 中介介紹房子給需要租房的人 房東脫中介介紹 *  * @param args */public static void main(String[] args) {MediatorA ma=new MediatorA();landlordperson lp1=new landlordperson("房東1",200,ma);landlordperson lp2=new landlordperson("房東2",400,ma);landlordperson lp3=new landlordperson("房東3",800,ma);lodgerperson ld1=new lodgerperson("房客1",100,300,ma);lodgerperson ld2=new lodgerperson("房客2",0,100,ma);lodgerperson ld3=new lodgerperson("房客3",400,800,ma);lodgerperson ld4=new lodgerperson("房客4",600,10000,ma);ld1.statisticsHouse();ld3.statisticsHouse();System.out.println();lp1.statisticsPerson();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.