Java???????????
???????????? ???????[ 2017/4/1 11:05:21 ] ??????????????????? Java ?????
???????????Bridge Pattern??????????????????????????????????????????仯????????????????????????????????????????????????????????????????仯?????????????????????????????????????????????
????????????????
????????????е??????
???????????????????????????BMT??BenZ??Land Rover?????????????????????????????????????????????????????????????????????3*3=9????
????1????ü?з???????????
??????????????????????????????????????????????????N??????????????M??仯???????MNMN???????????????????????????????
???????????????Transmission???????????????????嵵??AMT?????????????3????BMWAMT??BenZAMT??LandRoverAMT??
????2??????????
????????????????
???????????????????????????????????M*N??????????????????????仯??????????????????????
????????????????????嵵?????????????AMT????
????????????????????
????1??????
???????????£?
????package com.jasongj.brand;import com.jasongj.transmission.Transmission;public abstract class AbstractCar {
????protected Transmission gear;
????public abstract void run();
????public void setTransmission(Transmission gear) { this.gear = gear;
????}
????}
????2?????????BMW???
???????????£?
????package com.jasongj.brand;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class BMWCar extends AbstractCar{
????private static final Logger LOG = LoggerFactory.getLogger(BMWCar.class);
????@Override
????public void run() {
????gear.gear();
????LOG.info("BMW is running");
????};
????}
????3??BenZCar
???????????£?
????package com.jasongj.brand;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class BenZCar extends AbstractCar{
????private static final Logger LOG = LoggerFactory.getLogger(BenZCar.class);
????@Override
????public void run() {
????gear.gear();
????LOG.info("BenZCar is running");
????};
????}
????4??LandRoverCar
???????????£?
????package com.jasongj.brand;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class LandRoverCar extends AbstractCar{
????private static final Logger LOG = LoggerFactory.getLogger(LandRoverCar.class);
????@Override
????public void run() {
????gear.gear();
????LOG.info("LandRoverCar is running");
????};
????}
????5???????????
???????????£?
????package com.jasongj.transmission;public abstract class Transmission{
????public abstract void gear();
????}
????6???????
???????????£?
????package com.jasongj.transmission;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Manual extends Transmission {
????private static final Logger LOG = LoggerFactory.getLogger(Manual.class); @Override
????public void gear() {
????LOG.info("Manual transmission");
????}
????}
????7???????
???????????£?
????package com.jasongj.transmission;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Auto extends Transmission {
????private static final Logger LOG = LoggerFactory.getLogger(Auto.class); @Override
????public void gear() {
????LOG.info("Auto transmission");
????}
????}
????8?????????????????????????????????????????????????????????????
???????????£?
????package com.jasongj.client;import com.jasongj.brand.AbstractCar;import com.jasongj.brand.BMWCar;import com.jasongj.brand.BenZCar;import com.jasongj.transmission.Auto;import com.jasongj.transmission.Manual;import com.jasongj.transmission.Transmission;public class BridgeClient {
????public static void main(String[] args) {
????Transmission auto = new Auto();
????AbstractCar bmw = new BMWCar();
????bmw.setTransmission(auto);
????bmw.run();
????Transmission manual = new Manual();
????AbstractCar benz = new BenZCar();
????benz.setTransmission(manual);
????benz.run();
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11