Appendix A: Brief Overview of GoF Design Patterns

We all have unique thought processes. So, in the early days of software development, engineers faced a common problem—there was no standard to instruct them how to design their applications. Each team followed their own style, and when a new member (experienced or unexperienced) joined an existing team, understanding the architecture was a gigantic task. Senior or experienced members of the team would need to explain the advantages of the existing architecture and why alternative designs were not considered. The experienced developer also would know how to reduce future efforts by simply reusing the concepts already in place. Design patterns address this kind of issue and provide a common platform for all developers. You can think of them as the recorded experience of experts in the field. Patterns were intended to be applied in object-oriented designs with the intention of reuse.

In 1994, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides published the book Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1994). In this book, they introduced the concept of design patterns in software development. These authors became known as the Gang of Four. We will refer them as the GoF throughout this book. The GoF described 23 patterns that were developed by the common experiences of software developers over a period of time. Nowadays, when a new member joins a development team, the developer first learns about the design patterns of the existing system. Then the developer learns about the existing architecture. This allows the developer to actively participate in the development process within a short period of time.

The first concept of a real-life design pattern came from the building architect Christopher Alexander. During his lifetime, he discovered that many of the problems he faced were similar in nature. So, he tried to address those issues with similar types of solutions.

Each pattern describes a problem, which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.

—Christopher Alexander

The GoF assured us that though these patterns were described for buildings and towns, the same concepts can be applied to patterns in object-oriented design. We can substitute the original concepts of walls and doors with objects and interfaces. The common thing in both fields is that, at their cores, patterns are solutions to common problems.

The GoF discussed the original concepts in the context of C++. C# 1.0 was released in 2002, and at the time of this writing, C# 7.0 is available with Visual Studio 2017. In this book, I’ll examine the GoF’s original design patterns but with C#. The book is written in C#, but if you are familiar with any other popular programming languages such as Java, C++, and so on, you will be able to relate because I focus on the design patterns and not on the latest features of C#. In fact, I have purposely chosen simple examples to help you to understand these concepts easily.

Key Points

The following are key points about design patterns:

  • A design pattern is a general, reusable solution for commonly occurring problems. The basic idea is that you can solve similar kinds of problems with similar kinds of solutions. In addition, these solutions have been tested over a long period of time.

  • Patterns generally provide a template of how to solve a problem and can be used in many different situations. At the same time, they help you to achieve the best possible design much faster.

  • Patterns are descriptions of how to create objects and classes and then customize them to solve a general design problem in a particular context.

  • The GoF discussed 23 design patterns. Each of these patterns focuses on a particular object-oriented design. Each pattern can also describe the consequences and trade-offs of use. The GoF categorized these 23 patterns based on their purposes, as shown here:

    1. Creational patterns

      These patterns abstract the instantiation process. You make the systems independent from how their objects are composed, created, and represented. The following five patterns are this category:

      Singleton pattern

      Prototype pattern

      Factory Method pattern

      Builder pattern

      Abstract Factory pattern

    2. Structural patterns

      This category focuses on how classes and objects can be composed to form relatively large structures. They generally use inheritance to compose interfaces or implementations. The following seven patterns fall into this category:

      Proxy pattern

      Flyweight pattern

      Composite pattern

      Bridge pattern

      Facade pattern

      Decorator pattern

      Adapter pattern

    3. Behavioral patterns

      Here the concentration is on algorithms and the assignment of responsibilities among objects. You also need to focus on the communication between them and how the objects are interconnected. The following 11 patterns fall into this category:

      Observer pattern

      Strategy pattern

      Template Method pattern

      Command pattern

      Iterator pattern

      Memento pattern

      State pattern

      Mediator pattern

      Chain of Responsibility pattern

      Visitor pattern

      Interpreter pattern

      The GoF made another classification based on scope, namely, whether the pattern primary focuses on the classes or its objects. Class patterns deal with classes and subclasses. They use inheritance mechanisms, which are static and fixed at compile time. Object patterns deal with objects that can change at run time. So, object patterns are dynamic.

      For a quick reference, you can refer to the following table that was introduced by the GoF:

      A463942_1_En_BookBackmatter_Figa_HTML.jpg
  • In this book, you can start with any pattern you like. I have chosen simple examples so that you can pick up the basic ideas quickly. Read about each pattern, practice, try to solve other problems with the these patterns, and then just keep coding. This process will help you to master the subject quickly.

