手把手教你錘面試官 02——設計模式輕鬆答

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文是手把手教你錘面試官系列第二篇文章,該系列主要爲大家分析和講解在面試過程中,遇到面試官經常提出的面試問題如何進行攻防。另外本系列的文章也會提供許多小技巧給大家去間接試探出面試官的技術能力和專業水平,從而達到碾壓面試官的目的。本系列文章只適用於JAVA工程師。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 設計模式,是一個經常面試被問但是實際工作中很少用到的東西,或者你自己用到了你也沒感覺到自己使用到的東西。如果在面試的時候遇到面試官問你關於設計模式的相關問題,一定不要被嚇到。因爲設計模式的相關問題大多數是沒有標準答案,只要能把自己的理解說清楚就行了。根據我的經驗,大多數的面試官對於設計模式的相關問題自己也不太搞的清楚。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 我們讀書的時候都學過,設計模式一共可以分爲23種。如下所示"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"單例(Singleton)模式:某個類只能生成一個實例,該類提供了一個全局訪問點供外部獲取該實例,其拓展是有限多例模式。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"原型(Prototype)模式:將一個對象作爲原型,通過對其進行復制而克隆出多個和原型類似的新實例。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"工廠方法(Factory Method)模式:定義一個用於創建產品的接口,由子類決定生產什麼產品。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"抽象工廠(AbstractFactory)模式:提供一個創建產品族的接口,其每個子類可以生產一系列相關的產品。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"建造者(Builder)模式:將一個複雜對象分解成多個相對簡單的部分,然後根據不同需要分別創建它們,最後構建成該複雜對象。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"代理(Proxy)模式:爲某對象提供一種代理以控制對該對象的訪問。即客戶端通過代理間接地訪問該對象,從而限制、增強或修改該對象的一些特性。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"適配器(Adapter)模式:將一個類的接口轉換成客戶希望的另外一個接口,使得原本由於接口不兼容而不能一起工作的那些類能一起工作。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":8,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"橋接(Bridge)模式:將抽象與實現分離,使它們可以獨立變化。它是用組合關係代替繼承關係來實現,從而降低了抽象和實現這兩個可變維度的耦合度。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":9,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"裝飾(Decorator)模式:動態的給對象增加一些職責,即增加其額外的功能。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":10,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"外觀(Facade)模式:爲多個複雜的子系統提供一個一致的接口,使這些子系統更加容易被訪問。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":11,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"享元(Flyweight)模式:運用共享技術來有效地支持大量細粒度對象的複用。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":12,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"組合(Composite)模式:將對象組合成樹狀層次結構,使用戶對單個對象和組合對象具有一致的訪問性。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":13,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"模板方法(TemplateMethod)模式:定義一個操作中的算法骨架,而將算法的一些步驟延遲到子類中,使得子類可以不改變該算法結構的情況下重定義該算法的某些特定步驟。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":14,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"策略(Strategy)模式:定義了一系列算法,並將每個算法封裝起來,使它們可以相互替換,且算法的改變不會影響使用算法的客戶。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":15,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"命令(Command)模式:將一個請求封裝爲一個對象,使發出請求的責任和執行請求的責任分割開。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":16,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"職責鏈(Chain of Responsibility)模式:把請求從鏈中的一個對象傳到下一個對象,直到請求被響應爲止。通過這種方式去除對象之間的耦合。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":17,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"狀態(State)模式:允許一個對象在其內部狀態發生改變時改變其行爲能力。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":18,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"觀察者(Observer)模式:多個對象間存在一對多關係,當一個對象發生改變時,把這種改變通知給其他多個對象,從而影響其他對象的行爲。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":19,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"中介者(Mediator)模式:定義一箇中介對象來簡化原有對象之間的交互關係,降低系統中對象間的耦合度,使原有對象之間不必相互瞭解。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":20,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"迭代器(Iterator)模式:提供一種方法來順序訪問聚合對象中的一系列數據,而不暴露聚合對象的內部表示。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":21,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"訪問者(Visitor)模式:在不改變集合元素的前提下,爲一個集合中的每個元素提供多種訪問方式,即每個元素有多個訪問者對象訪問。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":22,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"備忘錄(Memento)模式:在不破壞封裝性的前提下,獲取並保存一個對象的內部狀態,以便以後恢復它。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":23,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"}],"text":"解釋器(Interpreter)模式:提供如何定義語言的文法,以及對語言句子的解釋方法,即解釋器。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":24,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":24,"align":null,"origin":null},"content":[{"type":"text","text":"上面那部分其實都是廢話。如果你要全部搞懂上面的23種設計模式,並且能夠寫出上面的對應的案例代碼,其實完全沒必要,我也覺得沒必要去記。以下是我個人多年工作經驗對於設計模式的看法,只要熟記我下圖的文字,你也可以假裝成爲設計模式的高手,讓你表面看起來有資深架構師的水平:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/50/50a933b3ecb87dd59ebb139e6d09a4c1.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照上圖我總結出來的話,在目前的市場環境下,可以毫無壓力錘死85%以上考察被面試者設計模式的面試官。同樣留一個小技巧給大家錘一下面試官,一般來說,面試官在最後都會問,你還有什麼問題想問我的?大家可以問面試官一個很簡單的小問題——在設計模式裏面,過濾器和攔截器都是基於責任鏈模式去設計的,我想問一下你對過濾器和攔截器的看法和比較。其實很簡單,這個問題的核心回答就只有一個——"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}}],"text":"攔截器是基於類的反射實現的,過濾器是依賴容器的方法回調實現的"},{"type":"text","text":"。只要沒有回答到這個點子上而去東拉西扯什麼攔截的數據不一樣啊,使用的範圍不一樣啊之類的面試官其專業能力一定是非常值得懷疑的,所在公司的專業水平也非常讓人懷疑,如果想要學習專業技能的剛畢業不久的學生,對於這種公司的選擇一定要謹慎謹慎再謹慎。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏爲什麼要把攔截器和過濾器單獨拎出來呢?因爲spring的核心是啥?AOP和IOC嗎?不!是攔截器。下篇文章我會整理一些簡單的spring面試知識,教大家錘爆問Spring知識點的面試官。盡請期待哦~"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章