- Head First Design Patterns A Brain-friendly Guide Book Pdf
- Design Patterns Pdf Download
- Head First Design Patterns Pdf
- Head First Design Patterns Ebook
- Head First Design Patterns Download
Chapter 1. Intro to Design Patterns: Welcome to Design Patterns
Head first design patterns Download head first design patterns or read online books in PDF, EPUB, Tuebl, and Mobi Format. Click Download or Read Online button to get head first design patterns book now. This site is like a library, Use search box in the widget to get ebook that you want. Jan 01, 2004 Head First Design Patterns is a great way to get a good overview (and some meat) about design patterns. This will interest programmers and computer scientists the most, but as you learn near the end of the book, design pattern were first created by an architect!
Someone has already solved your problems. In this chapter, you’ll learn why (and how) you can exploit the wisdom and lessons learned by other developers who’ve been down the same design problem road and survived the trip. Before we’re done, we’ll look at the use and benefits of design patterns, look at some key OO design principles, and walk through an example of how one pattern works. The best way to use patterns is to load your brain with them and then recognize places in your designs and existing applications where you can apply them. Instead of code reuse, with patterns you get experience reuse.
Joe works for a company that makes a highly successful duck pond simulation game, SimUDuck. The game can show a large variety of duck species swimming and making quacking sounds. The initial designers of the system used standard OO techniques and created one Duck superclass from which all other duck types inherit.
In the last year, the company has been under increasing pressure from competitors. After a week long off-site brainstorming session over golf, the company executives think it’s time for a big innovation. They need something really impressive to show at the upcoming shareholders meeting in Maui next week.
The executives decided that flying ducks is just what the simulator needs to blow away the other duck sim competitors. And of course Joe’s manager told them it’ll be no problem for Joe to just whip something up in a week. “After all,” said Joe’s boss, “he’s an OO programmer.. how hard can it be?”
What happened?
Joe failed to notice that not all subclasses of Duck should fly. When Joe added new behavior to the Duck superclass, he was also adding behavior that was not appropriate for some Duck subclasses. He now has flying inanimate objects in the SimUDuck program.
A localized update to the code caused a nonlocal side effect (flying rubber ducks)!
What Joe thought was a great use of inheritance for the purpose of reuse hasn’t turned out so well when it comes to maintenance.
Joe realized that inheritance probably wasn’t the answer, because he just got a memo that says that the executives now want to update the product every six months (in ways they haven’t yet decided on). Joe knows the spec will keep changing and he’ll be forced to look at and possibly override fly() and quack() for every new Duck subclass that’s ever added to the program.. forever.
So, he needs a cleaner way to have only some (but not all) of the duck types fly or quack.
What do YOU think about this design?
We know that not all of the subclasses should have flying or quacking behavior, so inheritance isn’t the right answer. But while having the subclasses implement Flyable and/or Quackable solves part of the problem (no inappropriately flying rubber ducks), it completely destroys code reuse for those behaviors, so it just creates a different maintenance nightmare. And of course there might be more than one kind of flying behavior even among the ducks that do fly..
At this point you might be waiting for a Design Pattern to come riding in on a white horse and save the day. But what fun would that be? No, we’re going to figure out a solution the old-fashioned way--by applying good OO software design principles.
Okay, what’s the one thing you can always count on in software development?
No matter where you work, what you’re building, or what language you are programming in, what’s the one true constant that will be with you always?
(use a mirror to see the answer)
No matter how well you design an application, over time an application must grow and change or it will die.
So we know using inheritance hasn’t worked out very well, since the duck behavior keeps changing across the subclasses, and it’s not appropriate for all subclasses to have those behaviors. The Flyable and Quackable interface sounded promising at first—only ducks that really do fly will be Flyable, etc.—except Java interfaces have no implementation code, so no code reuse. And that means that whenever you need to modify a behavior, you’re forced to track down and change it in all the different subclasses where that behavior is defined, probably introducing new bugs along the way!
Luckily, there’s a design principle for just this situation.
Design Principle
Identify the aspects of your application that vary and separate them from what stays the same.
The first of many design principles. We’ll spend more time on these throughout the book.
Take what varies and “encapsulate” it so it won’t affect the rest of your code.
The result? Fewer unintended consequences from code changes and more flexibility in your systems!
In other words, if you’ve got some aspect of your code that is changing, say with every new requirement, then you know you’ve got a behavior that needs to be pulled out and separated from all the stuff that doesn’t change.
Here’s another way to think about this principle: take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t.
As simple as this concept is, it forms the basis for almost every design pattern. All patterns provide a way to let some part of a system vary independently of all other parts.
Okay, time to pull the duck behavior out of the Duck classes!
Where do we start? As far as we can tell, other than the problems with fly() and quack(), the Duck class is working well and there are no other parts of it that appear to vary or change frequently. So, other than a few slight changes, we’re going to pretty much leave the Duck class alone.
Now, to separate the “parts that change from those that stay the same,” we are going to create two sets of classes (totally apart from Duck), one for fly and one for quack. Each set of classes will hold all the implementations of the respective behavior. For instance, we might have one class that implements quacking, another that implements squeaking, and another that implements silence.
We know that fly() and quack() are the parts of the Duck class that vary across ducks.
To separate these behaviors from the Duck class, we’ll pull both methods out of the Duck class and create a new set of classes to represent each behavior.
So how are we going to design the set of classes that implement the fly and quack behaviors?
We’d like to keep things flexible; after all, it was the inflexibility in the duck behaviors that got us into trouble in the first place. And we know that we want to assign behaviors to the instances of Duck. For example, we might want to instantiate a new MallardDuck instance and initialize it with a specific type of flying behavior. And while we’re there, why not make sure that we can change the behavior of a duck dynamically? In other words, we should include behavior setter methods in the Duck classes so that we can change the MallardDuck’s flying behavior at runtime.
Given these goals, let’s look at our second design principle:
Design Principle
Program to an interface, not an implementation.
From now on, the Duck behaviors will live in a separate class—a class that implements a particular behavior interface.
That way, the Duck classes won’t need to know any of the implementation details for their own behaviors.
We’ll use an interface to represent each behavior—for instance, FlyBehavior and QuackBehavior—and each implementation of a behavior will implement one of those interfaces.
So this time it won’t be the Duck classes that will implement the flying and quacking interfaces. Instead, we’ll make a set of classes whose entire reason for living is to represent a behavior (for example, “squeaking”), and it’s the behavior class, rather than the Duck class, that will implement the behavior interface.
This is in contrast to the way we were doing things before, where a behavior came either from a concrete implementation in the superclass Duck, or by providing a specialized implementation in the subclass itself. In both cases we were relying on an implementation. We were locked into using that specific implementation and there was no room for changing the behavior (other than writing more code).
With our new design, the Duck subclasses will use a behavior represented by an interface (FlyBehavior and QuackBehavior), so that the actual implementation of the behavior (in other words, the specific concrete behavior coded in the class that implements the FlyBehavior or QuackBehavior) won’t be locked into the Duck subclass.
“Program to an interface” really means “Program to a supertype.”
The word interface is overloaded here. There’s the concept of interface, but there’s also the Java construct interface. You can program to an interface, without having to actually use a Java interface. The point is to exploit polymorphism by programming to a supertype so that the actual runtime object isn’t locked into the code. And we could rephrase “program to a supertype” as “the declared type of the variables should be a supertype, usually an abstract class or interface, so that the objects assigned to those variables can be of any concrete implementation of the supertype, which means the class declaring them doesn’t have to know about the actual object types!”
This is probably old news to you, but just to make sure we’re all saying the same thing, here’s a simple example of using a polymorphic type—imagine an abstract class Animal, with two concrete implementations, Dog and Cat.
Programming to an implementation would be:
Note
Declaring the variable “d” as type Dog (a concrete implementation of Animal) forces us to code to a concrete implementation.
Head First Design Patterns A Brain-friendly Guide Book Pdf
But programming to an interface/supertype would be:
Note
We know it’s a Dog, but we can now use the animal reference polymorphically.
Even better, rather than hardcoding the instantiation of the subtype (like new Dog()) into the code, assign the concrete implementation object at runtime:
Note
We don’t know WHAT the actual animal subtype is.. all we care about is that it knows how to respond to makeSound().
Here we have the two interfaces, FlyBehavior and QuackBehavior, along with the corresponding classes that implement each concrete behavior:
Note
With this design, other types of objects can reuse our fly and quack behaviors because these behaviors are no longer hidden away in our Duck classes!
And we can add new behaviors without modifying any of our existing behavior classes or touching any of the Duck classes that use flying behaviors.
So we get the benefit of REUSE without all the baggage that comes along with inheritance.
Answers:
1) Create a FlyRocketPowered class that implements the FlyBehavior interface.
2) One example, a duck call (a device that makes duck sounds).
The key is that a Duck will now delegate its flying and quacking behavior, instead of using quacking and flying methods defined in the Duck class (or subclass).
Here’s how:
- ① First we’ll add two instance variables to the Duck class called flyBehavior and quackBehavior that are declared as the interface type (not a concrete class implementation type). Each duck object will set these variables polymorphically to reference the specific behavior type it would like at runtime (FlyWithWings, Squeak, etc.).We’ll also remove the fly() and quack() methods from the Duck class (and any subclasses) because we’ve moved this behavior out into the FlyBehavior and QuackBehavior classes.We’ll replace fly() and quack() in the Duck class with two similar methods, called performFly() and performQuack(); you’ll see how they work next.
- ② Now we implement performQuack():Pretty simple, huh? To perform the quack, a Duck just allows the object that is referenced by quackBehavior to quack for it.In this part of the code we don’t care what kind of object it is, all we care about is that it knows how to quack()!
- ③ Okay, time to worry about how the flyBehavior and quackBehavior instance variables are set. Let’s take a look at the MallardDuck class:So MallardDuck’s quack is a real live duck quack, not a squeak and not a mute quack. So what happens here? When a MallardDuck is instantiated, its constructor initializes the MallardDuck’s inherited quackBehavior instance variable to a new instance of type Quack (a QuackBehavior concrete implementation class).And the same is true for the duck’s flying behavior—the MallardDuck’s constructor initializes the flyBehavior instance variable with an instance of type FlyWithWings (a FlyBehavior concrete implementation class).
Good catch, that’s exactly what we’re doing.. for now.
Later in the book we’ll have more patterns in our toolbox that can help us fix it.
Still, notice that while we are setting the behaviors to concrete classes (by instantiating a behavior class like Quack or FlyWithWings and assigning it to our behavior reference variable), we could easily change that at runtime.
Design Patterns Pdf Download
So, we still have a lot of flexibility here, but we’re doing a poor job of initializing the instance variables in a flexible way. But think about it: since the quackBehavior instance variable is an interface type, we could (through the magic of polymorphism) dynamically assign a different QuackBehavior implementation class at runtime.
Take a moment and think about how you would implement a duck so that its behavior could change at runtime. (You’ll see the code that does this a few pages from now.)
- ① Type and compile the Duck class below (Duck.java), and the MallardDuck class from two pages back (MallardDuck.java).
- ② Type and compile the FlyBehavior interface (FlyBehavior.java) and the two behavior implementation classes (FlyWithWings.java and FlyNoWay.java).
- ③ Type and compile the QuackBehavior interface (QuackBehavior.java) and the three behavior implementation classes (Quack.java, MuteQuack.java, and Squeak.java).
- ④ Type and compile the test class (MiniDuckSimulator.java).
- ⑤ Run the code!
What a shame to have all this dynamic talent built into our ducks and not be using it! Imagine you want to set the duck’s behavior type through a setter method on the duck subclass, rather than by instantiating it in the duck’s constructor.
- ① Add two new methods to the Duck class:We can call these methods anytime we want to change the behavior of a duck on the fly.
Note
Editor note: gratuitous pun - fix - ② Make a new Duck type (ModelDuck.java).
- ③ Make a new FlyBehavior type (FlyRocketPowered.java).
- ④ Change the test class (MiniDuckSimulator.java), add the ModelDuck, and make the ModelDuck rocket-enabled.
To change a duck’s behavior at runtime, just call the duck’s setter method for that behavior.
Okay, now that we’ve done the deep dive on the duck simulator design, it’s time to come back up for air and take a look at the big picture.
Below is the entire reworked class structure. We have everything you’d expect: ducks extending Duck, fly behaviors implementing FlyBehavior, and quack behaviors implementing QuackBehavior.
Notice also that we’ve started to describe things a little differently. Instead of thinking of the duck behaviors as a set of behaviors, we’ll start thinking of them as a family of algorithms. Think about it: in the SimUDuck design, the algorithms represent things a duck would do (different ways of quacking or flying), but we could just as easily use the same techniques for a set of classes that implement the ways to compute state sales tax by different states.
Pay careful attention to the relationships between the classes. In fact, grab your pen and write the appropriate relationship (IS-A, HAS-A, and IMPLEMENTS) on each arrow in the class diagram.
The HAS-A relationship is an interesting one: each duck has a FlyBehavior and a QuackBehavior to which it delegates flying and quacking.
When you put two classes together like this you’re using composition. Instead of inheriting their behavior, the ducks get their behavior by being composed with the right behavior object.
This is an important technique; in fact, we’ve been using our third design principle:
Design Principle
Favor composition over inheritance.
Head First Design Patterns Pdf
As you’ve seen, creating systems using composition gives you a lot more flexibility. Not only does it let you encapsulate a family of algorithms into their own set of classes, but it also lets you change behavior at runtime as long as the object you’re composing with implements the correct behavior interface.
Composition is used in many design patterns and you’ll see a lot more about its advantages and disadvantages throughout the book.
Brain Power
A duck call is a device that hunters use to mimic the calls (quacks) of ducks. How would you implement your own duck call that does not inherit from the Duck class?
What’s the difference between these two orders? Not a thing! They’re both the same order, except Alice is using twice the number of words and trying the patience of a grumpy short-order cook.
What’s Flo got that Alice doesn’t? A shared vocabulary with the short-order cook. Not only does that make it easier to communicate with the cook, but it gives the cook less to remember because he’s got all the diner patterns in his head.
Design Patterns give you a shared vocabulary with other developers. Once you’ve got the vocabulary you can more easily communicate with other developers and inspire those who don’t know patterns to start learning them. It also elevates your thinking about architectures by letting you think at thepatternlevel, not the nitty-gritty object level.
Brain Power
Can you think of other shared vocabularies that are used beyond OO design and diner talk? (Hint: how about auto mechanics, carpenters, gourmet chefs, air traffic control.) What qualities are communicated along with the lingo?
Can you think of aspects of OO design that get communicated along with pattern names? What qualities get communicated along with the name “Strategy Pattern”?
When you communicate using patterns you are doing more than just sharing LINGO.
Shared pattern vocabularies are POWERFUL. When you communicate with another developer or your team using patterns, you are communicating not just a pattern name but a whole set of qualities, characteristics, and constraints that the pattern represents.
Note
“We’re using the Strategy Pattern to implement the various behaviors of our ducks.” This tells you the duck behavior has been encapsulated into its own set of classes that can be easily expanded and changed, even at runtime if needed.
Patterns allow you to say more with less. When you use a pattern in a description, other developers quickly know precisely the design you have in mind.
Talking at the pattern level allows you to stay “in the design” longer. Talking about software systems using patterns allows you to keep the discussion at the design level, without having to dive down to the nitty-gritty details of implementing objects and classes.
Note
How many design meetings have you been in that quickly degrade into implementation details?
A fully secured application with all those objects used during the process of creation of New Applications, Software, Web Development & more.An upgraded setup of this application user can download from our blog with “Activation Code” who enables the user to use for the Creation Of Web Development, New Software, iOS Applications, Games, Websites Designing, Use HTML/XML Coding, Android Applications Develop, All Languages Coding with “ Adobe Dreamweaver CS6 Keygen With Patch File“. Amazing Web Development, Web Designing, Websites Hosting, New Applications Development based moderated application with Excellent Code Editor “ Adobe Dreamweaver CS6 Crack“. 2019 Web Development Tool!Fully compatible with all the Microsoft, Linux & Mac OS. It’s supported with all the “Python”, “CSS”, “HTML”, “Cobra”, “C##”, “C”, “JavaScripts”, Also, Enable to “Add Social Buttons”, “Apply Effects”, “important Codes Highlight”, “Supported Adobe Air Plug-Ins”, “Manage Portfolios” through ““.
Shared vocabularies can turbo-charge your development team. A team well versed in design patterns can move more quickly with less room for misunderstanding.
Note
As your team begins to share design ideas and experience in terms of patterns, you will build a community of patterns users.
Shared vocabularies encourage more junior developers to get up to speed. Junior developers look up to experienced developers. When senior developers make use of design patterns, junior developers also become motivated to learn them. Build a community of pattern users at your organization.
Note
Think about starting a patterns study group at your organization. Maybe you can even get paid while you’re learning..
We’ve all used off-the-shelf libraries and frameworks. We take them, write some code against their APIs, compile them into our programs, and benefit from a lot of code someone else has written. Think about the Java APIs and all the functionality they give you: network, GUI, IO, etc. Libraries and frameworks go a long way towards a development model where we can just pick and choose components and plug them right in. But.. they don’t help us structure our own applications in ways that are easier to understand, more maintainable and flexible. That’s where Design Patterns come in.
Design patterns don’t go directly into your code, they first go into your BRAIN. Once you’ve loaded your brain with a good working knowledge of patterns, you can then start to apply them to your new designs, and rework your old code when you find it’s degrading into an inflexible mess of jungle spaghetti code.
Developer: Okay, hmm, but isn’t this all just good object-oriented design; I mean as long as I follow encapsulation and I know about abstraction, inheritance, and polymorphism, do I really need to think about Design Patterns? Isn’t it pretty straightforward? Isn’t this why I took all those OO courses? I think Design Patterns are useful for people who don’t know good OO design.
Guru: Ah, this is one of the true misunderstandings of object-oriented development: that by knowing the OO basics we are automatically going to be good at building flexible, reusable, and maintainable systems.
Developer: No?
Guru: No. As it turns out, constructing OO systems that have these properties is not always obvious and has been discovered only through hard work.
Developer: I think I’m starting to get it. These, sometimes non-obvious, ways of constructing object-oriented systems have been collected..
Guru: ..yes, into a set of patterns called Design Patterns.
Developer: So, by knowing patterns, I can skip the hard work and jump straight to designs that always work?
Guru: Yes, to an extent, but remember, design is an art. There will always be tradeoffs. But, if you follow well thought-out and time-tested design patterns, you’ll be way ahead.
Developer: What do I do if I can’t find a pattern?
Guru: There are some object-oriented principles that underlie the patterns, and knowing these will help you to cope when you can’t find a pattern that matches your problem.
Developer: Principles? You mean beyond abstraction, encapsulation, and..
Guru: Yes, one of the secrets to creating maintainable OO systems is thinking about how they might change in the future, and these principles address those issues.
You’ve nearly made it through the first chapter! You’ve already put a few tools in your OO toolbox; let’s make a list of them before we move on to Chapter 2.
Head First is a series of introductory instructional books to many topics, published by O'Reilly Media. It stresses an unorthodox, visually intensive, reader-involving combination of puzzles, jokes, nonstandard design and layout, and an engaging, conversational style to immerse the reader in a given topic.
Originally, the series covered programming and software engineering, but is now expanding to other topics in science, mathematics and business, due to success. The series was created by Bert Bates and Kathy Sierra, and began with Head First Java in 2003.
Concept[edit]
The main idea of the series is to effectively stimulate the brain by:[1]
- Telling stories
- Visualizing
- Using attention-grabbing tactics: page layout, non-standard examples, puzzles, jokes, and other means.
By using metacognition[2] the series' authors and editors try to employ varied methods to present information and accelerate the learning process.
Books[edit]
The offerings in the Head First series are quickly expanding. The books are also delving into subjects that are not directly related to IT, such as Math (Head First Algebra, Head First Statistics), Science (Head First Physics) and project management (Head First PMP). The books are also gaining some popularity for classroom use[citation needed] because of their novel approach to their subject matters.
Head First Design Patterns Ebook
The official web site for the Head First series has forums for each book as well as code downloads and sample chapters. They include:
- Head First Agile (ISBN978-1449314330) by Andrew Stellman and Jennifer Greene
- Head First Ajax (ISBN0-596-51578-2) by Rebecca Riordan
- Head First Algebra (ISBN0-596-51486-7) by Dan Pilone and Tracey Pilone
- Head First Android Development (ISBN1-4493-6213-3) by David Griffiths and Dawn Griffiths
- Head First C (ISBN1-4493-9991-6) by David Griffiths and Dawn Griffiths
- Head First C# (ISBN0-596-51482-4) by Andrew Stellman and Jennifer Greene
- Head First Data Analysis (ISBN0-596-15393-7) by Michael Milton
- Head First Design Patterns (ISBN0-596-00712-4) by Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates
- Head First EJB (ISBN0-596-00571-7) by Kathy Sierra and Bert Bates
- Head First Excel (ISBN0-596-80769-4) by Michael Milton
- Head First 2D Geometry (ISBN0-596-80833-X) by Lindsey Fallow and Dawn Griffiths
- Head First Go (ISBN1-491-96955-5) by Jay McGavren
- Head First HTML with CSS & XHTML (ISBN0-596-10197-X) by Elisabeth Freeman and Eric Freeman
- Head First HTML5 Programming (ISBN1-4493-9054-4) by Eric Freeman and Elisabeth Robson
- Head First iPhone Development (ISBN0-596-80354-0) by Dan Pilone and Tracey Pilone
- Head First iPhone and iPad Development (ISBN1-4493-8782-9) by Dan Pilone and Tracey Pilone
- Head First Java (ISBN0-596-00920-8) by Kathy Sierra and Bert Bates
- Head First JavaScript (ISBN0-596-52774-8) by Michael Morrison (Out of Print)
- Head First JavaScript Programming (ISBN978-1-449-34013-1) by Eric Freeman, Elisabeth Freeman
- Head First jQuery (ISBN1-4493-9321-7) by Ryan Benedetti and Ronan Cranley
- Head First Kotlin (ISBN978-1491996690) by David Griffiths and Dawn Griffiths
- Head First Learn to Code (ISBN978-1-491-95886-5) by Eric Freeman
- Head First Mobile Web (ISBN1-4493-0266-1) by Lyza Danger Gardner, Jason Grigsby
- Head First Networking (ISBN0-596-52155-3) by Ryan Benedetti, Al Anderson
- Head First Object-Oriented Analysis and Design (ISBN0-596-00867-8) by Brett McLaughlin, Gary Pollice and David West
- Head First PHP & MySQL (ISBN978-0-596-00630-3) by Lynn Beighley and Michael Morrison
- Head First Physics (ISBN0-596-10237-2) by Heather Lang
- Head First PMP (ISBN0-596-10234-8) by Jennifer Greene and Andrew Stellman
- Head First Programming (ISBN0-596-80237-4) by Paul Barry and David Griffiths
- Head First Python (ISBN1-4493-8267-3) by Paul Barry
- Head First Rails (ISBN0-596-51577-4) by David Griffiths
- Head First Servlets & JSP (ISBN0-596-51668-1) by Bryan Basham, Kathy Sierra and Bert Bates
- Head First Software Development (ISBN0-596-52735-7) by Dan Pilone and Russ Miles
- Head First SQL (ISBN0-596-52684-9) by Lynn Beighley
- Head First Statistics (ISBN0-596-52758-6) by Dawn Griffiths
- Head First Web Design (ISBN0-596-52030-1) by Ethan Watrall and Jeff Siarto
- Head First WordPress (ISBN0-596-80628-0) by Jeff Siarto
Head First Labs[edit]
Head First Design Patterns Download
O'Reilly established Head First Labs, where books of the series are presented and the main idea behind the series is explained. The Labs also hosts blogs by some of their authors and hosts some applets that complement their books.[3]
Awards[edit]
The books in the series have received three nominations for Product Excellence Jolt Awards, winning in 2005 for Head First Design Patterns, and were recognized on Amazon.com's yearly top 10 list for computer books from 2003 to 2005.
References[edit]
- ^'How Head First Helps You Learn'. Internet Archive.
- ^'The Head First Formula'. Internet Archive.
- ^[1]
External links[edit]
- Head First homepage at the Wayback Machine (archived February 17, 2018)
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Head_First_(book_series)&oldid=917062662'