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() {
this.order = new Order(); // Step 6: Object creation public Book(String title, boolean
private String name;
isAvailable) {
public ConcreteObserver(String name) { }
this.title = title;
this.name = name; // Method to create an order
this.isAvailable = isAvailable;
} public void createOrder() {
}
@Override System.out.println("Customer creates an order.");
order.createOrder(); // Step 3: Method calls in sequence public boolean isAvailable() {
public void update(String message) { return isAvailable;
}}
System.out.println(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<>(); this.payment = new Payment(); // Step 6: Object creation this.books = books;
public void addObserver(Observer observer) { } }
observers.add(observer); // Method to process an order public boolean checkAvailability(String
} title) {
public void createOrder() {
public void removeObserver(Observer observer) { for (Book book : books) {
System.out.println("Order created.");
if (book.isAvailable()) {
observers.remove(observer); payment.processPayment(); // Step 3: Call method in order
return true;
} }}
}}
public void notifyObservers(String message) {
class Payment { return false;
for (Observer observer : observers) { }}
observer.update(message); // Method to process payment
} }} public void processPayment() {
// Usage System.out.println("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"); customer.createOrder();
Observer observer2 = new ConcreteObserver("Observer 2"); }}
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.notifyObservers("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() {}
// Pizza.java // 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();
this.type = type; }
} return instance;
public void prepare() { }}
System.out.println("Preparing " + type + " pizza."); // Main Class to test
}} public class Main {
Create the Restaurant class with a factory method: public static void main(String[] args) {
// Restaurant.java Singleton singleton1 = Singleton.getInstance();
public class Restaurant { Singleton singleton2 = Singleton.getInstance();
public Pizza createPizza(String type) { // Check if both references point to the same instance
return new Pizza(type); System.out.println(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 = restaurant.createPizza("Margherita"); Explain information expert pattern with code by using java void draw();
pizza.prepare(); // Book Class (Information Expert) }
}} class Book { class Circle implements Shape {
private String title; public void draw() {
Explain the Adapter design pattern. private double price; System.out.println("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(); this.title = title; public void draw() {
} this.price = price; System.out.println("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 {
System.out.println("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) { }} (shapeType.equalsIgnoreCase("CIRCLE")) {
this.adaptee = adaptee; // Main Class to test return new Circle();
} public class Main { } else if
public void request() { public static void main(String[] args) { (shapeType.equalsIgnoreCase("RECTANGL
adaptee.specificRequest(); // Adapts specificRequest to request Book book = new Book("Java Programming", 50.0); E")) {
}} System.out.println("Title: " + book.getTitle()); // Output: Title: return new Rectangle();
// Main Class Java Programming }
public class Main { System.out.println("Total Price: " + return null;
public static void main(String[] args) { book.calculateTotalPrice()); // Output: Total Price: 55.0 }}
Adaptee adaptee = new Adaptee(); }}
Target target = new Adapter(adaptee);
target.request(); // Output: Specific request from Adaptee.
}}