Wednesday, December 13, 2023
HomeSoftware TestingHigh 100 Java Design Patterns Interview Questions And Solutions (2023)

High 100 Java Design Patterns Interview Questions And Solutions (2023)


Top 100 Java Design Patterns Interview Questions and Answers

1. What’s the Singleton Design Sample?

Reply:

The Singleton sample ensures a category has just one occasion and offers a worldwide level of entry to that occasion.

Instance:

public class Singleton {
    non-public static Singleton occasion;

    non-public Singleton() {}

    public static Singleton getInstance() {
        if (occasion == null) {
            occasion = new Singleton();
        }
        return occasion;
    }
}

2. Clarify the Manufacturing facility Technique Design Sample.

Reply:

The Manufacturing facility Technique sample defines an interface for creating objects, permitting subclasses to change the kind of objects that will likely be created.

Instance:

interface Product {
    void create();
}

class ConcreteProductA implements Product {
    public void create() {
        System.out.println("Product A created.");
    }
}

// ... ConcreteProductB and different product implementations ...

summary class Creator {
    public summary Product factoryMethod();
}

class ConcreteCreatorA extends Creator {
    public Product factoryMethod() {
        return new ConcreteProductA();
    }
}

// ... ConcreteCreatorB and different creator implementations ...

3. What’s the Observer Design Sample?

Reply:

The Observer sample defines a one-to-many dependency between objects. When one object modifications state, all its dependents are notified and up to date mechanically.

Instance:

import java.util.ArrayList;
import java.util.Checklist;

interface Observer {
    void replace(String message);
}

class ConcreteObserver implements Observer {
    non-public String title;

    public ConcreteObserver(String title) {
        this.title = title;
    }

    public void replace(String message) {
        System.out.println(title + " acquired message: " + message);
    }
}

class Topic {
    non-public Checklist<Observer> observers = new ArrayList<>();
    non-public String message;

    // ... addObserver, removeObserver, setMessage strategies ...
}

4. Clarify the Builder Design Sample.

Reply:

The Builder sample separates the development of a fancy object from its illustration, permitting the identical building course of to create totally different representations.

Instance:

class Product {
    non-public String partA;
    non-public String partB;

    // ... setters for partA and partB ...
}

interface Builder {
    void buildPartA();
    void buildPartB();
    Product getResult();
}

class ConcreteBuilder implements Builder {
    non-public Product product = new Product();

    // ... buildPartA, buildPartB, and getResult strategies ...
}

5. Describe the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

6. What’s the Decorator Design Sample?

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

7. Are you able to clarify the Prototype Design Sample?

Reply:

The Prototype sample creates new objects by copying an current object, generally known as the prototype. This may be extra environment friendly than creating a brand new occasion from scratch.

Instance:

class Prototype implements Cloneable {
    @Override
    public Prototype clone() throws CloneNotSupportedException {
        return (Prototype) tremendous.clone();
    }
}

8. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

9. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

10. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

11. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

12. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

13. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

14. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

15. What’s the Composite Design Sample?

Reply:

The Composite sample lets purchasers deal with particular person objects and compositions of objects uniformly. It composes objects into tree constructions to signify part-whole hierarchies.

Instance:

interface Part {
    void operation();
}

class Leaf implements Part {
    public void operation() {
        System.out.println("Leaf operation");
    }
}

class Composite implements Part {
    non-public Checklist<Part> parts = new ArrayList<>();

    public void addComponent(Part part) {
        parts.add(part);
    }

    public void removeComponent(Part part) {
        parts.take away(part);
    }

    public void operation() {
        System.out.println("Composite operation");
        for (Part part : parts) {
            part.operation();
        }
    }
}

16. Clarify the Flyweight Design Sample.

Reply:

The Flyweight sample minimizes reminiscence utilization or computational bills by sharing as a lot as doable with associated objects. It’s used for efficiency optimization.

Instance:

class Flyweight {
    non-public String information;

    public Flyweight(String information) {
        this.information = information;
    }

    public String getData() {
        return information;
    }
}

17. What’s the Proxy Design Sample?

Reply:

The Proxy sample offers a surrogate or placeholder for one more object to regulate entry to it. It’s used so as to add a stage of management over accessing a useful resource.

Instance:

interface Picture {
    void show();
}

