Java Interview--Design pattern

Source: Internet
Author: User
Tags instance method


Create type
  1. Factory method Mode
    Description: Use different factories to produce different products.
    Example: The production of TV factory, the production of different brands of mobile phones, if there is only one factory class, when the need to add a brand of mobile phone needs to modify the code, violation of the opening and closing principle
    Improved: Define a factory interface, and then define different implementation classes to produce different brands of mobile phones, add brand only need to add an implementation class to

    Definition: Factory method mode is also called Factory mode, the factory parent is responsible for defining the public interface that creates the product object, and the factory subclass is responsible for generating the specific product object, which is to defer the instantiation operation of the product class to the factory subclass, that is, through the factory subclass to determine exactly which particular product class should be instantiated.

    Application: Like the set frame, collection defines the iterator (). You can return a iterator (iterator) object that iterates through the collection, and the specific collection class returns a specific iterator object by implementing the method, which is the factory method




  2. Abstract Factory mode
    Description: The specific factory production is no longer the production of a certain product, but the production of a series of products
    Example: Haier factory production Haier TV, Haier air conditioning, Tcl factory production TCL TV, Tcl Air conditioning

    Definition: Provides an interface to create a series of related or interdependent objects without specifying their specific class



  3. Single-Case mode
    Example: the identity card number is unique

    Static Private member variable, private constructor, static public fetch instance method

    Definition: To ensure that a class has only one instance, and that it instantiates itself and provides this instance to the entire system, this class becomes a singleton class that provides a method for global access

    Applications: such as the Java.lang.Runtime class, each Java application has a unique runtime object that can interact with its running environment.
    In the popular Java EE Framework spring, when we try to get an instance of a class from the spring container, spring is created by default in singleton mode, where the bean instance in spring's bean factory has only one
    <bean id = "date" class = "java.util.Date" scope = "Singleton"/>
      
Structural type
    1. Adapter mode
      The classes that convert an interface to another interface that the customer wants, so that incompatible interfaces can work together

      Definition: Convert an interface into another interface that the customer wants, and the adapter mode can work with those classes that are incompatible with the interface, and its alias is the wrapper

      Application: The database connection tool JDBC enables the program to connect to the database and use the SQL language to query and manipulate the data. JDBC gives a common abstraction interface for clients, and each JDBC driver software for a specific database engine (such as SQL Server, Oracle, MySQL, and so on) is an adapter software between the JDBC interface and the database engine interface. The appropriate adapter software is required between the abstract JDBC interface and the individual database engine APIs, which is the driver for each of the different database engines




    2. Appearance mode
      Example: the navigation bar of the homepage of the website, can enter different subsystems
      Definition: Communication between an external and a subsystem must be done through a single visual object, providing a consistent interface to a set of interfaces in the subsystem, which defines a high-level interface that makes the subsystem easier to use
      Appearance mode is a very high frequency, easy to understand the model, in almost all the software can see the appearance of the application, such as the vast majority of B/s system has a home page or navigation pages, where the home and navigation page is the appearance of the role


    3. Proxy mode
      Example: Find a woman ticket, work busy can not personally find, find a matchmaking agency (agent, intermediary 、、、)
      In some cases, a customer can not or do not want to refer directly to an object, at this time through a third party called "Agent" to implement indirect reference, proxy objects play an intermediary role, can be removed through the proxy object customers can see the content and services or add additional services required by customers
      Definition: Provides a proxy for an object and controls a reference to the original object by a proxy object

      Application: The AOP technology in spring Framework is also an application of proxy mode




Behavioral type
    1. Iterator mode
      Example: Watching TV, through the remote control, you can see different channels, the TV has a lot of channels, through the remote control to traverse, do not need to know how these channels in the TV inside how to store
      Definition: Provides a way to access the aggregated object without exposing the object's internal representation
      Application: Collection in iterator ()
      In Java, there are few custom iterators that use the JDK built-in iterators to

    2. Observer pattern
      Example: Click a button, pop up a dialog box; stock up, investors excited here, button, investors are observers, mouse, stock is the observer's target
      In the MVC architecture, the model provides the data that the view layer observes, the same data may have different representations in the view layer, and if the model layer data changes, the chart object also changes, meaning that the chart object relies on the data object provided by the model layer, So any state changes to the data objects should be notified immediately.
      Definition: Defines a one-to-many relationship between objects, so that whenever an object changes, its dependent objects are notified and updated automatically

    3. Policy mode
      Free switching and scaling for algorithms
      Definition: Define some column algorithms, encapsulate each algorithm, and allow them to replace each other. Policy patterns change the algorithm to pre-use their customers independently

      The purpose of the strategy mode is to separate the definition of the algorithm, that is, to separate the behavior and environment of the algorithm, to put the definition of the algorithm in a special policy class, each policy class encapsulates an algorithm implementation, and in order to extend convenience, introduces the abstract policy class, and defines the abstract algorithm in the abstract policy class. The environment class is programmed for abstract policy classes and conforms to the "dependency reversal principle". In the advent of the algorithm, only need to add a new experience to implement the abstract policy class specific policy classes.

Java Interview--Design pattern

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.