Java design Pattern Template method pattern

Source: Internet
Author: User

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win, design patterns so that code is truly engineering, design patterns are the cornerstone of software engineering, like a block of bricks and tiles in the building. The rational use of design patterns in the project can solve many problems perfectly, each of which has corresponding principles in the present, each of which describes a recurring problem around us, and the core solution of the problem, which is why it can be widely used.

1. Classification of design PatternsCreate Model:
    • Singleton, Singleton mode: guarantees that a class has only one instance and provides a global access point to access it.
    • Abstract Factory, Abstraction Factory: Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.
    • Factory method, Factory methods: Define an interface for creating objects, let subclasses decide which class to instantiate, and Factory method to defer instantiation of a class to subclasses.
    • Builder, Build mode: Separates the construction of a complex object from his presentation, allowing the same build process to create different representations.
    • Prototype, prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.
Behavioral mode:
    • Iterator, Iterator mode: Provides a way to sequentially access individual elements of an aggregated object without exposing an internal representation of the object.
    • OBSERVER, Observer pattern: Defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified of Automatic Updates.
    • Template method, which defines the skeleton of an algorithm in an operation, and delays some steps into subclasses, Templatemethod allows subclasses to redefine the algorithm to some specific steps without altering the structure of an algorithm.
    • command, Order mode: Encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue requests and log requests, and support revocable operations.
    • State mode: Allows an object to change his behavior when its internal state changes. The object seems to have changed his class.
    • Strategy, Strategy mode: Define a series of algorithms, encapsulate them one by one, and allow them to replace each other, this mode allows the algorithm to be independent of the customers who use them.
    • China of Responsibility, responsibility chain mode: Enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the requested communicated and receivers
    • Mediator, Mediator mode: Encapsulates the object interaction of some columns with a mediation object.
    • Visitor, Visitor pattern: represents an operation that acts on elements in an object's structure, allowing you to define new actions that act on this element without changing the individual element classes.
    • Interpreter, Interpreter mode: Given a language, defines a representation of his grammar and defines an interpreter that uses that representation to interpret sentences in the language.
    • Memento, Memo mode: captures the internal state of an object without destroying the object, and saves the state outside the object.
Structural mode:
    • Composite, Combined mode: combines objects into a tree structure to represent a partial overall relationship, Composite makes the user consistent with the use of individual objects and composite objects.
    • Facade, Appearance mode: Provides a consistent interface for a set of interfaces in a subsystem, Fa?ade provides a high-level interface that makes subsystems easier to use.
    • Proxy, Agent mode: Provides a proxy for other objects to control access to this object
    • Adapter, adapter mode: Converts a class of interfaces into another interface that the customer wants, and the Adapter mode makes it possible for those classes to work together because the interface is incompatible.
    • Decrator, Decoration mode: Dynamically adds some additional responsibilities to an object, and in terms of added functionality, the decorator mode is more flexible than generating subclasses.
    • Bridge mode: Separates the abstract part from its implementation, allowing them to change independently.
    • Flyweight, enjoy meta mode
2. Template method
The overview template method pattern is the behavior pattern of the class. Prepare an abstract class, implement some of the logic in the form of concrete methods and concrete constructors, and then declare some abstract methods to force subclasses to implement the remaining logic. Different subclasses can implement these abstract methods in different ways, thus having different implementations of the remaining logic. This is the intent of the template method pattern.For example, the skeleton of an algorithm in an operation is defined, and the steps are deferred to the subclass. The template method allows subclasses to redefine some specific steps of an algorithm without altering the structure of an algorithm. roles in the patternabstract Class (AbstractClass): The template method is implemented, and the skeleton of the algorithm is defined. Concrete Class (Concreteclass): Implements the abstract method in the abstract class, has completed the complete algorithm. Design examples
1 //Abstract Templates2  Public Abstract classAbstractClass3 {4     protected Abstract voiddoanyting ();5 6     protected Abstract voiddosomething ();7 8      Public voidTemplatemethod ()9     {Ten doanyting (); One dosomething (); A     } - } -  the  Public classConcreteClass1extendsAbstractClass - { - @Override -     protected voiddoanyting () +     { -System.out.println ("Do Class1 Anything"); +     } A  at @Override -     protected voiddosomething () -     { -System.out.println ("Do Class1 something"); -     } - } in  -  Public classConcreteClass2extendsAbstractClass to { + @Override -     protected voiddoanyting () the     { *System.out.println ("Do class2 Anything"); $     }Panax Notoginseng  - @Override the     protected voiddosomething () +     { ASystem.out.println ("Do Class2 something"); the     } + } -  $  Public classClient $ { -      Public Static voidMainFinalstring[] args) -     { the         FinalAbstractClass C1 =NewConcreteClass1 (); -         FinalAbstractClass C2 =NewConcreteClass2 ();Wuyi C1.templatemethod (); the C2.templatemethod (); -     } Wu}

Java design Pattern Template method pattern

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.