class RealImage implements Picture {
    non-public String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    public void show() {
        System.out.println("Displaying " + fileName);
    }

    non-public void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }
}

class ProxyImage implements Picture {
    non-public RealImage realImage;
    non-public String fileName;

    public ProxyImage(String fileName){
        this.fileName = fileName;
    }

    public void show() {
        if(realImage == null){
            realImage = new RealImage(fileName);
        }
        realImage.show();
    }
}

18. Clarify the Adapter Design Sample.

Reply:

The Adapter sample permits the interface of an current class for use as one other interface. It’s typically used to make current courses work with others with out modifying their supply code.

Instance:

interface MediaPlayer {
    void play(String audioType, String fileName);
}

class AudioPlayer implements MediaPlayer {
    public void play(String audioType, String fileName) {
        // Implement playback logic right here
    }
}

interface AdvancedMediaPlayer {
    void playVlc(String fileName);
    void playMp4(String fileName);
}

class VlcPlayer implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Implement Vlc playback logic right here
    }

    public void playMp4(String fileName) {
        // Do nothing
    }
}

class Mp4Player implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Do nothing
    }

    public void playMp4(String fileName) {
        // Implement Mp4 playback logic right here
    }
}

19. What’s the Bridge Design Sample?

Reply:

The Bridge sample separates abstraction from implementation in order that they will fluctuate independently. It’s helpful when each should be prolonged independently.

Instance:

interface DrawAPI {
    void drawCircle(int radius, int x, int y);
}

class RedCircle implements DrawAPI {
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }
}

class GreenCircle implements DrawAPI {
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }
}

20. Clarify the Composite Design Sample.

Reply:

The Composite sample lets purchasers deal with particular person objects and compositions of objects uniformly. It composes objects into tree constructions to signify part-whole hierarchies.

Instance:

interface Part {
    void operation();
}

class Leaf implements Part {
    public void operation() {
        System.out.println("Leaf operation");
    }
}

class Composite implements Part {
    non-public Checklist<Part> parts = new ArrayList<>();

    public void addComponent(Part part) {
        parts.add(part);
    }

    public void removeComponent(Part part) {
        parts.take away(part);
    }

    public void operation() {
        System.out.println("Composite operation");
        for (Part part : parts) {
            part.operation();
        }
    }
}

21. What’s the Facade Design Sample?

Reply:

The Facade sample offers a simplified interface to a set of interfaces in a subsystem, making it simpler to make use of.

Instance:

class SubsystemA {
    public void operationA() {
        System.out.println("Subsystem A - Operation A");
    }
}

class SubsystemB {
    public void operationB() {
        System.out.println("Subsystem B - Operation B");
    }
}

class SubsystemC {
    public void operationC() {
        System.out.println("Subsystem C - Operation C");
    }
}

class Facade {
    non-public SubsystemA subsystemA;
    non-public SubsystemB subsystemB;
    non-public SubsystemC subsystemC;

    public Facade() {
        subsystemA = new SubsystemA();
        subsystemB = new SubsystemB();
        subsystemC = new SubsystemC();
    }

    public void operation() {
        subsystemA.operationA();
        subsystemB.operationB();
        subsystemC.operationC();
    }
}

22. Clarify the Proxy Design Sample.

Reply:

The Proxy sample offers a surrogate or placeholder for one more object to regulate entry to it. It’s used so as to add a stage of management over accessing a useful resource.

Instance:

interface Picture {
    void show();
}

class RealImage implements Picture {
    non-public String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    public void show() {
        System.out.println("Displaying " + fileName);
    }

    non-public void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }
}

class ProxyImage implements Picture {
    non-public RealImage realImage;
    non-public String fileName;

    public ProxyImage(String fileName){
        this.fileName = fileName;
    }

    public void show() {
        if(realImage == null){
            realImage = new RealImage(fileName);
        }
        realImage.show();
    }
}

23. What’s the Adapter Design Sample?

Reply:

The Adapter sample permits the interface of an current class for use as one other interface. It’s typically used to make current courses work with others with out modifying their supply code.

Instance:

interface MediaPlayer {
    void play(String audioType, String fileName);
}

class AudioPlayer implements MediaPlayer {
    public void play(String audioType, String fileName) {
        // Implement playback logic right here
    }
}

