Java Advanced question accumulation (II.)

Source: Internet
Author: User

Brief introduction of 23 designs

  1. Abstract Factory: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
  2. Adapter Mode (Adapter): Transforms the interface of one class into another interface that the customer wants. The adapter mode makes it possible for classes that cannot work together because of incompatible interfaces.
  3. Bridge mode: separates the abstract part from his implementation so that they can all change independently.
  4. build Mode (builder): Separates the construction of a complex object from his mark representation so that the same build process can create different representations.
  5. Responsibility Chain mode (Chain of Responsibility): To decouple the sender and receiver of the request, so that multiple objects have almost processed the request. The objects are linked to a chain and passed along this chain to the request, knowing that there is an object handling him.
  6. Command mode: Encapsulates a request as an object, allowing the client to be parameterized with different requests, arranging or logging the request log, and supporting operations that can be canceled.
  7. compositing mode (Composite): Combines objects into a tree structure to represent a "partial-whole" hierarchy. It enables customers to have consistent use of individual objects and composite objects.
  8. decorating Mode (Decorator): Dynamically adds some additional responsibilities to an object. In terms of extension functionality, the way he can generate subclasses is more flexible.
  9. façade mode (facade): Provides a consistent interface for a set of interfaces in a subsystem, and the façade pattern defines a high-level interface that makes the subsystem more container-used.
  10. Factory Method (Factory mehtod): Defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.
  11. enjoy meta mode (Flyweight): Use shared technology to effectively support a large number of fine-grained objects.
  12. interpreter mode (interpreter): Given a language, defines a representation of his grammar and defines an interpreter that interprets the sentences in the language using that representation.
  13. Iterative sub-pattern (Iterator): Provides a way to sequentially access individual elements in an aggregated object without exposing the object's internal representation.
  14. Mediator Mode (mediator): Uses a Mediation object to encapsulate a series of object interactions. The mediator makes the objects do not require an explicit internal representation.
  15. Memo Mode (Memento): captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to the saved state.
  16. Observer Mode (OBSERVER): Defines a one-to-many dependency between objects so that all dependent objects are notified and refreshed automatically when the state of an object changes.
  17. Original Model mode (PROTOTYPE): Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototype.
  18. Proxy Mode: Provides a proxy for other objects to control access to this object.
  19. Singleton mode (Singleton): guarantees that a class has only one instance and provides a global access point to access him.
  20. State mode: Allows an object to change his behavior when its internal state changes. The object appears to have modified the class to which he belongs.
  21. policy mode (strategy): Defines a series of algorithms, encapsulates them one by one, and allows them to replace each other. This mode allows the change of the algorithm to be used independently of his customers.
  22. Template Mehtod: Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. The template method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm.
  23. Visitor Mode (Visitor): Represents a function of each element in the structure of an object. This pattern enables you to define new actions that act on these elements without changing the class of each element.
Talk about design patterns with projects

Template Method Patterns combined with instances said, in the abstract class of submitting orders, will submit the method of fixing the order for example, check the shopping cart, payment method, check the user address, generate invoices and other methods.
Due to different order types (such as web-side orders, iOS orders, Andorid-side orders, etc.) and specific implementation of different, so the specific submission order with the abstract method definition let the specific subclass to implement the Order method.
Factory mode when doing an e-commerce system due to the different order instances created by the order type.
Therefore, the Factory mode is used to obtain the corresponding order instance through the order type.
policy Mode before the e-commerce system has a regular engine, through different algorithm rules to implement different policies.
The Observer pattern defines a one-to-many dependency between objects, and when an object's state changes, all objects dependent on it are notified of Automatic Updates
The Observer has all the observer instances and loops all observers to notify the change when the observer state changes.
In the common language event monitoring.
The adapter pattern is to turn a class's interface into another interface that the client expects. Divided into class adapters and object adapters.
The class adapter implementation implements the interface that needs to be adapted by inheriting the implementation class that is implemented partially.
An object adapter implements an interface that needs to be adapted by implementing an implementation class entity that contains partial implementations in a class.
You can choose the adapter mode when you want to apply the stored class, but the interface does not meet the requirements.
The singleton mode Spring container is a typical singleton pattern, which is implemented by defining a static final variable to point to an instance, and privatizing the constructor,
Provides the public method to provide an instance, ensuring that the class has only one instance.

