Common design Patterns in Java interview

Source: Internet
Author: User

I. Classification of design Patterns

In general, design patterns fall into three broad categories:

Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.

Behavioral pattern, total 11 kinds:

The policy mode, the template method pattern, the observer pattern, the iterative sub-mode, the responsibility chain mode, the command mode, the memo mode, the state mode, the visitor pattern, the mediator mode, the interpreter mode.

1. Please list a few common design patterns in the JDK?

Singleton patterns are used in runtime, calendar, and other classes. The Factory mode is used for a variety of immutable classes such as Boolean, like the Boolean.valueof method. The observer pattern is used for swing and a lot of time listening. Adorner mode is used for multiple Java IO classes.

2. What is design mode? Have you ever used any design patterns in your code?

Design patterns are the methods that the world's various programmers try and test to solve specific design problems. Design patterns are an extension of code availability.

What is a singleton design pattern in 3.Java? Please use Java to write thread safe single case mode

4. Explain what is the Observer pattern?

The Observer pattern is based on the state of the object and the observer's communication so that they can make the appropriate action. A simple example is a weather system that must be reflected in a view that is displayed to the public when the weather changes. This view object is a body, and the different views are the observers.

5. What are the main benefits of using the factory model? Where to use?

The biggest benefit of the factory pattern is the increased level of encapsulation at the time the object was created.

If you use a factory to create objects, you can then replace the original product implementation or class with a more advanced and higher performance implementation, which does not require any modification at the call layer.

6. Give a Java-implemented decorative pattern (decorator design pattern)? Does it work at the object level or at the class level?

Adornment mode increases the ability of a single object. The Java IO is used everywhere in decorative mode, typically buffered series classes such as BufferedReader and bufferedwriter, which enhance reader and writer objects to achieve Buffer-level reads and writes that improve performance.

7. Design an ATM machine, please say your design ideas.

Designing financial systems, for example, must know that they should work properly under all circumstances. Whether it is a power outage or otherwise, the ATM should maintain the correct state (transaction), think of lock (locking), Transaction (transaction

), error conditions (condition), boundary conditions (boundary condition), and so on. Although you can't think of a specific design, if you can point out the non-functional requirements, ask some questions and think about the boundary conditions, these will be good.

8. In Java, when to use overloading, when to rewrite?

If you see different implementations of a class that have different ways of doing the same thing, then you should rewrite (overriding), while overloading (overloading) is doing the same thing with different inputs. In Java, overloaded method signatures are different, and overrides are not.

9. For example, what situations would be more inclined to use abstract classes than interfaces?

Both interfaces and abstract classes follow the "interface-oriented rather than implementation-coding" design principle, which increases the flexibility of the code to accommodate changing requirements. Here are a few points to help you answer this question:

In Java, you inherit only one class, but you can implement multiple interfaces. So once you inherit a class, you lose the chance to inherit other classes. Interfaces are often used to represent subordinate descriptions or behaviors such as Runnable, Clonable, Serializable, and so on, so when you use abstract classes to represent behavior, your classes cannot be both Runnable and clonable ( Note: This means that if runnable is implemented as an abstract class, because you cannot inherit two classes in Java, your class can have multiple different behaviors at the same time when you use an interface. In some applications where time requirements are high, it tends to use abstract classes, which can be slightly faster than interfaces. Abstract classes are a better choice if you want to standardize a series of behaviors within the class inheritance hierarchy and can be better coded in the same place. Sometimes interfaces and abstract classes can be used together, interfaces define functions, and default implementations are defined in abstract classes.

10. What is the MVC design pattern? Give an example of an MVC design pattern.

11. What is the responsibility chain model?

12 What is adapter mode? Give examples of implementing adapter patterns in Java.

Common design Patterns in Java interview

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.