interface AdvancedMediaPlayer {
    void playVlc(String fileName);
    void playMp4(String fileName);
}

class VlcPlayer implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Implement Vlc playback logic right here
    }

    public void playMp4(String fileName) {
        // Do nothing
    }
}

class Mp4Player implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Do nothing
    }

    public void playMp4(String fileName) {
        // Implement Mp4 playback logic right here
    }
}

24. Clarify the Chain of Duty Design Sample.

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

25. Clarify the Observer Design Sample.

Reply:

The Observer sample defines a one-to-many dependency between objects. When one object modifications state, all its dependents are notified and up to date mechanically.

Instance:

import java.util.ArrayList;
import java.util.Checklist;

interface Observer {
    void replace(String message);
}

class ConcreteObserver implements Observer {
    non-public String title;

    public ConcreteObserver(String title) {
        this.title = title;
    }

    public void replace(String message) {
        System.out.println(title + " acquired message: " + message);
    }
}

class Topic {
    non-public Checklist<Observer> observers = new ArrayList<>();
    non-public String message;

    // ... addObserver, removeObserver, setMessage strategies ...
}

26. What’s the Technique Design Sample?

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

27. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

28. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

29. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

30. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

31. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

32. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

33. Clarify the Composite Design Sample.

Reply:

The Composite sample lets purchasers deal with particular person objects and compositions of objects uniformly. It composes objects into tree constructions to signify part-whole hierarchies.

Instance:

interface Part {
    void operation();
}

class Leaf implements Part {
    public void operation() {
        System.out.println("Leaf operation");
    }
}

class Composite implements Part {
    non-public Checklist<Part> parts = new ArrayList<>();

    public void addComponent(Part part) {
        parts.add(part);
    }

    public void removeComponent(Part part) {
        parts.take away(part);
    }

    public void operation() {
        System.out.println("Composite operation");
        for (Part part : parts) {
            part.operation();
        }
    }
}

34. What’s the Flyweight Design Sample?

Reply:

The Flyweight sample minimizes reminiscence utilization or computational bills by sharing as a lot as doable with associated objects. It’s used for efficiency optimization.

Instance:

class Flyweight {
    non-public String information;

    public Flyweight(String information) {
        this.information = information;
    }

    public String getData() {
        return information;
    }
}

35. Clarify the Proxy Design Sample.

Reply:

The Proxy sample offers a surrogate or placeholder for one more object to regulate entry to it. It’s used so as to add a stage of management over accessing a useful resource.

Instance:

interface Picture {
    void show();
}

class RealImage implements Picture {
    non-public String fileName;

    public RealImage(String fileName) {
        this.fileName = fileName;
        loadFromDisk(fileName);
    }

    public void show() {
        System.out.println("Displaying " + fileName);
    }

    non-public void loadFromDisk(String fileName) {
        System.out.println("Loading " + fileName);
    }
}

class ProxyImage implements Picture {
    non-public RealImage realImage;
    non-public String fileName;

    public ProxyImage(String fileName){
        this.fileName = fileName;
    }

    public void show() {
        if(realImage == null){
            realImage = new RealImage(fileName);
        }
        realImage.show();
    }
}

36. What’s the Adapter Design Sample?

Reply:

The Adapter sample permits the interface of an current class for use as one other interface. It’s typically used to make current courses work with others with out modifying their supply code.

Instance:

interface MediaPlayer {
    void play(String audioType, String fileName);
}

class AudioPlayer implements MediaPlayer {
    public void play(String audioType, String fileName) {
        // Implement playback logic right here
    }
}

interface AdvancedMediaPlayer {
    void playVlc(String fileName);
    void playMp4(String fileName);
}

class VlcPlayer implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Implement Vlc playback logic right here
    }

    public void playMp4(String fileName) {
        // Do nothing
    }
}

class Mp4Player implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // Do nothing
    }

    public void playMp4(String fileName) {
        // Implement Mp4 playback logic right here
    }
}

37. Clarify the Bridge Design Sample.

Reply:

The Bridge sample separates abstraction from implementation in order that they will fluctuate independently. It’s helpful when each should be prolonged independently.

Instance:

interface DrawAPI {
    void drawCircle(int radius, int x, int y);
}