Is the GC algorithm understood?

GC's basic algorithms are reference counting, copy, edit-sweep, edit-compress, generational, etc.
reference count: by adding a counter for each object, the number of active references to the object is recorded through the counter.
If the counter is 0, it means that the object is not referenced by any variables, and GC can be garbage collected.
copy: divides the memory space into 2 equal regions, using only one region at a time. During garbage collection, traverse the current area of use and copy the objects being used to another area.
edit-Sweep: The first stage iterates through all the references, tags the live objects, and the second stage iterates over the heap, erasing the unmarked objects. However, the entire application needs to be paused and memory fragmentation is generated.
tag-compression: The first stage marks the live object, and the second stage compresses the unlabeled objects and puts them sequentially into a block in the heap.
Generational: based on the object life cycle analysis of garbage collection algorithm, the object is divided into young generation, old generation, durable generation, the different life cycle using different algorithms for recycling.

Spring Core Container principle

The core of spring's interior is the IOC container. is a container that has dependency injection capabilities. The container creates an instance from the configuration file and creates a dependency between the individual instances.
The objects that are initialized, assembled, and managed by the spring container are called beans. Represents the interface as Beanfactory. Provides the most basic functionality of the IOC container.
While ApplicationContext inherits Beanfactory, it adds support for more enterprise-class features.
Due to the IOC's support for enterprise-class, the interface-oriented development model is widely adopted.
By the way, spring's AOP-oriented aspect programming is actually a way of intercepting messages through a dynamic proxy, decorating the message and replacing the execution of the original object's behavior.
Generally used for permissions, caching, log processing, transactions, synchronization and other places of application.

JVM Memory management mechanism
JVM Memory Distribution Reference Map

The program counter is used to indicate which instruction is executed. Multithreading is performed by CPU switching. Therefore, each thread needs to have its own independent program counter after switching threads in order to recover the threads before the switchover.
The virtual machine stack is the memory model that the Java method executes. Stacks are stored in the stack frame, each stack frame corresponding to the method called, in the stack frame including the local variable table, the operand stack, point to the current method of the class to run the constant pool of reference, method return address and so on. When the thread executes the method, a corresponding stack frame is created, and the stack frame of the resume is stacked, and the stack frame is stacked when the method finishes executing.
A local variable table is used to store local variables in a method. For variables of the base data type, the value is stored directly, and a reference to the object is stored for a variable of the reference type.
The role of the operand stack is in the program, so the computational process is done with the help of the operand stack.
A reference to a run-time pool , which is likely to require a constant in the class, must have a reference pointing to the run-time, as the method executes.
method returns an address that, when a method executes, returns the place where it was previously called, so a method return address must be saved in the stack frame.
Because the methods that each thread is executing may be different, each thread will have a separate virtual machine stack.
The local method stack and the virtual machine stack are similar in function and principle. The difference is in the service object. The virtual machine stack is a service to the Java method, and the local method stack serves the local method.
The heap is used to store the object itself, as well as arrays. Resources are thread-shared, and references in the virtual machine stack point to specific instances in the heap, with only one heap in the JVM.
A method area is also a thread-shared area that stores information about each class (such as the name of a class, method information, field information, and so on), static variables, constants, and compiler-compiled code.

The role of the Java thread pool

In order to resolve a server to accept a large number of short threads of the request to avoid generating a lot of creation and destruction of thread actions, reduce the server on the creation and destruction of the thread spent on the time and consumption of system resources.


FU: Welcome to the vast number of Java program Ape will own experience in the face of questions, bloggers will update to the article, thank you support ~


Java Advanced question accumulation (II.)

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.