Q&A Session

  1. What are differences between class patterns and object patterns?

    In general, class patterns focus on static relationship, and object patterns can focus on dynamic relationships. As the names suggest, class patterns focus on classes and their subclasses, and object patterns focus on the object relationships.

    The GoF further differentiated them as follows:

     

    Class Patterns

    Object Patterns

    Creational

    Can defer object creation to subclasses

    Can defer object creation to another object

    Structural

    Focuses on the composition of classes (primarily uses the concept of inheritance)

    Focuses on the different ways of composition of objects

    Behavioral

    Describes the algorithms and execution flows

    Describes how different objects can work together and complete a task

  2. Can two or more patterns be combined in an application?

    Yes. In real-world scenarios, this type of activity is common.

  3. Do these patterns depend on a particular programming language?

    Programming languages can play an important role in pattern use. But the basic ideas are the same; patterns are just like templates, and they will give you some idea in advance of how you can solve a particular problem. In this book, I primarily focus on object-oriented programming with the concept of reuse. But instead of an object-oriented programming language, suppose you have chosen some other language like C. In that case, you may need to think about the core object-oriented principles such as inheritance, polymorphism, encapsulation, abstraction, and so on, and how to implement them. So, the choice of a particular language is always important because it may have some specialized features that can make your life easier.

  4. Should we consider common data structures such as arrays and linked lists as design patterns?

    The GoF clearly excludes these concepts, saying that they are not complex, domain-specific designs for an entire application or subsystem. They can be encoded in classes and reused as is. So, they are not your concern in this book.

Appendix B: Some Useful Resources

This appendix lists some useful resources. The following are helpful books:

  • Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma et al. (Addison-Wesley, 1995)

  • Head First Design Patterns by Eric Freeman and Elisabeth Robson (O’Reilly, 2004)

  • Java Design Patterns by Vaskaran Sarcar (Apress, 2015)

  • Design Patterns For Dummies by Steve Holzner (Wiley Publishing, Inc, 2006)

  • Design Patterns in C# by Jean Paul (Kindle edition, 2012)

The following are helpful online resources/websites:

Appendix C: The Road Ahead

Congratulations! You have reached the end of our journey together. I believe you have enjoyed your learning experience, and this experience can help you to learn and experiment further in this category. I said earlier that if you repeatedly think about the discussions, examples, implementations, and questions and answers discussed in the book, you will have more clarity about them, you will feel more confident about them, and you will remake yourself in the programming world.

A detailed discussion of any particular design pattern would need many more pages, and the size of the book would be too gigantic to digest. So, what is next? You should not forget the basic principle that learning is a continuous process. This book was an attempt to encourage you to learn the core concepts in depth so that you can continue learning.

Still, learning by yourself will not be enough. I suggest you participate in open forums and discussions to get more clarity on the subjects in this book. This process will not only help you, it will help others also.

Lastly, I have a request: criticism is allowed, but please also let me know what you liked about this book. In general, an artistic view and open mind are required to discover the true efforts that are associated with any kind of work. Thank you and happy coding!

Index

A

  1. Abstract Factory pattern

    1. Class Diagram

    2. concept

    3. GoF definition

    4. illustration

    5. implementation

    6. Output

    7. PetAnimalFactory

    8. real-life example

    9. Solution Explorer View

    10. structure

    11. WildAnimalFactory

  2. Adapter pattern

    1. class

    2. Class Diagram

    3. concept

    4. Directed Graph Document

    5. drawbacks

    6. electrical outlet

    7. GetArea() method

    8. GoF definition

    9. implementation

    10. modified illustration

      1. characteristics

      2. implementation

      3. Output

      4. Solution Explorer View

    11. object

    12. Output

    13. Solution Explorer View

    14. user interface and database

  3. Anti-patterns

    1. catalog

    2. causes

    3. concepts

    4. definition

    5. refactoring

    6. software developers

    7. symptoms

B

  1. Bridge pattern

    1. advantages

    2. challenges

    3. Class Diagram

    4. concept

    5. electronic items

    6. GoF definition

    7. GUI frameworks

    8. implementation

    9. Output

    10. simple subclassing

    11. Solution Explorer View

    12. State pattern

  2. Builder pattern

    1. Class Diagram

    2. concept

    3. ConcreteBuilder objects

    4. converting RTF to ASCII

    5. GoF definition

    6. IBuilder interface

    7. implementation

    8. Output

    9. real-life example

    10. Solution Explorer View

    11. vs. Strategy pattern