class RedCircle implements DrawAPI {
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }
}

class GreenCircle implements DrawAPI {
    public void drawCircle(int radius, int x, int y) {
        System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", y: " + y + "]");
    }
}

38. What’s the Composite Design Sample?

Reply:

The Composite sample lets purchasers deal with particular person objects and compositions of objects uniformly. It composes objects into tree constructions to signify part-whole hierarchies.

Instance:

interface Part {
    void operation();
}

class Leaf implements Part {
    public void operation() {
        System.out.println("Leaf operation");
    }
}

class Composite implements Part {
    non-public Checklist<Part> parts = new ArrayList<>();

    public void addComponent(Part part) {
        parts.add(part);
    }

    public void removeComponent(Part part) {
        parts.take away(part);
    }

    public void operation() {
        System.out.println("Composite operation");
        for (Part part : parts) {
            part.operation();
        }
    }
}

39. What’s the Facade Design Sample?

Reply:

The Facade sample offers a simplified interface to a set of interfaces in a subsystem, making it simpler to make use of.

Instance:

class SubsystemA {
    public void operationA() {
        System.out.println("Subsystem A - Operation A");
    }
}

class SubsystemB {
    public void operationB() {
        System.out.println("Subsystem B - Operation B");
    }
}

class SubsystemC {
    public void operationC() {
        System.out.println("Subsystem C - Operation C");
    }
}

class Facade {
    non-public SubsystemA subsystemA;
    non-public SubsystemB subsystemB;
    non-public SubsystemC subsystemC;

    public Facade() {
        subsystemA = new SubsystemA();
        subsystemB = new SubsystemB();
        subsystemC = new SubsystemC();
    }

    public void operation() {
        subsystemA.operationA();
        subsystemB.operationB();
        subsystemC.operationC();
    }
}

40. Clarify the Observer Design Sample.

Reply:

The Observer sample defines a one-to-many dependency between objects. When one object modifications state, all its dependents are notified and up to date mechanically.

Instance:

import java.util.ArrayList;
import java.util.Checklist;

interface Observer {
    void replace(String message);
}

class ConcreteObserver implements Observer {
    non-public String title;

    public ConcreteObserver(String title) {
        this.title = title;
    }

    public void replace(String message) {
        System.out.println(title + " acquired message: " + message);
    }
}

class Topic {
    non-public Checklist<Observer> observers = new ArrayList<>();
    non-public String message;

    // ... addObserver, removeObserver, setMessage strategies ...
}

41. What’s the Technique Design Sample?

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

42. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

43. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

44. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

45. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

46. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

47. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

48. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

49. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

50. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

51. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

52. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

53. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

54. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

55. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

56. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

57. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

58. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

59. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

60. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

61. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

62. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

63. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

64. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

65. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

66. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

67. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

68. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

69. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

70. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

71. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

72. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

73. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

74. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

75. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

76. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

77. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

78. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

79. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

80. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

81. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

82. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

83. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

84. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

85. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

86. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

87. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

88. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

89. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

90. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

91. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

92. What’s the Interpreter Design Sample?

Reply:

The Interpreter sample defines a grammar for the language and offers an interpreter to interpret sentences of the language.

Instance:

interface Expression {
    boolean interpret(String context);
}

class TerminalExpression implements Expression {
    non-public String information;

    public TerminalExpression(String information) {
        this.information = information;
    }

    public boolean interpret(String context) {
        return context.incorporates(information);
    }
}

class OrExpression implements Expression {
    non-public Expression expr1;
    non-public Expression expr2;

    public OrExpression(Expression expr1, Expression expr2) {
        this.expr1 = expr1;
        this.expr2 = expr2;
    }

    public boolean interpret(String context)  expr2.interpret(context);
    
}

93. Clarify the Memento Design Sample.

Reply:

The Memento sample offers the flexibility to revive an object to its earlier state. It’s used to implement an undo mechanism.

Instance:

class Memento {
    non-public String state;

    public Memento(String state) {
        this.state = state;
    }

    public String getState() {
        return state;
    }
}

class Originator {
    non-public String state;

    public void setState(String state) {
        this.state = state;
    }

    public Memento saveStateToMemento() {
        return new Memento(state);
    }

