文档章节

18 Java design pattern questions.

Simon253
 Simon253
发布于 2014/05/01 08:41
字数 1513
阅读 14
收藏 0
1. When to use Strategy Design Pattern in Java?
Strategy pattern in quite useful for implementing set of related algorithms e.g. compression algorithms, filtering strategies etc. Strategy design pattern allows you to create  Context  classes, which uses Strategy implementation classes for applying business rules. This pattern follow open closed design principle and quite useful in Java. One example of Strategy pattern from JDK itself is a  Collections.sort()  method and  Comparator interface , which is a strategy interface and defines strategy for comparing objects. Because of this pattern, we don't need to modify  sort()  method (closed for modification) to compare any object, at same time we can implement Comparator interface to define new comparing strategy (open for extension).

2. What is Observer design pattern in Java? When do you use Observer pattern in Java?
This is one of the most common Java design pattern interview question. Observer pattern is based upon notification, there are two kinds of object Subject and Observer. Whenever there is change on subject's state observer will receive notification. See What is Observer design pattern in Java with real life example  for more details.

3. Difference between Strategy and State design Pattern in Java?
This is an interesting Java design pattern interview questions as both Strategy and State pattern has same structure. If you look at UML class diagram for both pattern they look exactly same, but there intent is totally different. State design pattern is used to define and mange state of object, while Strategy pattern is used to define a set of interchangeable algorithm and let's client to choose one of them. So Strategy pattern is a client driven pattern while Object can manage there state itself.

4. What is decorator pattern in Java? Can you give an example of Decorator pattern?
Decorator pattern is another popular java design pattern question which is common because of its heavy usage in  java.io package.  BufferedReader  and  BufferedWriter  are good example of decorator pattern in Java. See  How to use Decorator pattern in Java  fore more details.

5. When to use Composite design Pattern in Java? Have you used previously in your project?
This design pattern question is asked on Java interview not just to check familiarity with Composite pattern but also, whether candidate has real life experience or not.  Composite pattern  is also a core Java design pattern, which allows you to treat both whole and part object to treat in similar way. Client code, which deals with Composite or individual object doesn't differentiate on them, it is possible because Composite class also implement same interface as there individual part. One of the good example of Composite pattern from JDK is  JPanel  class, which is both Component and Container.  When paint()  method is called on  JPanel , it internally called  paint()  method of individual components and let them draw themselves. On second part of this design pattern interview question, be truthful, if you have used then say yes, otherwise say that you are familiar with concept and used it by your own. By the way always remember, giving an example from your project creates better impression.

6. What is Singleton pattern in Java? 
Singleton pattern in Java is a pattern which allows only one instance of Singleton class available in whole application. java.lang.Runtime is good example of Singleton pattern in Java. There are lot's of follow up questions on Singleton pattern see  10 Java singleton interview question answers  for those followups 

7. Can you write thread-safe Singleton in Java?
There are multiple ways to write thread-safe singleton in Java e.g by writing singleton using double checked locking, by using static Singleton instance initialized during  class loading.  By the way using Java  enum  to create thread-safe singleton is most simple way. See  Why Enum singleton is better in Java  for more details.

8. When to use Template method design Pattern in Java? Template pattern is another popular core Java design pattern interview question. I have seen it appear many times in real life project itself. Template pattern outlines an algorithm in form of template method and let subclass implement individual steps. Key point to mention, while answering this question is that template method should be final, so that subclass can not override and change steps of algorithm, but same time individual step should be abstract, so that child classes can implement them.

9. What is Factory pattern in Java? What is advantage of using static factory method to create object?
Factory pattern in Java is a creation Java design pattern and favorite on many Java interviews.Factory pattern used to create object by providing static factory methods. There are many advantage of providing factory methods e.g. caching immutable objects, easy to introduce new objects etc. See  What is Factory pattern in Java and benefits  for more details.

10. Difference between Decorator and Proxy pattern in Java? Another tricky Java design pattern question and trick here is that both Decorator and Proxy implements interface of the object they decorate or encapsulate. As I said, many Java design pattern can have similar or exactly same structure but they differ in there intent. Decorator pattern is used to implement functionality on already created object, while Proxy pattern is used for controlling access to object. One more difference between Decorator and Proxy design pattern is that, Decorator doesn't create object, instead it get object in it's constructor, while Proxy actually creates objects.

11. When to use Setter and Constructor Injection in Dependency Injection pattern?
Use Setter injection to provide optional dependencies of an object, while use Constructor injection to provide mandatory dependency of an object, without which it can not work. This question is related to  Dependency Injection design pattern  and mostly asked in context of Spring framework, which is now become an standard for developing Java application. Since Spring provides IOC container, it also gives you way to specify dependencies either by using setter methods or constructors. You can also take a look my  previous post  on same topic.


12. What is difference between Factory and Abstract factory in Java
see   here  to answer this Java design pattern interview question. 


13. When to use Adapter pattern in Java? Have you used it before in your project?
Use Adapter pattern when you need to make two class work with incompatible interfaces. Adapter pattern can also be used to encapsulate third party code, so that your application only depends upon Adapter, which can adapt itself when third party code changes or you moved to a different third party library. By the way this Java design pattern question can also be asked by providing actual scenario.

