IntroductionDesign patterns are very popular among software developers. A design pattern is a well-described solution to a common software problem. Show
Some of the benefits of using design patterns are:
Java design patterns are divided into three categories - creational, structural, and behavioral design patterns. This article serves as an index for all the Java design pattern articles. Creational Design PatternsCreational design patterns provide solutions to instantiate an 1. Singleton PatternThe singleton pattern restricts the instantiation of a 2. Factory PatternThe factory design pattern is used when we have a superclass with multiple subclasses and based on input, we need to return one of the subclasses. This pattern takes out the responsibility of the instantiation of a 3. Abstract Factory PatternThe abstract factory pattern is similar to the factory pattern and is a factory of factories. If you are familiar with the factory design pattern in Java, you will notice that we have a single factory class that returns the different subclasses based on the input provided and the factory class uses 4. Builder PatternThe builder pattern was introduced to solve some of the problems with factory and abstract Factory design patterns when the object contains a lot of attributes. This pattern solves the issue with a large number of optional parameters and
inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final 5. Prototype PatternThe prototype pattern is used when the Structural Design PatternsStructural design patterns provide different ways to create a 1. Adapter PatternThe adapter design pattern is one of the structural design patterns and is used so that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called an adapter. 2. Composite PatternThe composite pattern is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply the composite design pattern. 3. Proxy PatternThe proxy pattern provides a placeholder for another 4. Flyweight PatternThe flyweight design pattern is used when we need to create a lot of String pool implementation in Java is one of the best examples of flyweight pattern implementation. 5. Facade PatternThe facade pattern is used to help client applications easily interact with the system. 6. Bridge PatternWhen we have interface hierarchies in both interfaces as well as implementations, then the bridge design pattern is used to decouple the interfaces from the implementation and to hide the implementation details from the client programs. The implementation of the bridge design pattern follows the notion of preferring composition over inheritance. 7. Decorator PatternThe decorator design pattern is used to modify the functionality of an object at runtime. At the same time, other instances of the same class will not be affected by this, so the individual object gets the modified behavior. The decorator design pattern is one of the structural design patterns (such as adapter pattern, bridge pattern, or composite pattern) and uses abstract classes or interface with the composition to implement. We use inheritance or composition to extend the behavior of an object, but this is done at compile-time, and it’s applicable to all the instances of the class. We can’t add any new functionality to remove any existing behavior at runtime – this is when the decorator pattern is useful. Behavioral Design PatternsBehavioral patterns provide a solution for better interaction between objects and how to provide loose-coupling and flexibility to extend easily. 1. Template Method PatternThe template method pattern is a behavioral design pattern and is used to create a method stub and to defer some of the steps of implementation to the subclasses. The template method defines the steps to execute an algorithm, and it can provide a default implementation that might be common for all or some of the subclasses. 2. Mediator PatternThe mediator design pattern is used to provide a centralized communication medium between different objects in a system. If the objects interact with each other directly, the system components are tightly-coupled with each other which makes maintainability cost higher and not flexible to extend easily. The mediator pattern focuses on providing a mediator between objects for communication and implementing loose-coupling between objects. The mediator works as a router between objects, and it can have its own logic to provide a way of communication. 3. Chain of Responsibility PatternThe chain of responsibility pattern is used to achieve loose-coupling in software design where a request from the client is passed to a chain of objects to process them. Then the object in the chain will decide who will be processing the request and whether the request is required to be sent to the next object in the chain or not. We know that we can have multiple 4. Observer PatternAn observer design pattern is useful when you are interested in the state of an Java provides an built-in platform for implementing the observer pattern through the 5. Strategy PatternStrategy pattern is used when we have multiple algorithms for a specific task, and the client decides the actual implementation be used at runtime. A strategy pattern is also known as a policy pattern. We define multiple algorithms and let client applications pass the algorithm to be used as a parameter. One of the best examples of this pattern is the 6. Command PatternThe command pattern is used to implement loose-coupling in a request-response model. In this pattern, the request is sent to the invoker and the invoker passes it to the encapsulated command object. The command object passes the request to the appropriate method of receiver to perform the specific action. 7. State PatternThe state design pattern is used when an 8. Visitor PatternThe visitor pattern is used when we have to perform an operation on a group of similar kinds of objects. With the help of a visitor pattern, we can move the operational logic from the objects to another class. 9. Interpreter PatternThe interpreter pattern is used to define a grammatical representation of a language and provides an interpreter to deal with this grammar. 10. Iterator PatternThe iterator pattern is one of the behavioral patterns and is used to provide a standard way to traverse through a group of objects. The iterator pattern is widely used in Java Collection Framework where the iterator interface provides methods for traversing through a 11. Memento PatternThe memento design pattern is used when we want to save the state of an object so that we can restore it later on. This pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the Memento pattern is implemented with two Miscellaneous Design PatternsThere are a lot of design patterns that don’t come under Gang of Four design patterns. Let’s look at some of these popular design patterns. 1. DAO Design PatternThe Data Access Object (DAO) design pattern is used to decouple the data persistence logic to a separate layer. DAO is a very popular pattern when we design systems to work with databases. The idea is to keep the service layer separate from the data access layer. This way we implement the separation of logic in our application. 2. Dependency Injection PatternThe dependency injection pattern allows us to remove the hard-coded dependencies and make our application loosely-coupled, extendable, and maintainable. We can implement dependency injection in Java to move the dependency resolution from compile-time to runtime. Spring framework is built on the principle of dependency injection. 3. MVC PatternModel-View-Controller (MVC) Pattern is one of the oldest architectural patterns for creating web applications. ConclusionThis article summarized Java design patterns. You can check out Java design patterns example code from our GitHub Repository. Continue your learning with more Java tutorials. Which of the following design concepts is the quantitative measure of how closely related classes are linked?Coupling: a quantitative measure of how closely related classes are linked.
Is a qualitative measure of how closely the classes in a design class diagram are linked together?Coupling is a qualitative measure of how closely the classes in a design class diagram are linked. A simple way to think about coupling is as the number of navigation arrows on the design class diagram.
Which of the following is included at the bottom compartment of a design class?The middle compartment contains the class's attributes. Each attribute can specify a name, a type and a default value. Only the name is compulsory. The bottom compartment contains the class's operations.
What are the characteristics of a Web service quizlet?What are the features of web services? It is available over the Internet or private (intranet) networks. It uses a standardized XML messaging system. It is not tied to any one operating system or programming language.
|