Java Design Patterns Tutorial

Lessons

Introduction to Design Patterns

In this lesson, you will get introduced to Design Patterns. You will learn what Design Patterns are, why they should be used in our code and how to select and use one. Finally, the categorization of the existing patterns is described.

Adapter Design Pattern

Via a real life example, you will learn how and when the Adapter pattern should be used and how to structure your code in order to implement it. You will see how it can lead to elegant solutions to code problems.

Facade Design Pattern

The Facade Pattern makes a complex interface easier to use, using a Facade class. The Facade Pattern provides a unified interface to a set of interface in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Composite Design Pattern

The Composite Pattern allows you to compose objects into a tree structure to represent the part-whole hierarchy which means you can create a tree of objects that is made of different parts, but that can be treated as a whole one big thing. Composite lets clients to treat individual objects and compositions of objects uniformly, that’s the intent of the Composite Pattern.

Bridge Design Pattern

The Bridge Pattern’s intent is to decouple an abstraction from its implementation so that the two can vary independently. It puts the abstraction and implementation into two different class hierarchies so that both can be extend independently.

Singleton Design Pattern

The Singleton pattern is used when there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point or when the sole instance should be extensible by sub-classing, and clients should be able to use an extended instance without modifying their code.

Observer Design Pattern

The Observer Pattern is a kind of behavior pattern which is concerned with the assignment of responsibilities between objects. It should be used when an abstraction has two aspects, one dependent on the other, when a change to one object requires changing others, and you don’t know how many objects need to be changed or when an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don’t want these objects tightly coupled.

Mediator Design Pattern

The Mediator Pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Rather than interacting directly with each other, objects ask the Mediator to interact on their behalf which results in reusability and loose coupling. You will learn how and when the Mediator design pattern should be used and how to structure your code in order to implement it.

Proxy Design Pattern

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. It comes up with many different variations. Some of the important variations are, Remote Proxy, Virtual Proxy, and Protection Proxy. In this lesson, we will know more about these variations and we will implement each of them in Java. But before we do that, let’s get to know more about the Proxy Pattern in general. You will learn how and when the Proxy design pattern should be used and how to structure your code in order to implement it.

Chain of Responsibility Design Pattern

The Chain of Responsibility pattern is a behavior pattern in which a group of objects is chained together in a sequence and a responsibility (a request) is provided in order to be handled by the group. If an object in the group can process the particular request, it does so and returns the corresponding response. Otherwise, it forwards the request to the subsequent object in the group.

Flyweight Design Pattern

The Flyweight Pattern is designed to control object creation where objects in an application have great similarities and are of a similar kind, and provides you with a basic caching mechanism. It allows you to create one object per type (the type here differs by a property of that object), and if you ask for an object with the same property (already created), it will return you the same object instead of creating a new one.

Builder Design Pattern

The intent of the Builder Pattern is to separate the construction of a complex object from its representation, so that the same construction process can create different representations. This type of separation reduces the object size. The design turns out to be more modular with each implementation contained in a different builder object. Adding a new implementation (i.e., adding a new builder) becomes easier.

Factory Method Design Pattern

The Factory Method Pattern gives us a way to encapsulate the instantiations of concrete types. The Factory Method pattern encapsulates the functionality required to select and instantiate an appropriate class, inside a designated method referred to as a factory method. The Factory Method selects an appropriate class from a class hierarchy based on the application context and other influencing factors. It then instantiates the selected class and returns it as an instance of the parent class type.

Abstract Factory Design Pattern

The Abstract Factory (A.K.A. Kit) is a design pattern which provides an interface for creating families of related or dependent objects without specifying their concrete classes. The Abstract Factory pattern takes the concept of the Factory Method Pattern to the next level. An abstract factory is a class that provides an interface to produce a family of objects.

Prototype Design Pattern

The Prototype design pattern is used to specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. The concept is to copy an existing object rather than creating a new instance from scratch, something that may include costly operations. The existing object acts as a prototype and contains the state of the object.

Memento Design Pattern

Sometimes it’s necessary to record the internal state of an object. This is required when implementing checkpoints and “undo” mechanisms that let users back out of tentative operations or recover from errors. You must save state information somewhere, so that you can restore objects to their previous conditions. But objects normally encapsulate some or all of their state, making it inaccessible to other objects and impossible to save externally. Exposing this state would violate encapsulation, which can compromise the application’s reliability and extensibility. The Memento pattern can be used to accomplish this without exposing the object’s internal structure.

Template Design Pattern

The Template Design Pattern is a behavior pattern and, as the name suggests, it provides a template or a structure of an algorithm which is used by users. A user provides its own implementation without changing the algorithm’s structure. The Template Pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses to redefine certain steps of an algorithm without changing the algorithm’s structure.

