• <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.xx3.com | www.8494o.com | www.60886k.com | hg4299.com | 1213ppp.com | www.55676m.com | 97799z.com | www.87680b.com | www.16878g.com | 7192004.com | www.r9478.com | www.235127.com | 38989x.com | www.222999qipai.com | 7893w07.com | www.362858.com | www.05500w.com | 3726n.com | www.1064b.com | 29918uu.com | www.ag6969.com | www.60108d.com | 7681005.com | www.58665t.com | s444000.com | www.vns9968.com | www.3126c.com | 99589ff.net | www.js7000.com | w2649.com | www.94498.com | www.233591.com | www.2233524.com | www.71399z.com | 26668w.com | www.hg241.com | 0289a.com | www.hs066.com | o2490.com | www.82894w.com | www.665881.com | www.422442.com | www.84499z.com | 84490.com | www.270sun.com | dingji0088.com | www.5049u.com | v997a.com | www.blm138.com | www.387708.com | www.wn2048.com | www.77ttz.com | 76886q.com | www.520988.net | 8538u.com | www.653263.com | vip332.com | www.7334h.com | 915902207.com | www.4963zz.com | 0698b.com | www.56655f.com | 48139.com | www.788sb.21.sb | yataibet5.com | www.82409.com | 8030s.com | www.tc8803.com | bfvip88.com | www.tyc723.com | 33313k.com | www.40hga.com | 6002b.com | www.86339q.com | 4060pp.com | www.234.pw | 3788ff.com | www.89777q.com | 32yxw.com | www.89777j.com | 28288qq.com | www.3978m.com | 00ee8331.com | www.26163m.com | xpj888e.com | www.956568.com | www.99999jsc.com | www.370766.com | www.55002055.com | 3258h.com | www.11ledian11.cc | q61788.com | www.bet91488.com | 9679n.com | www.88266t.com | 2455d.com | www.937986.com | www.3459p.com | www.108013.com | www.www727939.com | 889345.com | www.bwin930.co | 5504p.com | www.hg341.com | 9694z.com | www.88yfa.com | www.85770v.com | www.bizhong55.com | www.9699.us | 3379yy.com | www.f12189.com | 7003rr.com | www.32123z.com | www.033033o.com | www.506972.com | www.pj8980.com | 0419n.com | www.4546700.com | 3333338722.com | www.lczg1.com | www.31896000.com | 5168333.cc | www.tj58qp.com | 30007c.com | www.587833.com | www.49899.com | 8084.com | www.86333e.com | 2569003.com | www.055973.com | www.rcw88.vip | 4556r.com | www.0014h.com | www.2078t.com | e1915.com | www.4102l.com | 22296cc.com | www.040431.com | www.4694v.com | 99589dd.net | www.962505.com | www.764609.com | 4136f.com | www.yi654.com | www.82508b.com | wn99gg.com | www.50054k.com | www.sky2888.com | 22pp145.com | www.36589999.com | www.2223xj.com | 7508w.com | www.zc35.com | www.854569.com | h01234.com | www.909888.com | www.mark428.com | 88993w.com | www.915231.com | www.938968.com | vipvip2222.com | www.77801u.com | www.k1432.com | dc1108.com | le888r.com | www.47707.co | www.y22365.com | 3957z.com | www.8499n.com | www.w2826.com | 36404411.com | www.191761.com | www.xj6004.com | www.3435999.com | 892979.com | www.07163r.com | www.555030a.com | 66681f.com | www.265990.com | www.627072.com | www.010xpj.com | 1775ww.com | www.qilc9.com | www.998855f.com | 3078u.com | cn.man853.com | www.615006.com | www.xpj1535.com | 8988s.com | 55ww8331.com | www.25688l.com | www.suncity9.com | 11vn77.com | 9737.me | www.1368g.cc | www.9737yy.me | www.4759rr.com | 22274066.com | www.218195.com | www.130a56.com | www.mapai02.com | 3467r.com | www.fh2801.com | www.50051t.com | www.0967111.com | bwin2808.com | www.364343.com | www.222999qipai.com | www.vip36059.com | 0080x.com | 66671b.com | www.hf6660.com | www.1851137.com | www.9068xx.com | dd01234.com | 272ff.net | www.565hc.com | www.47506j.com | www.25288r.com | 08159s.com | 6137g.net | www.hr9888.com | www.62979e.com | www.334msc.com | vvv8827.com | 6261ww.com | man152.com | www.2934o.com | www.b32031.com | www.hg7853.com | 3544l.com | 4018kk.com | 50000021.com | www.929709.com | www.5446h.com | www.3709889.com | www.68666q.com | 3566qq.com | dzhcpw.com | www.587244.com | www.8582ee.com | www.88166f.com | www.11088l.net | 83377d.com | jj444000.com | rf034.com | www.dcw566.com | www.65707g.com | www.591069.com | www.988800.com | www.3459v.com | 4997h.com | bh889933.com | pj12567.com | 2381mmm.com | www.876879.com | www.96386z.com | www.733483.com | www.76773.com | www.28481166.com | www.4759jj.com | 332229.com | so222.cc | 0698o.com | 2934l.com | www.108065.com | www.c1393.com | www.7148.com | www.1115yl.com | www.88166f.com | www.456131.com | www.77537s.com | www.gh0092.com | 28758f.com | 38850011.com | 4789cz.com | 88850dd.com | bet28.com | 8827iii.com | www.178769.com | www.901576.com | www.00840y.com | www.4331l.com | www.5517bbb.com | www.0590777.com | www.8886567.com | www.xh0066.com | www.l30226.com | www.888vip5.com | 2381d.com | 2709a.com | 2078t.com | o67890.com | 9896.com | 99909k.com | 4289e.com | 11bwi.com | 23233y.com | 876878u.com | 983888s.com | www.082985.com | www.477373.com | www.701548.com | www.138136a.com | www.600hc.com | www.bei01.com | www.5095y.com | www.1368x.cc | www.ascp2.com | www.2934x.com | www.58688.cc | www.66clf.com | www.zfcp1.com | www.flff2.com | www.ya515.com | www.1158c.com | www.2632u.com | www.2373w.com | www.gen08.com | www.918681.com | www.979683.com | www.910883.com | www.810852.com | www.567630.com | www.308908.com | www.99677o.com | zzylc3.com | 3245777.com | 59759i.vip | k08199.com | 5002yy.com | 496.vc | pj88ss.com | fc9098.com | 11989.com | www.887.ag | www.7025g.com | www.bc581.com | www.y4042.com | www.8850w8.com | www.6687x.com | www.302551.com | www.55228c.com | www.777hy.cc | www.630933.com | www.068671.com | 497944.com | 22qq8332.com | 4426.com | 0600o.cc | www.xpj6t6.com | www.js12258.com | www.79500i.com | www.0600.cc | www.77803m.com | www.369888aa.com | www.083019.com | 13222u.com | bwinlll.com | 99567b.com | 5144v.com | www.5556358.com | www.kj803.com | www.4996fs.com | www.663189.com | www.6832q.com | www.506791.com | pujingzaixianyule.com | 33p88.cc | zunyi801.com | www.414133.com | www.789270.com | www.00772g.com | www.7969.com | www.985576.com | 3616d.com | l3405.com | 365102f.com | www.vnsr500.com | www.114654.com | www.837929.com | www.2109b.com | www.107501.com | 1596b.com | 50067d.com | www.pjzhejiang.com | www.38789b.com | www.i063801.com | www.08500w.com | www.100202.com | 3421w.com | 9895x.com | www.7811b.com | www.64566c.com | www.50026p.com | www.178259.com | hgw168p.com | 6220i.com | www.seqhi.com | www.k1432.com | www.2350m.com | c.kuktt.cn | 3846ccc.com | www.133494.com | www.c559cc.com | www.amdwc.net | www.393121.com | ca5033.com | 66066.com | www.2289msc.com | www.369158.com | www.197802.com | 12274477.com | www.wnsr878.com | www.88166a.com | www.9478y.com | 83086n.com | 26668o.com | www.tbb003.com | www.188611.com | www.599476.com | hga008.com | www.88517888.cc | www.899741.com | www.22603.cc | zunyi18.com | 6220a.com | www.838080.com | www.90920.com | 2008hh.com | 7042004.com | www.777423.net | www.390737.com | www.84499o.com | 1407.com | www.444540.com | www.15b32.net | g17022.com | 4023o.com | www.o9478.com | www.2934b.com | bwin8y.com | 5855tt.com | www.cn365j.com | www.808482.com | hg999333b.com | www.2090966.com | www.xpj3133.cc | www.183799.com | 7003kk.com | www.amdc0009.com | www.21202c.com | 7733vn77.com | www.76060b.com | www.xingji01.com | 7284444.com | 2776dh.com | www.00618e.com | www.602467.com | 3662j.com | www.9409.com | www.gai50.com | wn99gg.com | www.9949c.com | www.60108b.com | 3616c.com | www.yh1357.com | www.js02347.com | 7874.cc | www.7720k.com | www.42842807.com | 9607w.vip | www.700h.com | www.ya2019b.com | 1173322.com | www.56520x.com | www.rfdc04.com | 201.com | www.xin888111.com | www.29496d.com | 138.am | www.9999304.com | www.9356n.com | 3033s.com | www.3814b.com | www.930155.com | 8449pp.com | www.8313b.com | www.50064e.com | 1624zz.com | www.hg7226.com | nnn5144.com | www.2078a.com | www.3479r.com | 4005.com | www.198930.com | www.77803e.com | hg999333.org | www.pjzxyl.co | 5443w.com | www.vns3938.com | www.545568.com | 2190w.com | www.e94600.com | 32355aa.com | www.423886.com | www.755740.com | 8905a.com | www.97828i.vip | 22883y.com | www.889tyc.com | www.351376.com | 91019g.net | www.88065f.com | xpj8182.cc | www.hg8xx.com | 0434333.com | www.32666b.com | www.981090.com | 3258x.com | www.win1232.com | 496nn.com | www.c5c33.com | x33v.vip | www.pj6767.com | www.707250.com | 37570m.com | www.7714q.com | 13222g.com | www.13k0048.com | 889345.com | www.567878.com | www.768299.com | www.6491f.com | www.063wy.com | www.2945g.com | www.86079.cc | 55797b.com | www.206269.com | 3656vv.com | www.ba509.com | y68357.com | www.bwinyz22.com | 22117m.com | www.c44ii.com | 2127y.com | www.44438.com | 8569811t.com | www.344559.com | www.88266i.com | www.32666m.com | www.375967.com | www.bwin1188.net | www.674770.com | www.133494.com | www.ck1178.com | 953644.com | www.zch2.com | www.8520u.com | www.qml2.com | www.dfs995.com | www.gczj7.com | 6175r.com | www.c9335.com | www.xjs8890.com | www.972876.com | www.0555hg.com | www.810575.com | www.223456k.com | www.558414.com | www.55268ss.com | www.234265.com | www.79890m.com | 667766j.com | www.5006c.com | 3552c.com | www.430578.com | 9679x.com | www.6687r.com | 33112211.com | www.154405.com | 44488d.com | www.ya270.com | www.888vh.com | www.394811.com | www.244749.com | 0088cpw.com | www.hh58.com | qq444000.com | www.1466l.com | 55ff8331.com | www.cb0999.com | www.67797q.com | bet777000.com | www.cheng777.com | 11005c.com | www.785409.com | 26444k.com | www.aa3126.com | www.8344567.com | LXYL365.com | www.00618h.com | m40033.com | www.80767n.com | www.033033o.com | www.372082.com | www.78188d.com | 2455g.com | www.68365d.com | 009900g.com | www.821260.com | www.cjycp55.com | bojue009.com | www.r999999.com | 87965mm.com | www.887771.com | www.5187575.com | 2267z.com | www.yh8899.bet | 31780000.com | www.126730.com | www.a81e.cc | 2685.com | www.7116a.com | www.8814e5.com | 3mgmmmm.com | www.883399h.com | 99111yy.com | www.322424.com | www.138cpp.com | 158808.com | www.88cmc.com | www.bmw1195.com | e14666.com | www.a991qp.com | www.7669b.net | msyz818.com | www.r999991.com | 7792l.com | P35x.com | www.369401.com | 996622rr.com | www.77802v.com | www.06617b.com | 3948722.com | www.330985.com | www.38345s.com | a08199.com | www.401274.com | www.hga55500.com | 110770.com | www.163734.com | www.829797d.com | 2767l.com | www.033253.com | www.7893w35.com | ttt2848.com | 52599j.com | www.621051.com | www.35530.com | 57157f.com | www.770802.com | www.777444k.com | 3700688.com | www.9818d.cc | www.725558.com | 4196u.com | www.547577.com | www.7777yh.com | 4997u.com | 01885b.com | www.99094y.com | www.77755545.com | 30019w.com | www.595hc.com | www.d9008.com | 50099l.com | www.61655e.com | www.29977.com | www.55574.vip | 66300vip41.com | www.qin618.com | www.fh2801.com | 537a.cc | aipin66.me | www.0270n.com | www.77958.com | 36405500.com | www.068671.com | www.9187q.com | www.777444q.com | 30177755.com | www.558610.com | www.115527o.com | www.63877h.com | 84494466.com | www.699925.com | www.77658zz.com | www.6491c.com | 3644o.com | www.691663.com | www.567678.cc | www.xpj9878.com | 402d5.com | www.369477.com | www.0066psb.com | www.98am444.com | 5002nnn.com | 2455w.com | www.77114b.com | www.9788t.com | 54540123.com | dianzi580.com | www.581166.com | www.9187e.com | www.901008.com | sp2088.com | 8827ll.com | www.918375.com | www.32031q.com | www.6663009.com | pc413.cn | 1669u.com | www.930557.com | www.3032kk.com | www.58557q.com | 2127e.com | 22117.com | www.626212.com | www.x63568.com | www.500128.com | qy192.vip | 18438y.com | www.083016.com | www.81233y.com | www.99113x.com | www.hg0051.com | 7605e.com | 2061.com | www.586775.com | www.28891f.com | www.3846l.com | www.728055.com | 7605e.com | 084939.com | www.346377.com | www.77801p.com | www.55060j.com | www.10611a.com | feicai0852.com | 71372225.com | 8722tttt.com | www.652861.com | www.868559.com | www.5099gg.com | www.hg7869.com | 8977.com | ylhg2233.com | 2595g.com | www.66653e.com | www.3668aa.com | www.yh888b.com | www.h948h.com | www.792suncity.com | rr2649.com | 0245w.com | 3049w.com | www.317787.com | www.9478h.com | www.5599nsb.com | www.27363z.com | www.255251.com | 80892tt.com | yh645.cn | 781520.com | 51200xx.com | www.353990.com | www.2632i.com | www.6667758.com | www.9737pp.com | www.82205.com | www.vns9948.com | 2418005.com | hggjtg19.com | bg222333.com | ycl888.com | xpj01988.com | www.631012.com | www.7777y.cc | www.bj699.com | www.8w08.com | www.k32939.com | www.tb970.com | www.88837b.com | www.xpj1111.cc | 56988r.com | 2012vip66.com | 47751.com | hh38648.cc | bet99949.com | 915904605.com | www.338035.com | www.914906.com | www.8039l.com | www.bet73f.com | www.1764w.com | www.781516.com | www.808888q.com | www.74109f.com | www.hg83555.com | www.hg00895.com | www.bh11345.com | 99111.com | 30006c.com | a3672.com | blr01.vip | w21148.com | 3658766.com | 99995309.com | 3559s.com | 5804v.com | vns.com | 61320055.com | 80567s.com | 9889567.com | 38821155.com | 3156sss.com | 22117i.com | tz22777.com | 20559944.com | cr789.com | b762.xyz | 142788.com | 6868mm.cc | 3844ff.com | 11995002.com | 9390009.com" | cc3189.com | 7249o.com | feicai0530.com | 80368jj.com | 22883e.com | 53262o.com | c89fhg3.com | 5008999.com | 32666a.com | 3522j.cc | 7508d.com | 808cp.org | 3844rr.com | 9679w.com | www.6033c.com | www.xw66666.com | www.xhtd1122.com | www.365815d.com | www.440887.com | www.xpj2718.com | www.588112.com | www.45681b.com | www.zzyl68.com | www.hs066.com | www.33678dd.com | www.087k.com | www.81608c.com | www.7239t.com | www.5854k.cc | www.848566.com | www.268177.com | 0080s.com | 771381.com | zb153.com | 67890hhh.com | 7499822.com | hrviip1.com | www.767997.com | www.xj4466.com | www.hg415.com | www.5981p.com | www.0088hgii.com | www.9646v.com | www.3890u.com | www.721560.com | www.66332i.com | aa3336.com | 2240099.com | 45637p.com | 1483ww.com | www.9996ss.com | www.03499.com | www.bfcaip.am | www.758567a.com | www.83033k.com | www.777768a.com | www.032823.com | 80567b.com | 6082.com | 6220y.com | www.hg8662.com | www.55690.com | www.bet63j.com | www.6889791.com | www.8299.cc | www.306061.com | tiexin88.com | 0729i.com | 88770.com | www.7830s.com | www.cr811.com | www.58908b.com | www.50024h.com | www.506729.com | 3327.com | 3242j.com | 023139.com | www.s95568.com | www.00772b.com | www.xy52aa.com | www.847577.com | 51200uu.com | 33kbk.com | 2649pp.com | www.lgf08.com | www.4078k.com | www.hg1177.com | www.599781.com | 6137g.net | 9522gg.com | www.185ks.com | www.508345.com | www.4102m.com | www.hcjt5.com | 68228a.com | uuu4165.com | www.8520t.com | www.hg7739.com | www.86339a.com | www.c9218.com | 8722tttt.com | 9506j.com | www.benz4455s.com | www.cp0028.com | www.390622.com | www.330256.com | 2998r.com | f3144.com | www.873999.com | www.7737ee.com | www.901013.com | 897dd.cc | 3685t.com | www.387411.com | www.40288f.com | www.912483.com | 56987nn.com | 4131.com | www.c6784.com | www.25229.com | www.550437.com | 35222z.com | lewen123.com | www.hnhdkm.com | www.bet73r.com | www.060640.com | 2jsddd.com | www.32126d.net | www.57800y.com | www.077wy.com | 8577k.cc | 37570.com | www.447334.com | www.66136000.com | 32424f.com | 3522q.cc | www.0805u.com | www.792086.com | www.404099.com | 11453322.com | www.hg00188.com | www.377666q.com | www.808910.com | 35590088.com | www.pjdc4488.com | www.sha2233.com | www.963220.com | 5554136.com | www.ra8878.com | www.bwinyz06.com | www.904827.com | feicai0879.com | www.5777933.com | www.4521i.com | www.504833.com | lh66v.com | www.066005.com | www.9989578.com | 2247h.com | 55402hd.com | www.55526f.com | www.061wy.com | 5350.com | www.bet3650614.com | www.60886.com | www.170955.com | 28288y.com | www.hg481.com | www.5091y.com | 1591mmm.com | www.689578.com | www.42070012.com | www.66653v.com | 9649m.com | www.fh990.com | www.904830.com | 062hhh.com | www.333423.net | www.71399d.com | 2566h9.com | www.www-866766.com | www.hbet98.com | 2757m.com | lewen123.com | www.4521d.com | www.75538a.com | 3222c.cc | www.4078t.com | www.371411.com | 192222.com | www.854608.com | www.616383.com | 884167.com | www.55526g.com | www.755901.com | 4488yyy.com | www.fff3997.com | www.794918.com | hjcp3.com | www.50000991.com | www.717671.com | mg887788.com | www.58665c.com | www.398846.com | 3685m.com | www.4996sy.com | www.43131y.com | 50027711.com | www.wns22.me | vs665.com | www.pjbet3388.com | www.8582bb.com | 585886.com | www.320288.com | www.055i.cc | 6704888.com | www.69249.com | www.339752.com | 7989l.com | www.285628.com | zhcp34.com | www.77cmsc.net | www.21202d.com | 54146611.com | www.015.com | www.270648.com | 3121cc.com | www.j98478.com | 1770m.com | www.741199b.com | www.775703.com | 2222k08.com | www.69567.com | h4255.com | www.8884j.com | www.997666.com | 62222k.com | www.9895n.com | dh3868.com | www.xb0024.com | www.820380.com | 518cp99.com | www.hg0335.com |