The application of design pattern in the project

Source: Internet
Author: User

Simple plant design patterns are applied in the project

Project Usage scenarios: Depending on the manufacturer, there are different forms of report printing, and for report printing they have a common section, print the report header, print the bottom, and print the middle section.

We can use simple Factory mode, abstract out the interface of the printed report, different manufacturers to implement different print interface.


The singleton mode is applied in the project

Scenario 1: When the DOM reads the configuration file, because it is often used in this class, we can set a global class without having to new as many classes each time, saving resource consumption.

Scenario 2: At the time of spring's IOC dependency injection, the default is to use a singleton pattern, such as a class in the controller layer that is injected to use the service layer, and the service class is a global variable. Spring is implemented by a singleton pattern, and of course we can modify (prototype) by configuring spring's Springmvc.xml file.

Singleton mode action: guarantees that a class has only one instance and provides a global access point to access it.

as shown in the following:



  1. /**
  2. * @ClassName: Singleton_simple
  3. * @Description: Single case Mode--a Hungry man mode
  4. * @author Summer
  5. * @date -7-14
  6. *
  7. */
  8. public class Singleton_simple {
  9. Private static final Singleton_simple simple = new Singleton_simple ();
  10. Private Singleton_simple () {}
  11. public static Singleton_simple getinstance () {
  12. return simple;
  13. }
  14. }

Of course, for reading the configuration file class we can write as a static class, then why use a singleton mode? The b EAN created in the Spring factoryis the singleton mode and the prototype format, why not use the form of a static class? Or can you choose a static method for a non-static class?

1, static class is loaded in the JVM when the memory is occupied, and the singleton mode, can be used when the load. That is, a singleton can delay initialization.

2, a singleton can inherit the class, you can implement the interface, and static class cannot.

3, a singleton class can be integrated, his method can be overwritten.

      4, the Singleton class can be used for multiple units.

5, and static methods for non-static classes, if a class does not need to extend the words can be used in this way, with a single case can be, but need to expand the class, preferably using a singleton.

Application of proxy mode in the project

Application Scenario: The core of Spring AOP is the proxy, which is decoupled by the implementation of the proxy pattern.

Proxy Mode: Provides a proxy for other objects to control access to this object.

The JDK proxy mode is as follows:



Application of the appearance mode in the project

Scenario: A layer is extracted from the interface and the business logic layer, the controller layer calls the facade layer, and the service layer is called by the facade layer.

Appearance mode (facade): Provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes this subsystem easier to use.













Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The application of design pattern in the project

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.