Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the interview questions for Java design pattern

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Java design pattern interview questions, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

1: what is a design pattern?

It is a practically proven solution to a specific problem in a particular environment.

2: what are design patterns used for?

Looking for a suitable partner

Determine the granularity of an object

Specify the interface of the object

Describe the implementation of the object

Application of reuse mechanism

Reuse proven solutions to solve a certain type of problem to reduce

The purpose of workload, improving the accuracy and so on.

3: what is object granularity

The number of methods in the object is granularity.

4: what are the rules that basic Java programming should follow?

Interface-oriented programming, giving priority to object composition

5: the scope of application of design patterns

In a specific class of problems that can be solved.

6: briefly describe what is the singleton pattern, and the problems he solves, the application environment, the solution, and the nature of the model?

A pattern in which only one instance of a class exists at any one time.

The singleton pattern needs to be used in an environment where there is an area from which to access and maintain some type of data globally.

The solution is to ensure that only one instance of a class exists.

The essence is that instances share the same memory area.

7: code example: two implementation methods of singleton pattern, and illustrate the advantages and disadvantages

Public class Test {public Test () {} private static Test test= new Test (); public static Test getInstance () {return test;}} public class Test {private static Test test= null; private Test () {} public static Test getInstance () {if (test==null) {test= new Test ();} return test;}}

The second way is not to create an instance of the class each time, but only for the first time

8: briefly describe what is the factory model, and the problems he solves, the application environment, the solution, and the nature of the model.

A pattern that uses a factory to solve the problem of interface selection.

Application environment: factory pattern is needed when a class cannot predict what kind of objects to create or when a class needs subclasses to specify the objects to be created.

Solution: define an interface to create objects and let subclasses decide which class to instantiate.

The essence is to choose different interfaces according to different situations.

9: code example: implementation of factory pattern

Public class Factory {public static Sample creator (int which) {if (which==1) {return new SampleA ();} else if (which==2) return new SampleB ();} Public class MyFactory {Public static myFactory f = null; Public MyFactory () {} Public static MyFactory getInstance () {If (f==null) {F=new MyFactory () }} Public DBDAO getDAO () {Return new DBDAOImpl ();}} 10: describe what the value object pattern is, and the problem it solves, the environment it applies, the solution, the nature of the pattern.

A pattern used to encapsulate a set of data into an object.

Solve the problem: when the number of calls to remote methods increases, the performance of the related application will degrade greatly.

Solution: when using a value object, you can get the entire object with just one method call, rather than using multiple method calls to get the value of each field in the object.

Essence: it is to encapsulate multiple values that need to be passed into an object and pass them at once.

11: code example: implementation of the value object pattern

Public class UserModel {private String userId; private String userName; public void setUserId (String id) {this.userId = id;} public String getUserId () {return userId;} public void setUserName (String name) {this.userName = name;} public String getUserName () {return userName;} 12: briefly describe what the DAO pattern is, and the problems it solves, the environment it applies, the solutions it solves, and the nature of the pattern.

DAO: data access object

Solve the problem: data access varies depending on the data source. Access to persistent storage (such as databases) varies greatly depending on the type of storage (relational database, object-oriented database, plain file, etc.) and vendor implementation. How to shield these complexities from modules outside the storage layer to provide a unified call storage implementation. The distributed problem of the program.

The solution is to abstract the data access logic into special resources, that is, to isolate the interface of the system resources from its underlying access mechanism; by packaging the calls to data access, data access objects can promote data access to different database types and patterns.

The essence of DAO: shielding one change at a time.

Essence: layering is the adapter between the system components and the data source. (one layer shields one change)

13: code example: implementation of DAO pattern

Public interface CustomerDAO {public int insertCustomer (...); public Collection selectCustomersVO (...);} 14: what is the Open-closed Rule (OCP)

Extensible but cannot change existing modules

Open to extension and closed to modification

15: what is the law of substitution (LSP)

A function that uses a reference to a base class (superclass) must be able to derive a specific class (subclass) object without knowing it

Use in the case of type

16: how to use the design patterns we have learned to build a reasonable application structure

The interface is used for isolation, and then the value object and the factory class are exposed at the same time. If the function of data storage is needed, it will interact with the data storage layer through DAO schema.

17: build a common and reasonable Java application package structure

Ui (presentation layer)

Business--factory,ebi,ebo

Dao--factory,dao,impl

Thank you for reading this article carefully. I hope the article "what are the interview questions for Java design patterns" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report