Design patterns with real time example

后端 未结 4 804
陌清茗
陌清茗 2021-02-03 15:35

I want to learn Design patterns with real time example. So can any one suggest where I can start.

相关标签:
4条回答
  • 2021-02-03 16:06

    Note: Adding brief definition with real life and Java API examples.

    Creational

    How do you want to create objects?

    Prototype : A fully initialized instance to be copied or cloned
    Example : initial status of chess game

    • java.lang.Object#clone()

    Builder - Separates the construction of a complex object from its representation so that the same construction process can create different representations
    Example : Kitchen is a Factory, Chef is a Builder where waiter tell to chef "pizza with cheese, onion". Here chef exposed attributes but hidden implementation.

    • java.lang.StringBuilder

    Singleton - A class of which only a single instance can exist
    Example : President of a country

    • java.lang.Runtime#getRuntime()

    Factory Method - Creates an instance of several derived classes.
    Example : In an organisation HR works as factory method. Here development team request type of resource need to HR. Based on request type, HR provide resource to Development team.

    • java.util.Calendar#getInstance()

    Abstract Factory - Creates an instance of several families of classes
    Example : HP, Samsung and Dell laptops are uses Intel and AMD processor.

    • javax.xml.parsers.DocumentBuilderFactory#newInstance()

    Factory Method vs Abstract Factory

    Structural

    This design patterns is all about Class and Object composition i.e. How do you want structure the software component.

    Proxy - An object representing another object
    Example : check book leaf, credit card, debit card are proxy for Money and a customer representative to order a product.

    • java.rmi.*, the whole API actually.

    Composite - Gives an unified interface to a leaf and composite.
    Example : File System in Operating Systems, Directories are composite and files are leaves. System call Open is single interface for both composite and leaf.

    Decorator - Gives additional feature to leaf, while giving unified interface.
    Example : 1) Adding discounts on an order 2) gun is a deadly weapon on it's own. But you can apply certain "decorations" to make it more accurate, silent and devastating.

    • All subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor taking an instance of same type.

    Facade - Simplifies multiple complex components with single interface
    Example : Control Panel

    • javax.faces.context.ExternalContext, which internally uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, et

    Adapter - Provides different interface for an interface.
    Example : Power Adapters

    • java.util.Arrays#asList()

    Flyweight - A fine-grained instance used for efficient sharing
    Example : Dial tone

    • java.lang.Integer#valueOf(int) (also on Boolean, Byte, Character, Short and Long)

    Behavioral

    This design patterns is all about Class's objects communication i.e How do you want a behavior in software?

    Chain of Responsibility - A way of passing a request between a chain of objects
    Example : Loan or Leave approval process

    • javax.servlet.Filter#doFilter()

    Iterator - Sequentially access the elements of a collection
    Example : Next/Previous buttons on TV

    • All implementations of java.util.Iterator & java.util.Enumeration

    State - Alter an object's behavior when its state changes
    Example : A fan wall control

    Observer - A way of notifying change to a number of classes
    Example : Bidding or auction

    • Publish/Subscribe JMS API

    Visitor - Defines a new operation to a class without change
    Example : Taxi

    Template - Defer the exact steps of an algorithm to a subclass
    Example : A blue print

    • All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
    • All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
    • javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.
      • JMSTemplate HibernateTemplate and JdbcTemplate in Spring

    Command - Encapsulate a command request as an object
    Example : The "Guest Check" at a diner is an example of a Command pattern. The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of "checks" used by each waiter is not dependent on the menu, and therefore they can support commands to cook many different items.

    • All implementations of java.lang.Runnable

    Memento - Capture and restore an object's internal state
    Example : save the state in a game & Undo/Redo operation in Windows

    • All implementations of java.io.Serializable

    Mediator - Defines simplified communication between classes
    Example : Air Traffic Controller(ATC)

    Strategy - A Strategy defines a set of algorithms that can be used interchangeably.
    Example : Modes of transportation

    • java.util.Comparator#compare(), executed by among others Collections#sort().
    • javax.servlet.http.HttpServlet, the service() and all doXXX() methods take HttpServletRequest and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).
    • javax.servlet.Filter#doFilter()

    Courtesy.
    Design Patterns

    0 讨论(0)
  • 2021-02-03 16:14

    I believe these are the two standard references:

    1. Head First Design Patterns
    2. Design Patterns

    From what I've heard, the first is easier to start with.

    The steps I took to investigate this:

    1. google "design pattern books"
    2. read this question
    0 讨论(0)
  • 2021-02-03 16:20

    If you are looking for C# design patterns then refer:

    'C# Design Patters a tutorial' - Jame W Cooper

    0 讨论(0)
  • 2021-02-03 16:24

    Now here is an answer which would attract a lot of downvotes. But I'll tell it any way.

    My suggestion is, "Don't learn design patterns!!!"

    By sticking into design patterns, you restrict your creativity. Also, some design patterns have bad sides, which they don't tell you. For example, the Singleton pattern can cause issues if not used with care.

    Also, IMO, some famous design patterns were created with one language in mind, to solve a particular issue with that language. However, evolved languages like Python and Javascript can be used pretty amazingly without sticking into design patterns.

    Instead of learning design patterns, learn programming paradigms, and the internal concepts. I'll copy paste the following list from Wikipedia,

    • imperative which allows side effects,
    • functional which disallows side effects,
    • declarative which does not state the order in which operations execute,
    • object-oriented which groups code together with the state the code modifies,
    • procedural which groups code into functions,
    • logic which has a particular style of execution model coupled to a particular style of syntax and grammar, and
    • symbolic programming which has a particular style of syntax and grammar

    Of course you can read through the standards design patterns to get some basic idea. But don't learn them from A to Z. It can destroy your creativity.

    0 讨论(0)
提交回复
热议问题