C

  1. Chain of Responsibility pattern

    1. advantages

    2. AuthenticationErrorHandler

    3. challenges

    4. Class Diagram

    5. concept

    6. customer care representatives

    7. vs. Decorator pattern

    8. Directed Graph Document

    9. EmailErrorHandler and FaxErrorHandler

    10. GoF definition

    11. implementation

    12. message priorities

    13. vs. Observer pattern

    14. Output

    15. request-handling mechanism

    16. software application

    17. Solution Explorer View

  2. Class adapters

  3. Command pattern

    1. advantages

    2. challenges

    3. Class Diagram

    4. concept

    5. Directed Graph Document

    6. GoF definition

    7. implementation

    8. vs. Interpreter pattern

    9. invocation process

    10. invoker

    11. vs. Memento pattern

    12. modified Class Diagram

    13. modified implementation

    14. Modified Output

    15. Output

    16. painting

    17. receivers

    18. Solution Explorer View

    19. WPF

  4. Common Language Runtime (CLR)

  5. Composite pattern

    1. abstract class

    2. advantages

    3. challenges

    4. Class Diagram

    5. college organization

    6. concept

    7. data structure

    8. GoF definition

    9. implementation

    10. iterator design pattern

    11. object-oriented programming

    12. organization and employees

    13. Output

    14. Solution Explorer View

    15. tree data structure

  6. Criticisms

D, E

  1. Decorator pattern

    1. abstract method

    2. advantages

    3. Class Diagram

    4. component

    5. concept

    6. disadvantages

    7. dynamic behavior

    8. dynamic binding

    9. functionality

    10. GoF definition

    11. GUI-based toolkit

    12. implementation

    13. inheritance mechanism

    14. original house

    15. Output

    16. polymorphism

    17. single responsibility

    18. Solution Explorer View

F

  1. Facade pattern

    1. vs. Adapter pattern

    2. advantages

    3. birthday party

    4. vs. Builder pattern

    5. challenges

    6. Class Diagram

    7. concept

    8. ConstructMilanoRobot and DestroyMilanoRobot

    9. Directed Graph Document

    10. GoF definition

    11. implementation

    12. loose coupling

    13. Output

    14. Solution Explorer View

    15. subsystems

  2. Factory Method pattern

    1. analysis

    2. Class Diagram

    3. concept

    4. database users

    5. Directed Graph Document

    6. GoF definition

    7. IAnimalFactory class

    8. implementation

    9. Output

    10. Solution Explorer View

  3. Flyweight pattern

    1. advantages

    2. business cards

    3. challenges

    4. Class Diagram

    5. computer game

    6. concept

    7. GoF definition

    8. implementation

    9. improvements

    10. modified Class Diagram

    11. modified implementation

    12. Modified Output

    13. multithreading

    14. nonshareable

    15. Output

    16. vs. Singleton pattern

    17. small and large robots

    18. Solution Explorer View

    19. vs. State pattern

G, H

  1. Garbage collection (GC)

    1. allocated objects

    2. automatic memory management

    3. characteristics

    4. coding

      1. comment lines

      2. compile

      3. destructor method

      4. developer command prompt

      5. invoking Dispose()

      6. prediction, output

    5. Common Language Runtime (CLR)

    6. compaction

    7. destructors

    8. dirty objects

    9. Dispose() method

    10. Finalize() method

    11. finalizers, drawbacks

    12. GC.Collect()

    13. GC.SuppressFinalize(this)

    14. IDisposable interface

    15. implementation

    16. longer-lived objects

    17. low-priority thread

    18. managed heap

    19. memory consumption rate

    20. Output

    21. short-lived objects

    22. System.GC.Collect() method

I, J, K, L

  1. Interpreter pattern

    1. advantages

    2. challenges

    3. Class Diagram

    4. concept

    5. GoF definition

    6. grammar rules

    7. implementation

    8. Java compiler

    9. Output

    10. Solution Explorer View

    11. translator, foreign language

  2. Iterator pattern

    1. challenges

    2. Class Diagram

    3. collection objects

    4. concept

    5. data structure, student records

    6. Directed Graph Document

    7. First(), Next(), IsDone(), and CurrentItem() methods

    8. GoF definition

    9. implementation

    10. Output

    11. Solution Explorer View

