Explain the observer pattern.
interface Observer { Sequence diagram converted to program.
void update(String message); class Customer { Explain GRASP design pattern w in java
} private Order order; class Book {
// Concrete Observer // Constructo private String title;
private boolean isAvailable;
class ConcreteObserver implements Observer { public Customer() {
[Link] = new Order(); // Step 6: Object creation public Book(String title, boolean
private String name;
isAvailable) {
public ConcreteObserver(String name) { }
[Link] = title;
[Link] = name; // Method to create an order
[Link] = isAvailable;
} public void createOrder() {
}
@Override [Link]("Customer creates an order.");
[Link](); // Step 3: Method calls in sequence public boolean isAvailable() {
public void update(String message) { return isAvailable;
}}
[Link](name + " received: " + message); }}
class Order {
}} class Library {
private Payment payment;
// Subject class // Constructor private List<Book> books;
class Subject { public Order() { public Library(List<Book> books) {
private List<Observer> observers = new ArrayList<>(); [Link] = new Payment(); // Step 6: Object creation [Link] = books;
public void addObserver(Observer observer) { } }
[Link](observer); // Method to process an order public boolean checkAvailability(String
} title) {
public void createOrder() {
public void removeObserver(Observer observer) { for (Book book : books) {
[Link]("Order created.");
if ([Link]()) {
[Link](observer); [Link](); // Step 3: Call method in order
return true;
} }}
}}
public void notifyObservers(String message) {
class Payment { return false;
for (Observer observer : observers) { }}
[Link](message); // Method to process payment
} }} public void processPayment() {
// Usage [Link]("Payment processed successfully.");
}}
public class Main {
public class Main {
public static void main(String[] args) {
public static void main(String[] args) {
Subject subject = new Subject(); Customer customer = new Customer();
Observer observer1 = new ConcreteObserver("Observer 1"); [Link]();
Observer observer2 = new ConcreteObserver("Observer 2"); }}
[Link](observer1);
[Link](observer2);
[Link]("Hello, Observers!");
}} Explain Singleton pattern with code by using java
// Singleton Class
Example Factory design pattern. class Singleton {
(creating object of class B=pizza class assigned to class A=restaurant class) private static Singleton instance;
// Private constructor prevents instantiation from other classes
Define the Pizza class: private Singleton() {}
// [Link] // Static method to get the single instance
public class Pizza { public static Singleton getInstance() {
private String type; if (instance == null) {
public Pizza(String type) { instance = new Singleton();
[Link] = type; }
} return instance;
public void prepare() { }}
[Link]("Preparing " + type + " pizza."); // Main Class to test
}} public class Main {
Create the Restaurant class with a factory method: public static void main(String[] args) {
// [Link] Singleton singleton1 = [Link]();
public class Restaurant { Singleton singleton2 = [Link]();
public Pizza createPizza(String type) { // Check if both references point to the same instance
return new Pizza(type); [Link](singleton1 == singleton2); // Output: true
}} }}
Use the Restaurant class to create Pizza objects:
public class Main {
public static void main(String[] args) { Explain the Factory design pattern.
Restaurant restaurant = new Restaurant(); interface Shape {
Pizza pizza = [Link]("Margherita"); Explain information expert pattern with code by using java void draw();
[Link](); // Book Class (Information Expert) }
}} class Book { class Circle implements Shape {
private String title; public void draw() {
Explain the Adapter design pattern. private double price; [Link]("Drawing a Circle.");
// Target Interface private static final double TAX_RATE = 0.10; // 10% tax }}
interface Target { Book(String title, double price) { class Rectangle implements Shape {
void request(); [Link] = title; public void draw() {
} [Link] = price; [Link]("Drawing a
// Adaptee Class with an incompatible interface } Rectangle.");
class Adaptee { // Method to calculate the total price including tax }}
void specificRequest() { public double calculateTotalPrice() { class ShapeFactory {
[Link]("Specific request from Adaptee."); return price + (price * TAX_RATE); // Method to create shape objects based on
}} } the shape type
// Adapter Class // Getter for title (optional) public Shape getShape(String shapeType)
class Adapter implements Target { public String getTitle() { {
private Adaptee adaptee; return title; if
Adapter(Adaptee adaptee) { }} ([Link]("CIRCLE")) {
[Link] = adaptee; // Main Class to test return new Circle();
} public class Main { } else if
public void request() { public static void main(String[] args) { ([Link]("RECTANGL
[Link](); // Adapts specificRequest to request Book book = new Book("Java Programming", 50.0); E")) {
}} [Link]("Title: " + [Link]()); // Output: Title: return new Rectangle();
// Main Class Java Programming }
public class Main { [Link]("Total Price: " + return null;
public static void main(String[] args) { [Link]()); // Output: Total Price: 55.0 }}
Adaptee adaptee = new Adaptee(); }}
Target target = new Adapter(adaptee);
[Link](); // Output: Specific request from Adaptee.
}}