State Design Pattern

The State Design Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. The state of an object can be defined as its exact condition at any given point of time, depending on the values of its properties or attributes. The set of methods implemented by a class constitutes the behavior of its instances. Whenever there is a change in the values of its attributes, we say that the state of an object has changed.

Strategy Design Pattern

The Strategy Design Pattern seems to be the simplest of all design patterns, yet it provides great flexibility to your code. This pattern is used almost everywhere, even in conjunction with the other design patterns. The Strategy Design Pattern defines a family of algorithms, encapsulating each one, and making them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

Command Design Pattern

The Command Design Pattern is a behavioral design pattern and helps to decouples the invoker from the receiver of a request. The intent of the Command Design Pattern is to encapsulate a request as an object, thereby letting the developer to parameterize clients with different requests, queue or log requests, and support undoable operations.

Interpreter Design Pattern

The Interpreter Design Pattern is a heavy-duty pattern. It’s all about putting together your own programming language, or handling an existing one, by creating an interpreter for that language. Given a language, we can define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Decorator Design Pattern

The intent of the Decorator Design Pattern is to attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality. The pattern is used to extend the functionality of an object dynamically without having to change the original class source or using inheritance. This is accomplished by creating an object wrapper referred to as a Decorator around the actual object.

Iterator Design Pattern

The intent of the Iterator Design Pattern is to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. he Iterator pattern allows a client object to access the contents of a container in a sequential manner, without having any knowledge about the internal representation of its contents.

Visitor Design Pattern

The Visitor Design Pattern provides you with a way to add new operations on the objects without changing the classes of the elements, especially when the operations change quite often. The intent of the Visitor Design Pattern is to represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末元媚,一起剝皮案震驚了整個濱河市抱完,隨后出現(xiàn)的幾起案子尤溜,更是在濱河造成了極大的恐慌杰妓,老刑警劉巖雾棺,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件念链,死亡現(xiàn)場離奇詭異哑了,居然都是意外死亡搀缠,警方通過查閱死者的電腦和手機硅确,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門目溉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人菱农,你說我怎么就攤上這事缭付。” “怎么了循未?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵陷猫,是天一觀的道長。 經(jīng)常有香客問我,道長绣檬,這世上最難降的妖魔是什么足陨? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮娇未,結(jié)果婚禮上墨缘,老公的妹妹穿的比我還像新娘。我一直安慰自己零抬,他們只是感情好飒房,可當(dāng)我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著媚值,像睡著了一般狠毯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上褥芒,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天嚼松,我揣著相機與錄音,去河邊找鬼锰扶。 笑死献酗,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的坷牛。 我是一名探鬼主播罕偎,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼京闰!你這毒婦竟也來了颜及?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蹂楣,失蹤者是張志新(化名)和其女友劉穎俏站,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體痊土,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡肄扎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了赁酝。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片犯祠。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖酌呆,靈堂內(nèi)的尸體忽然破棺而出衡载,到底是詐尸還是另有隱情,我是刑警寧澤肪笋,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布月劈,位于F島的核電站度迂,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏猜揪。R本人自食惡果不足惜惭墓,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望而姐。 院中可真熱鬧腊凶,春花似錦、人聲如沸拴念。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽政鼠。三九已至风瘦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間公般,已是汗流浹背万搔。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留官帘,地道東北人瞬雹。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像刽虹,于是被迫代替她去往敵國和親酗捌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,554評論 0 23
  • 本文是戰(zhàn)隼老師在喜馬拉雅FM的《戰(zhàn)隼:無需意思志力的習(xí)慣養(yǎng)成法-100天行動》的筆記涌哲。如需要原文胖缤,請收聽節(jié)目。如有...
    西西西西力閱讀 251評論 0 2
  • 來大理已有旬日膛虫,日日見蒼山負雪巍立草姻,心向往之,知有今日之登臨也稍刀。 至蒼山腳下,仰望敞曹,青松被覆账月。以其山之高也,故綠有...
    胡斐閱讀 649評論 3 4
  • 好久沒能聞到樹葉的清香了澳迫,終于局齿,在長沙連下了幾天雨之后,我在回賓館的路上又再次聞到了這種沁人心脾的香氣橄登。這次帶寶寶...
    奔跑的馬齒莧閱讀 170評論 0 0
  • 半年前,新家快裝修好時谣妻,Y先生說等我們?nèi)胱r萄喳,一定要記得邀請他和他女朋友過來暖居,看著春風(fēng)洋溢的他蹋半,我知道來暖居是...
    修心的大叔閱讀 255評論 0 1