• <menu id="gyiem"><menu id="gyiem"></menu></menu>
  • <menu id="gyiem"><code id="gyiem"></code></menu>

    Java設計模式(八) 適配器模式

    原創文章,轉載請務必將下面這段話置于文章開頭處(保留超鏈接)。
    本文轉發自技術世界原文鏈接 http://www.luozeyang.com/design_pattern/adapter/

    適配器模式介紹

    適配器模式定義

    適配器模式(Adapter Pattern),將一個類的接口轉換成客戶希望的另外一個接口。適配器模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作。

    適配器模式類圖

    適配器模式類圖如下
    Adapter pattern class diagram

    適配器模式角色劃分

    • 目標接口,如上圖中的ITarget
    • 具體目標實現,如ConcreteTarget
    • 適配器,Adapter
    • 待適配類,Adaptee

    實例解析

    本文代碼可從作者Github下載

    目標接口

    1
    2
    3
    4
    5
    6
    7
    package com.jasongj.target;

    public interface ITarget {

    void request();

    }

    目標接口實現

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.jasongj.target;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;


    public class ConcreteTarget implements ITarget {

    private static Logger LOG = LoggerFactory.getLogger(ConcreteTarget.class);

    @Override
    public void request() {
    LOG.info("ConcreteTarget.request()");
    }

    }

    待適配類,其接口名為onRequest,而非目標接口request

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    package com.jasongj.adaptee;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import com.jasongj.target.ConcreteTarget;

    public class Adaptee {

    private static Logger LOGGER = LoggerFactory.getLogger(ConcreteTarget.class);

    public void onRequest() {
    LOGGER.info("Adaptee.onRequest()");
    }

    }

    適配器類

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    package com.jasongj.target;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import com.jasongj.adaptee.Adaptee;

    public class Adapter implements ITarget {

    private static Logger LOG = LoggerFactory.getLogger(Adapter.class);

    private Adaptee adaptee = new Adaptee();

    @Override
    public void request() {
    LOG.info("Adapter.request");
    adaptee.onRequest();
    }

    }

    從上面代碼可看出,適配器類實際上是目標接口的類,因為持有待適配類的實例,所以可以在適配器類的目標接口被調用時,調用待適配對象的接口,而客戶端并不需要知道二者接口的不同。通過這種方式,客戶端可以使用統一的接口使用不同接口的類。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    package com.jasongj.client;

    import com.jasongj.target.Adapter;
    import com.jasongj.target.ConcreteTarget;
    import com.jasongj.target.ITarget;

    public class AdapterClient {

    public static void main(String[] args) {
    ITarget adapter = new Adapter();
    adapter.request();

    ITarget target = new ConcreteTarget();
    target.request();
    }

    }

    適配器模式適用場景

    • 調用雙方接口不一致且都不容易修改時,可以使用適配器模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作
    • 多個組件功能類似,但接口不統一且可能會經常切換時,可使用適配器模式,使得客戶端可以以統一的接口使用它們

    適配器模式優缺點

    適配器模式優點

    • 客戶端可以以統一的方式使用ConcreteTarget和Adaptee
    • 適配器負責適配過程,而不需要修改待適配類,其它直接依賴于待適配類的調用方不受適配過程的影響
    • 可以為不同的目標接口實現不同的適配器,而不需要修改待適配類,符合開放-關閉原則

    適配器模式與OOP原則

    已遵循的原則

    • 依賴倒置原則
    • 迪米特法則
    • 里氏替換原則
    • 接口隔離原則
    • 單一職責原則
    • 開閉原則

    未遵循的原則

    • NA

    Java設計模式系列

    郭俊 Jason wechat
    歡迎關注作者微信公眾號【大數據架構】
    您的贊賞將支持作者繼續原創分享
    速赢彩app www.559748.com | 3846ww.com | www.573995.com | www.3421b.com | www.kelake66.com | 9998jsc.com | xh6.org | www.33598x.com | www.132c73.com | 66665156.com | bb40033.com | biying950vip.com | www.5086i.com | www.00773i.com | www.3332hg.com | 65365.com | 3156www.com | www.66ffk.com | www.72345.com | www.xpj130.com | 00992007.com | 20160913.one | 2004874.com | www.8667i.com | www.55070a.com | www.026918.com | www.792082.com | www.767c.cc | 80878z.com | 7893w32.com | dh08199.com | www.1368.cc | www.597763.com | www.yxinct.com | zhcpdd.com | js26311.com | 66648a.com | www.500yf.com | www.8911333.com | www.003344.cc | www.86611y.com | 3559dd.com | yyh910.com | www.407871.com | www.81233hh.com | www.0077xpj.net | www.1596b.com | 5446y.com | chengbet.cc | c62365.com | 26119b.com | www.csgc8.com | www.183msc.com | www.yfcp066.com | www.155439.com | 55vn77.com | 8037uuu.com | 1775ww.com | 66648a.com | www.815266.com | www.0270e.com | www.115527g.com | www.569393a.com | www.h7788e.com | oo2205.com | 4018.com | drcp222.com | da1369.com | www.310615.com | www.w984.com | www.12455w.com | www.3775t.com | www.26123cc.com | www.996sun.com | www.9679q.com | www.8124v.com | 667766h.com | 39199y.com | h4212.com | 35252q.com | www.063552.com | www.803244.com | www.2875c.com | www.sx139972.com | www.hg77730.com | www.733485.com | www.8850w4.com | www.999530.com | www.c32126.cc | www.6000bjl.com | feicai0457.com | 112x.net | 50067x.com | 22098b.com | 8722nnnn.com | jixiangfang.com | yabobbs.com | 7141pp.com | www.158267.com | www.626990.com | www.hm2777.com | www.61411.cc | www.40484.com | www.hga99900.com | www.818779.com | www.7140588.com | www.83066.com | www.bwinyz36.com | www.55060h.com | www.5099yy.com | www.206037.com | www.8yw.net | www.hg7849.com | www.92220746.com | www.fzf098.com | www.4938o.com | www.hg021.com | www.am88800.com | www.e0686.com | 023139.com | 5446bb.com | 7008037.com | 1077ppp.com | zhcp19.com | 8827vv.com | 11989u.com | 2381i.com | 111122ww.com | www.3888bet.com | www.ao699.com | www.xj9900.com | www.38200s.com | www.25673.com | www.454.net | www.tyc357.com | www.32666t.com | www.55112055.com | www.70022.com | www.84786.com | www.88399b.com | www.66.42.42.157 | www.98698p.com | www.bet91483.com | www.69567t.com | www.97828c.vip | www.6939o.com | www.99788l.com | www.50052p.com | www.202115.com | www.783709.com | www.444739.com | www.063911.com | 8827.cc | 71366s.com | vip66657.com | 00755d.com | 99111kk.com | 3018pp.com | www.blr0088.com | www.hg6642.com | www.330099j.com | www.330007.com | www.088tyc.com | www.xycp98.net | www.73990c.com | www.37377f.com | www.7793n.com | www.930557.com | www.339786.com | 2997772.com | 6699vn77.com | 51133ss.com | feicai0433.com | www.kima88.com | www.5y.cc | www.766577.com | www.4446eee.com | www.692021.com | www.36788p.com | www.709928.com | hrbbdyy.com | 80567s.com | 01234r.com | hwcp33.com | www.7435b.com | www.3170002.com | www.35252m.com | www.460017.com | www.86267z.com | www.891477.com | 25295bb.com | 1389z.com | 53262t.com | 6647a.com | www.v15501.com | www.9404077.com | www.60886a.com | www.49956h.com | www.700626.com | 2383oo.com | 547827.com | 86811l.com | www.76060n.com | www.734555.com | www.377666l.com | www.96386c.com | www.560935.com | 3936e.com | 78808a.com | 92958.com | www.240av.com | www.45598r.com | www.81678p.com | www.506791.com | qq365q.com | 56787ff.com | www.25673f.com | www.140suncity.com | www.570337.com | www.810525.com | 3522ss.cc | bet36550000.com | www.hg0128.com | www.932222.com | www.1434c.com | www.874110.com | 3122bb.com | js84.com | www.35441777.com | www.6386b.com | www.1035i.com | 44990.com | 3656ww.com | www.289599.com | www.5049k.com | www.16878e.com | www.325771.com | 5651g.com | 1288ss.com | www.38238x.com | www.0194009.com | www.210793.com | bet667766.com | 3844q.com | www.tm300.com | www.37377d.com | www.278688.com | 616.net | www.pj2666.com | www.hao500.com | www.8039a.com | 115119.com | 3559uu.com | www.777444r.com | www.45598u.com | www.flff5.com | 1458r.com | 775085.com | www.696568.com | www.0883888.com | www.50732a.com | 3522ll.cc | www.40686f.com | www.33678oo.com | www.772701.com | ppp1915.com | www.77764400.com | www.839797.com | www.cb2388.com | huangma21.com | 3024t.com | www.rycp053.com | www.0014x.com | 23478111.com | www.ven0000.com | www.3066jj.com | www.903117.com | 22222007.com | www.7830d.com | www.2607777.com | www.361706.com | 3614j.com | www.dhycp8811.com | www.ya2019s.com | 55818d.com | 9895t.com | www.c9478.com | www.lqc4.com | 58222zz.com | www.xpj5t5.com | www.99094x.com | 2247j.com | 2247kk.com | www.55526l.com | www.ascp5.com | 454647.com | www.40686c.com | www.s98478.com | xpj893.com | 44bb8331.com | www.pjgw11.com | www.191761.com | y68bb.com | www.751cp5.com | www.ck9191.com | 97340055.com | www.872879.com | www.34972.com | kb080.com | www.pj038888.com | www.9478s.com | 51133ww.com | www.58777b.com | www.c387387.com | 23800.com | www.668567b.com | www.9422b.com | 83377c.com | www.wnsr038.com | www.5856861.com | 3788m.com | www.85770b.com | www.3691p.com | 8901e.com | www.v3305.com | www.954321s.com | 84494499.com | www.16297722.com | www.yi648.com | yun777.com | www.201081.com | www.655870.com | 878365mm.com | www.1851131.com | www.49956i.com | 69990v.com | www.270sun.com | 3242q.com | www.8694v.com | www.50026h.com | 112p.net | www.3239888.com | www.548177.com | 2864d.com | www.47506q.com | 66458v.com | www.zq.hk | www.33997x.com | 119649.com | www.2945d.com | www.202340.com | yin1022.com | www.68993229.com | htbiochem.com | www.776775.com | www.342877.com | 66876q.com | www.87480.com | p838.com | www.23 | 89777m.com | www.bet3650214.com | www.29277s.com | 77606r.com | www.10999s.com | 88553885.com | www.879nn.com | www.914077.com | 9149q.com | www.p63568.com | 3890h.com | www.xxk6386.com | www.csw288.com | www.hgttt.com | www.50024y.com | 7240q.com | www.l32031.com | js26732.com:9885 | www.bet421.com | 55545h.com | zevrez.cn | www.blr6644.com | 2490p.com | www.5504d.com | 500000414.com | www.js7888.co | www.607315.com | 90307y.com | www.58688.cc | kkk8827.com | www.6939w.com | 4152.com | www.js57666.com | 92958b.com | www.4136o.com | tz22555.com | www.444230.com | 524350.com | www.pj5904.com | www.372477.com | www.9777hg.com | www.843633.com | 88535d.com | www.2875s.com | 3122s2.com | www.50074g.com | xpj61788.com | www.76520b.com | 50067i.com | www.0601j.com | 9994cc16.com | www.dayou266.com | 123444.com | www.95222w.com | yhguangxi.vip | www.919699.com | 61322211.com | www.4323p.com | 780.so | www.81233d.com | 8884win.com | www.77802r.com | w35151.com | www.26878l.com | jjjj008.com | www.78wcp.com | www.86611h.com | www.995772.com | www.22832333.com | www.527990.com | www.777444o.com | www.106367.com | www.7276p.com | dz3777.com | www.67799.com | f5429.com | www.83568q.com | 33567w.com | www.88325s.com | 2324fff.com | www.0601j.com | s47479.com | www.99jtb.com | www.7720r.com | www.303482.com | www.3170001.com | 48281111.com | www.9737qq.me | 0698o.com | www.vip7033.com | 2267l.com | www.zhang258.com | www.8882558.com | www.26299e.com | www.hg8728.net | 30019n.com | www.hg99398.com | hggjtg8.com | www.5446v.com | baliren.com | www.fcyl5.com | www.l30226.com | 0245d.com | www.88166x.com | 32212k.com | www.49956w.com | www.9374l.com | www.41518i.com | www.77731v.com | 9737dd.me | www.u8999.com | biying970vip.com | www.196903.com | www.6809pj.com | 7720s.net | www.666825.com | www.glc22.com | www.78949o.com | www.5049l.com | vvv5657.com | www.61233m.com | www.vns994.cc | 9121903.com | www.77775309.com | man966.com | www.900122.com | www.1596f.com | 1654j.com | www.4331vip.com | 1168b.com | q759.top | www.pjgw55.com | wxc0126.com | www.638806.com | www.zs5544.com | 01578.com | www.99343.cc | www.vns365x.net | 42352207.com | www.800938.com | www.10899b.com | 1434l.com | www.3775k.com | www.yh77707.com | hg33311.com | www.38775xx.com | y00351.com | 99988807.com | www.http://443598.com/ | zhcp30.com | www.190163.com | www.848777b.com | 7792a.com | www.165090.com | www.55060k.com | uu3189.com | www.51515a.com | www.93955b.com | 2247f.com | 00nn8331.com | www.56011d.com | www.hj5888.com | 15a26.net | www.0601y.com | www.799msc.com | 1592i.com | www.33112g.com | www.6137f.com | msbet888.com | www.791537.com | www.27363k.com | 201877880.com | www.52072i.com | www.69567a.com | www.a1a000.cc | 1389qq.com | www.15b32.net | www.js778911.com | 4239.com | www.452801.com | www.35700a.com | 1594002.com | yin3939.com | www.57578b.com | www.777888yh.com | 1724444.com | www.183065.com | www.ttt2848.com | www.333222d.com | 88850uu.com | www.22yfa.com | www.y948y.com | 9149y.com | df6272.com | www.16878u.com | www.hg6929.com | hg56555.com | 2836513.com | www.5856869.com | www.0241mmm.com | 173203.com | www.192722.com | www.97828u.vip | www.cs11666.com | 4036nn.com | www.083552.com | www.55717s.com | www.bd0088.com | 520159407.com | 074q7.com | www.9646b.com | www.833898.com | 7200126.com | 20188w.com | www.35918y.com | www.55526y.com | 272t.net | bwin2808.com | www.504633.com | www.56011p.com | www.553843.com | 22117v.com | 88807a.com | www.055m.cc | www.bwinyz46.com | www.3459j.com | vip332.net | 7720w.com | www.87668c.com | www.c559vip.com | 121ccc.com | hg3737.mx | www.111306.com | www.588vip.top | www.72msxfpta.com | 52688h.com | nn500p.com | www.168232.com | www.6664588.com | www.998855q.com | www.8181sun.com | 078xl.com | bwinsss.com | www.50052k.com | www.38345v.com | www.www-36525h.com | 3788.red | 83086u.com | www.910282.com | www.0600f.cc | www.33396.com | 1483ll.com | 83138o.com | 54443v.com | www.8816r.com | www.1754m.com | www.9103ss.com | tt56988.com | 98345bb.com | 3522r.vip | www.99jtc.com | www.2208553.com | www.6676.cc | www.zun371.com | jj1915.com | 876878w.com | www.hcw266.com | www.07088.com | www.7415tt.com | www.q13608.com | 44077l.com | nn1915.com | 38365p.com | www.611949.com | www.1432.com | www.b35bb.com | www.jnh885.com | 55984u.com | 38820011.com | yl66yl11.com | 68228e.com | www.fcyl0.com | www.6678699.com | www.8967e.com | www.xb0024.com | 18438n.com | js8068.com | bet3658.com | 65366.com | www.944229.com | www.82894d.com | www.4996km.com | www.518999.com | www.bet33365.net | 2851.com | 2147dh.com | www.8667i.com | www.t98478.com | www.16181l.com | www.88399a.com | www.b35oo.com | www.hg7942.com | www.28758i.com | 2613.com | 7599ff.com | 3559cc.com | 8970399.com | www.295570.com | www.den00.com | www.29496c.com | www.621233.com | www.71071q.com | www.3l55.com | www.66318t.com | www.wn0003.com | 700589.com | 4022v.com | 3392958.com | 1859002.com | 1665gg.com | www.373553.com | www.918356a.com | www.50080j.com | www.68993235.com | www.63606b.com | www.80969c.com | www.44488807.com | www.69111k.com | www.m27229.com | www.56012.com | 4997n.com | 00048.com | y67890.com | 2007893.com | jbb49.cc | 2677jjj.com | f5429.com | zhcp68.com | 7196m.com | www.188732.com | www.566217.com | www.992599.com | www.2188cai.com | www.81520h.com | www.502206.com | www.98xpj98.com | www.u3410.com | www.55070w.com | www.e92776.com | www.006357.com | www.868444.com | www.999866.com | www.rycp13.com | www.hg4010.com | www.77537y.com | www.10899b.com | www.vnsr600.com | 9195js.com | 36736.com | x48p.com | www.pj90666.com | www.js7888.com | 1592l.com | 4633344.com | 1466m.com | www.123456pk.com | www.www-728855.com | www.aicaiaa.com | www.qy324.com | www.hg6906.com | www.pj99997.com | www.hg8988.com | www.hg7979.com | www.9374z.com | www.25673b.com | www.55115454.com | www.29886v.com | www.k55106.com | www.wnsr4439.com | www.345594.com | www.hg8779.com | www.861000.com | www.hj358.com | www.l456x.com | www.p3065.com | www.531789.com | www.8645005.com | www.wns123k.com | www.52062l.com | www.86333f.com | www.702wb.com | www.60108l.com | www.5086f.com | www.969713.com | www.637477.com | www.303861.com | 1407q.com | 1077xx.com | hrbkldt.com | 228888u.com | g40033.com | 33382ss.com | 0080z.com | www.o168a.com | www.yh6679.com | www.3730-251.com | www.hg131d.com | www.laocaipiao.com | www.3983184.com | www.780780g.com | www.535520.com | www.3552a.com | www.753956.com | www.186769.com | 38821100.com | 15a46.net | xpj5009.com | 3522x.vip | 5443aa.com | www.6491b.com | www.16297766.com | www.rb208.com | www.sha0088.com | www.110589.com | www.99638c.com | www.dan03.com | www.351817.com | 1468.com | 8547hh.com | 56560524.com | 28839z.com | 5077777.com | www.4972mm.com | www.001472.com | www.4212t.com | www.vinisi08.com | www.32123t.com | www.701564.com | www.50732v.com | 22117p.com | 8344333.com | 9498.biz | www.2626sun.com | www.k2341.com | www.282654.com | www.82894.com | www.978797.com | qjdc111.com | 86688100.com | 08159c.com | 66577.cc | www.027322.com | www.6776bb.com | www.5859118.com | www.bxcp3.com | 32424c.com | y77304.com | 11005b.com | www.bet33365.net | www.xj666q.com | www.272018.com | www.99830.cc | www.139630.com | LXYL362.com | 838388m.com | www.7720k.com | www.770777.com | www.pj55716.com | www.1035aa.com | www.50026i.com | 55331p.com | 5589h.com | www.7893w62.com | www.9c123.com | www.3404.cc | www.737689.com | 2649k.com | 6487qqq.com | www.7788076.com | www.xpj540.com | www.86339i.com | www.ch8911.com | ys777.co | xpj01000.com | www.pjliaoning.com | www.060sunbet.com | www.6889785.com | www.599494.com | 7720u.com | jbb71.cc | www.js5666.com | www.7225s.com | www.5091f.com | 142788.com | 3522f.cc | www.558809.com | www.4963ww.com | www.68682c.com | www.50080o.com | 287733.com | www.wn2038.com | www.799666u.com | www.7714s.com | www.wcp666888.com | 8722qqqq.com | 19991c.com | www.jsc12315.com | www.3036222.com | www.35918v.com | 77606m.com | www.6666.cm | www.4078z.com | www.757zf.com | 131pp.net | 55545d.com | www.163888ll.com | www.xpj815.com | www.653436.com | xpj764.cn | www.zb623.com | www.255723.com | www.3479e.com | 3788m.com | j3144.com | www.827733.com | www.2418p.com | www.65707e.com | 222.am | www.995f.net | www.u3410.com | www.611669.com | ww080.us | www.365109e.com | www.888ag.net | www.815807.com | 4809y.com | www.hg77848.com | www.86999a.com | www.66ffi.com | 66066o.com | p888123.com | www.m456x.com | www.0014z.com | mf704.com | www.6033i.com | www.55060g.com | www.817336.com | mf708.com | www.h7788e.com | www.86339x.com | www.136031.com | hjcp188.com | www.hg8443.com | www.89894g.com | sxpj62617.com | 7945.com | www.88166d.com | www.923887.com | k7454.com | www.438msc.com | www.zzyl64.com | 2389900.cc | 7141ii.com | www.938u.cc | www.hm2777.com | pjguizhou.com | www.8814e9.com | www.80030066.com | 3991100.com | 2455n.com | www.677256.com | www.755901.com | 4465k.com | www.828798.com | www.84141.com | 5429.com | www.76543o.com | www.6238.am | 8790b.com | 00778p.com | www.4546800.com | cdwb.com | 8030o.com | www.7415e.com | www.393860.com | 2jsiii.com | www.07.bet | www.725898.com | 4288oo.com | www.901690.com | www.c9664.com | v3410.com | www.67894.com | www.88jcw.com | 35ii.vip | www.77938.com | www.1035k.com | 81366r.com | www.4625gg.com | www.84499.cc | 55577381.com | www.118668.com | www.007hy.cc | c89od261.com | www.01411.cc | www.973320.com | 36405511.com | www.caiba200.com | www.594611.com | 998w.cc | www.ggr.99233r.com | www.401270.com | jindaye88.com | www.4996bj.com | bet365ok03.com | 44462949.com | www.hg0298.com | 2146f.com | www.58777x.com | www.22303.cc | 444000dd.com | www.2302321.com | www.566668.com | feicai0571.com | www.2000e.cc | 35252w.com | www.v3303.com | www.77210k.com | 43234323.com | www.hg7743.com | www.652861.com | 951201.com | www.99552bb.com | 01234rr.com | www.32666g.com | www.2373b.com | sss40033.com | www.h36.com | 22556o.com | www.798878.com | www.68568c.com | 61789j.com | www.60468277.com | ra9993.com | www.38200u.com | www.49956h.com | 67888y.cc | www.hg8ee.com | 22113885.com | www.29886q.com | www.sjgc9.com | 6261q.com | www.33678aa.com | 60958888.com | www.pj5664.com | www.677375.com | 3089g.com | www.4331u.com | 5360a.com | www.804171.com | 91019y.com | www.8867p.com | www.c1359.com | 1077kk.com | www.31468.com | cai89.cc | www.hao500.com | z6528.com | www.hg486.com | www.689631.com | 131t.net | www.99677w.com | 40033r.com | www.390292.com | g86811.com | www.tyc788.com | vic040.com | www.330099c.com | www.816830.com | 8742a.com | www.50054g.com | 11cc8332.com | www.e94600.com | 6218d.com | www.86999.com | 3421v.com | www.345345.com | 45637f.com | www.sb5201.com | www.630933.com | www.glc22.com | www.7777hv.com | 500000311.com | www.bet73t.com | pj88dd.com | www.742488.com | 141237.com | www.1851139.com | 1634t.com | www.r3065.com | 00oo8332.com | www.813365.com | 7377sj.com | www.44234.com | 0729b.com | www.8837138.com | df6258.com | www.hg7227.com | www.190881.com | www.7111m.com | www.348499.com | www.h7788e.com | www.506969.com | www.4625t.com | www.684335.com | www.6491v.com | www.719113.com | www.617705.com | www.596233.com | www.hggj.ac |