M

  1. Mediator pattern

    1. advantages

    2. airplanes

    3. analysis

    4. Class Diagram

    5. concept

    6. disadvantages

    7. vs. Facade pattern

    8. GoF definition

    9. implementation

    10. interconnections, objects

    11. modified implementation

    12. Modified Output

    13. vs. Observer pattern

    14. Output

    15. participants

    16. registered participants

    17. Solution Explorer View

  2. Memento pattern

    1. advantages

    2. ArrayList

    3. challenges

    4. Class Diagram

    5. concept

    6. Directed Graph Document

    7. drawing application

    8. GoF definition

    9. implementation

    10. mathematical model

    11. memento, originator, and caretaker

    12. modified implementation

    13. Modified Output

    14. operations

    15. Output

    16. Solution Explorer View

  3. Memory leaks

    1. causes

    2. CLR profiler

    3. demonstration code

    4. Diagnostic Tools

    5. heap view, snapshots

    6. leaking rate

    7. IL code of Main() method

    8. modified code

    9. modified results

    10. objects

    11. online application

    12. symptoms

    13. System.OutOfMemoryException

    14. tools

    15. try/finally blocks

    16. using statement

  4. Model-view-controller (MVC) pattern

    1. advantages

    2. architecture, variations

    3. challenges

    4. Class Diagram

    5. components

    6. definition

    7. implementation

    8. Output

    9. restaurant

    10. Solution Explorer View

    11. web programming frameworks

    12. Windows Forms application

N

  1. Null Object pattern

    1. analysis, unwanted input

    2. challenges

    3. Class Diagram

    4. concept

    5. definition

    6. faulty program

    7. if/else blocks

    8. immediate remedy

    9. implementation

    10. Output

    11. Output with valid inputs

    12. runtime exception

    13. Solution Explorer View

    14. vehicles

    15. washing machine

O

  1. Object adapters

  2. Observer pattern

    1. benefits

    2. Chain of Responsibility

    3. challenges

    4. Class Diagram

    5. concept

    6. GoF definition

    7. implementation

    8. notifications

    9. notifications from subject

    10. one-to-many relationships

    11. Output

    12. Publisher-Subscriber model

    13. Solution Explorer View

    14. UI-based example

P, Q, R

  1. Prototype pattern

    1. BasicCar object

    2. Class Diagram

    3. computer world example

    4. concept

    5. demonstration code

    6. Directed Graph Document

    7. GoF definition

    8. implementation

    9. Output

    10. real-life example

    11. Solution Explorer View

  2. Proxy pattern

    1. and adapters

    2. Class Diagram

    3. computer world example

    4. concept

    5. and decorators

    6. Directed Graph Document

    7. GoF definition

    8. implementation

    9. modified implementation

    10. Modified Output

    11. Output

    12. real-life example

    13. Solution Explorer View

S

  1. Simple Factory pattern

    1. challenges

    2. characteristics

    3. Class Diagram

    4. concept

    5. definition

    6. Directed Graph Document

    7. exception

    8. Factory Method and Abstract Factory patterns

    9. implementation

    10. ISimpleFactory

    11. Output

    12. Solution Explorer View

    13. static class

    14. Wikipedia

  2. Singleton pattern

    1. challenges

    2. characteristics

    3. Class Diagram

    4. computer world example

    5. concept

    6. vs. Factory Method pattern

    7. GoF definition

    8. implementation

    9. Output

    10. real-life example

    11. Solution Explorer View

    12. static initialization

  3. State pattern

    1. characteristics

    2. Class Diagram

    3. concept

    4. connection issues

    5. contexts, method parameters

    6. Directed Graph Document

    7. GoF definition

    8. implementation

    9. job-processing system

    10. Output

    11. pros and cons

    12. real-world scenario

    13. Solution Explorer View

  4. Strategy pattern

    1. abstract class

    2. advantages

    3. challenges

    4. Class Diagram

    5. concept

    6. default behavior

    7. Directed Graph Document

    8. GoF definition

    9. implementation

    10. inheritance mechanism

    11. Output

    12. Solution Explorer View

  5. Static initialization

T, U

  1. Template Method pattern

    1. advantages

    2. BasicEngineering class

    3. challenges

    4. Class Diagram

    5. concept

    6. GoF definition

    7. implementation

    8. Modified Output

    9. Modified Program

    10. Output

    11. Solution Explorer View

V

  1. Visitor pattern

    1. Class Diagram

    2. Composite pattern

      1. implementation

      2. Output

      3. Solution Explorer View

      4. tree structure

    3. concept

    4. drawbacks

    5. encapsulation

    6. GoF definition

    7. implementation

    8. Output

    9. public APIs

    10. Solution Explorer View

W, X, Y, Z

  1. Windows Presentation Foundation (WPF)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.214.155