14. Can you write code to implement producer consumer design pattern in Java?
Producer consumer design pattern is a concurrency design pattern in Java which can be implemented using multiple way. if you are working in Java 5 then its better to use Concurrency util to implement producer consumer pattern instead of plain old wait and notify in Java .  Here is a good example of implementing  producer consumer problem using BlockingQueue in Java .

15. What is Open closed design principle in Java?
Open closed design principle is one of the SOLID principle defined by Robert C. Martin, popularly known as Uncle Bob. This principle advices that a code should be open for extension but close for modification. At first this may look conflicting but once you explore power of polymorphism, you will start finding patterns which can provide stability and flexibility of this principle. One of the key example of this is State and Strategy design pattern, where Context class is closed for modification and new functionality is provided by writing new code by implementing new state of strategy. See  this  article to know more about Open closed principle.

16. What is Builder design pattern in Java? When do you use Builder pattern ?
Builder pattern in Java is another creational design pattern in Java and often asked in Java interviews because of its specific use when you need to build an object which requires multiple properties some optional and some mandatory. See  When to use Builder pattern in Java  for more details  


17. Can you give an example of  SOLID design principles in Java?
There are lots of SOLID design pattern which forms acronym SOLID, read this  list of SOLID design principles for Java programmer   to answer this Java interview question.

18. What is difference between Abstraction and Encapsulation in Java?
I have already covered answer of this Java interview question in my previous post as  Difference between encapsulation and abstraction in Java . See there to answer this question. 


This was my list of  10 popular design pattern interview question in Jav a. I have not included MVC (Model View Controller) design pattern because that is more specific to J2EE and  Servlet JSP interview , but if you are going for any Java interview which demands experience in J2EE than you must prepare MVC design pattern. That's all on Java design pattern interview question and answers. Please let us know if you have any other interesting question on Java design pattern.

© 著作权归作者所有

Simon253
粉丝 4
博文 94
码字总数 114771
作品 0
美国
私信 提问
Design Principles from Design Patterns

Leading-Edge Java Design Principles from Design Patterns A Conversation with Erich Gamma, Part III by Bill Venners June 6, 2005 Erich Gamma lept onto the software world stage in......

Jerikc
2014/09/15
29
0
外企公司招聘Senior Symbian Engineer based in Beijing

Title: Senior Symbian Engineer Lcoation: Beijing, China Position description: Work within a Symbian/S60 product/platform program in the areas of analysis, design, implementation......

Angela WANG
2009/11/16
300
0
mina-statemachine源码解析-目录

先发一个目录吧,估计全部写完,还是要花费几天的时间。 mina-statemachine源码解析 前言 1. 相关技术介绍 1.1 状态机 1.2 java 动态代理 1.3 Java annotation技术 1.4 factory design patte...

算法与编程之美
2012/12/04
177
1
某些人就不要拉低Java黑的平均水平了

说的就是你@eechen 我们这些成天黑Java的,一般都黑Java的type system是unsound的,Java的泛型实现是错误的(Type Erasure),Java没有值类型影响实时性,Java没有运算符重载,Java既没有fir...

江湖习气
2017/04/03
516
2
Spring中@Pattern的使用

Controller层 为Controller添加表示启用验证。 为Controller中特定请求添加正则表达式校验。 Json响应 总结 很方便,还可以自己捕获这个异常的对返回进行自定义处理。 参考: Spring Boot RES...

亚林瓜子
2018/07/19
37
0

没有更多内容

加载失败,请刷新页面

加载更多

计算机实现原理专题--二进制减法器(二)

在计算机实现原理专题--二进制减法器(一)中说明了基本原理,现准备说明如何来实现。 首先第一步255-b运算相当于对b进行按位取反,因此可将8个非门组成如下图的形式: 由于每次做减法时,我...

FAT_mt
今天
4
0
好程序员大数据学习路线分享函数+map映射+元祖

好程序员大数据学习路线分享函数+map映射+元祖,大数据各个平台上的语言实现 hadoop 由java实现,2003年至今,三大块:数据处理,数据存储,数据计算 存储: hbase --> 数据成表 处理: hive --> 数...

好程序员官方
今天
7
0
tabel 中含有复选框的列 数据理解

1、el-ui中实现某一列为复选框 实现多选非常简单: 手动添加一个el-table-column,设type属性为selction即可; 2、@selection-change事件:选项发生勾选状态变化时触发该事件 <el-table @sel...

everthing
今天
6
0
【技术分享】TestFlight测试的流程文档

上架基本需求资料 1、苹果开发者账号(如还没账号先申请-苹果开发者账号申请教程) 2、开发好的APP 通过本篇教程,可以学习到ios证书申请和打包ipa上传到appstoreconnect.apple.com进行TestF...

qtb999
今天
10
0
再见 Spring Boot 1.X,Spring Boot 2.X 走向舞台中心

2019年8月6日,Spring 官方在其博客宣布,Spring Boot 1.x 停止维护,Spring Boot 1.x 生命周期正式结束。 其实早在2018年7月30号,Spring 官方就已经在博客进行过预告,Spring Boot 1.X 将维...

Java技术剑
今天
18
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部