    public void getStateFromMemento(Memento memento) {
        state = memento.getState();
    }
}

94. What’s the Customer Design Sample?

Reply:

The Customer sample represents an operation to be carried out on the weather of an object construction. It helps you to outline a brand new operation with out altering the courses of the weather on which it operates.

Instance:

interface Customer {
    void go to(ElementA elementA);
    void go to(ElementB elementB);
}

interface Ingredient {
    void settle for(Customer customer);
}

class ElementA implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ElementB implements Ingredient {
    public void settle for(Customer customer) {
        customer.go to(this);
    }
}

class ConcreteVisitor implements Customer {
    public void go to(ElementA elementA) {
        System.out.println("Customer visited ElementA");
    }

    public void go to(ElementB elementB) {
        System.out.println("Customer visited ElementB");
    }
}

95. Clarify the Technique Design Sample.

Reply:

The Technique sample defines a household of algorithms, encapsulates each, and makes them interchangeable. It lets the algorithm fluctuate independently from purchasers that use it.

Instance:

interface PaymentStrategy {
    void pay(int quantity);
}

class CreditCardPayment implements PaymentStrategy {
    non-public String cardNumber;

    // ... constructor, getters, and setters ...
}

class PayPalPayment implements PaymentStrategy {
    non-public String e mail;

    // ... constructor, getters, and setters ...
}

class ShoppingCart {
    non-public PaymentStrategy paymentStrategy;

    // ... setPaymentStrategy and checkout strategies ...
}

96. Clarify the Decorator Design Sample.

Reply:

The Decorator sample attaches extra duties to an object dynamically. Decorators present a versatile different to subclassing for extending performance.

Instance:

interface Espresso {
    String getDescription();
    double getCost();
}

class SimpleCoffee implements Espresso {
    public String getDescription() {
        return "Easy Espresso";
    }

    public double getCost() {
        return 2.0;
    }
}

// ... CoffeeDecorator, MilkDecorator, and so on. ...

97. What’s the Command Design Sample?

Reply:

The Command sample encapsulates a request as an object, thereby parameterizing purchasers with queues, requests, and operations. It permits for the help of undoable operations.

Instance:

interface Command {
    void execute();
}

class ConcreteCommand implements Command {
    non-public Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.motion();
    }
}

class Receiver {
    public void motion() {
        System.out.println("Receiver motion executed.");
    }
}

98. Clarify the State Design Sample.

Reply:

The State sample permits an object to change its habits when its inside state modifications. The thing will seem to alter its class.

Instance:

interface State {
    void doAction(Context context);
}

class StartState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in begin state");
        context.setState(this);
    }

    public String toString() {
        return "Begin State";
    }
}

class StopState implements State {
    public void doAction(Context context) {
        System.out.println("Participant is in cease state");
        context.setState(this);
    }

    public String toString() {
        return "Cease State";
    }
}

class Context {
    non-public State state;

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

99. What’s the Chain of Duty Design Sample?

Reply:

The Chain of Duty sample passes a request alongside a series of handlers. Upon receiving a request, every handler decides both to course of it or to move it alongside.

Instance:

summary class Logger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;

    protected int stage;

    protected Logger nextLogger;

    public void setNextLogger(Logger nextLogger){
        this.nextLogger = nextLogger;
    }

    summary protected void write(String message);
}

class ConsoleLogger extends Logger {
    public ConsoleLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("Console Logger: " + message);
    }
}

class FileLogger extends Logger {
    public FileLogger(int stage){
        this.stage = stage;
    }

    protected void write(String message) {
        System.out.println("File Logger: " + message);
    }
}

100. Clarify the Template Technique Design Sample.

Reply:

The Template Technique sample defines the construction of an algorithm within the superclass however lets subclasses override particular steps of the algorithm with out altering its construction.

Instance:

summary class Sport {
    summary void initialize();
    summary void startPlay();
    summary void endPlay();

    public ultimate void play(){
        initialize();
        startPlay();
        endPlay();
    }
}

class Cricket extends Sport {
    void initialize() {
        System.out.println("Cricket Sport Initialized! Begin enjoying.");
    }

    void startPlay() {
        System.out.println("Cricket Sport Began. Benefit from the sport!");
    }

    void endPlay() {
        System.out.println("Cricket Sport Completed!");
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments