What is the difference between the Decorator and Flyweight patterns?
- The Decorator pattern is used to dynamically add or override behavior of an object, while the Flyweight pattern is used to reduce the number of objects created by sharing objects that are costly to create.
- The Decorator pattern is used to dynamically add or override behavior of an object, while the Flyweight pattern is used to share objects that are costly to create.
- The Decorator pattern is used to reduce the number of objects created by sharing objects that are costly to create, while the Flyweight pattern is used to dynamically add or override behavior of an object.
- The Decorator pattern is used to share objects that are costly to create, while the Flyweight pattern is used to dynamically add or override behavior of an object.
The Decorator pattern is used to dynamically add or override behavior of an object, while the Flyweight pattern is used to share objects that are costly to create.
What is the purpose of the Decorator design pattern?
- To attach additional responsibilities to an object dynamically.
- To create objects that are interchangeable with their base classes.
- To ensure that a class has only one instance and provide a global point of access to that instance.
- To manage the flow of control between objects in a complex system.
The Decorator design pattern is used to attach additional responsibilities to an object dynamically. This allows for greater flexibility and modularity in the design of a system, as well as the ability to add or remove responsibilities as needed.
What does program to interfaces, not implementations mean?
- This means that when designing software, the focus should be on defining the behavior of the components, rather than the specific implementations of those components.
- This means that when designing software, the focus should be on defining the data structures and algorithms used by the components, rather than the specific implementations of those components.
- This means that when designing software, the focus should be on defining the input and output of the components, rather than the specific implementations of those components.
- This means that when designing software, the focus should be on defining the interfaces between different components, rather than the specific implementations of those components. This makes the code more flexible and easier to maintain, as it reduces the amount of coupling between components.
This means that when designing software, the focus should be on defining the interfaces between different components, rather than the specific implementations of those components. This makes the code more flexible and easier to maintain, as it reduces the amount of coupling between components.
What is the Null Object pattern?
- A creational design pattern that provides a way to instantiate an object in a superclass, but allow subclasses to alter the type of objects that will be created.
- A design pattern used to ensure a class has only one instance, while providing a global access point to this instance for the entire system.
- A software design pattern that enables an object to be passed around as a value.
- The Null Object pattern is a behavioral design pattern that provides a default object as a representative of "null" in the context of design patterns.
The Null Object pattern is a behavioral design pattern that provides a default object as a representative of "null" in the context of design patterns.
What is the purpose of the Command design pattern?
- To create objects that are interchangeable with their base classes.
- To encapsulate operations in an object and pass them as messages.
- To manage the flow of control between objects in a complex system.
- To provide a way to invoke operations without coupling the invoker object to the target object that performs the operation.
The Command design pattern decouples the invoker of an operation from the target object that performs the operation. This allows for greater flexibility and modularity in the design of a system.
What is Flyweight pattern?
- A design pattern that allows objects with incompatible interfaces to work together by converting the interface of one object into an interface expected by the client
- A design pattern that creates objects in a step-by-step manner, allowing for more control over the object creation process
- A design pattern that provides a simplified interface to a complex system, allowing the client to interact with the system through a single, unified interface
- A design pattern that uses shared objects to support large numbers of similar objects efficiently
Flyweight pattern is a design pattern that uses shared objects to support large numbers of similar objects efficiently. The pattern involves creating a shared object that can be used by multiple clients, reducing the amount of memory required to store the objects. This pattern can be useful in situations where you need to create large numbers of similar objects, but you want to minimize the memory footprint of the application.
What is an example of Adapter pattern?
- An example of Adapter pattern would be a TV with an HDMI port that needs to be connected to a DVD player with a composite video port. The adapter would convert the composite video output into HDMI, allowing the TV and DVD player to work together
- An example of Adapter pattern would be a computer mouse that uses a USB-A port, but the computer only has a USB-C port. The adapter would convert the USB-A port into USB-C, allowing the mouse to be used with the computer
- An example of Adapter pattern would be a laptop with a USB-C port that needs to be connected to a monitor with a VGA port. The adapter would convert the USB-C output into VGA, allowing the laptop and monitor to work together
- An example of Adapter pattern would be a mobile phone that uses a micro-USB port for charging, but the user has a charger with a lightning port. The adapter would convert the lightning port into micro-USB, allowing the mobile phone to be charged
An example of Adapter pattern would be a TV with an HDMI port that needs to be connected to a DVD player with a composite video port. The adapter would convert the composite video output into HDMI, allowing the TV and DVD player to work together. This example demonstrates the adapter pattern by showing how two objects with incompatible interfaces can work together through the use of an adapter.
What is the difference between a Singleton and a Prototype pattern?
- None of the above
- The Singleton and Prototype patterns are the same
- The Singleton pattern creates multiple instances, while the Prototype pattern creates only one instance
- The Singleton pattern creates only one instance, while the Prototype pattern creates multiple instances
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance, while the Prototype pattern provides a mechanism for creating object instances that are exact copies of a prototype object
Name some of the design patterns used in JDK library.
- Abstract Factory, Builder, Factory Method, Singleton
- Adapter, Facade, Observer, Prototype
- Chain of Responsibility, Command, Interpreter, Iterator
- Decorator, Mediator, State, Strategy
Some of the design patterns used in the JDK library include Decorator, Mediator, State, and Strategy. These patterns are used in various classes and APIs of the Java Standard Library to provide a clean and efficient implementation of common design problems.
How can you create Singleton class in Java?
- By declaring a private constructor and a public static method that returns the only instance of the class
- By declaring all of its variables and methods as final
- By declaring all of its variables and methods as static
- By implementing the Cloneable interface
A Singleton class in Java can be created by declaring a private constructor and a public static method that returns the only instance of the class. This ensures that there is only one instance of the class in the JVM and that it can be easily accessed by other objects in the system.