0% found this document useful (0 votes)
14 views

Design Pattern - Part 2

The document discusses 5 different design patterns: 1. Interpreter pattern - Defines a representation for a language grammar and an interpreter that uses this to interpret sentences in the language. 2. Command pattern - Encapsulates a request as an object to parameterize clients with different requests and support undoable operations. 3. Bridge pattern - Splits a class and its implementation into two hierarchies that can be developed independently. 4. Decorator pattern - Attaches additional behaviors to objects by placing them inside wrapper objects that contain these behaviors. 5. Proxy pattern - Provides a surrogate or placeholder for another object for the purpose of controlling access.

Uploaded by

indra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Design Pattern - Part 2

The document discusses 5 different design patterns: 1. Interpreter pattern - Defines a representation for a language grammar and an interpreter that uses this to interpret sentences in the language. 2. Command pattern - Encapsulates a request as an object to parameterize clients with different requests and support undoable operations. 3. Bridge pattern - Splits a class and its implementation into two hierarchies that can be developed independently. 4. Decorator pattern - Attaches additional behaviors to objects by placing them inside wrapper objects that contain these behaviors. 5. Proxy pattern - Provides a surrogate or placeholder for another object for the purpose of controlling access.

Uploaded by

indra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

11) Interpreter

12) Command
13) Bridge
14) Decorator
15) Proxy
11) Interpreter
“Given a language, define a representation for its grammar along with an interpreter that uses the
representation to interpret sentences in the language”.

public interface Expression {


public boolean interpret(String context);
}

public class TerminalExpression implements Expression {


private String data;
public TerminalExpression(String data){
this.data = data;
}
@Override
public boolean interpret(String context) {

if(context.contains(data)){
return true;
}
return false;
}
}

public class AndExpression implements Expression {


private Expression expr1 = null;
private Expression expr2 = null;
public AndExpression (Expression expr1, Expression expr2) {
this.expr1 = expr1;
this.expr2 = expr2;
}
@Override
public boolean interpret(String context) {
return expr1.interpret(context) && expr2.interpret(context);
}
}

import java.util.Scanner;

public class Client {


public static void main(String[] args){
Expression jack = new TerminalExpression("jack");
Expression roseWedding = new TerminalExpression("weds rose");
Scanner inp = new Scanner(System.in);

System.out.println("Enter rose wedding info:");


if(new AndExpression(jack,roseWedding).interpret(inp.nextLine()))
System.out.println("Hey..! Jack and Rose are get united...!");
}
}

OUTPUT: (on running Client)

12)Command:
“Encapsulate a request as an object, thereby letting you parameterize clients with different requests,
queue or log requests, and support undoable operations”.

public interface Operations {


public void execute();
}

public class Command1 implements Operations{


Receiver r;
Command1(Receiver r){
this.r = r;
}
@Override
public void execute() {
r.targetedMethod1();
}
}
public class Receiver {
String name;
Receiver(String name){
this.name = name;
}
public void targetedMethod1(){
System.out.println("Performing targeted Operation on "+name);
}
}

import java.util.ArrayList;
import java.util.List;

public class Invoker {


List<Operations> op = new ArrayList<>();
public void addCommands(Operations o){
op.add(o);
}
public void startExecution(){
for(Operations o : op){
o.execute();
}
}
}

public class Client {


public static void main(String[] args){
Receiver r = new Receiver("Our Receiver");
Command1 c1 = new Command1(r);
Invoker i = new Invoker();
i.addCommands(c1);
i.startExecution();
}
}

OUTPUT: (on execu ng client)

13) Bridge:
“Bridge is a structural design pa ern that lets you split a large class or a set of closely related classes
into two separate hierarchies—abstrac on and implementa on—which can be developed
independently of each other.”
public interface Color {
public void drawColor(String shape);
}

public class Gold implements Color{


@Override
public void drawColor(String shape) {
System.out.println("\n\nDrawing "+shape+" in Gold color");
}
}

public class Green implements Color{


@Override
public void drawColor(String shape) {
System.out.println("\n\nDrawing "+shape+" in Green color");
}
}

public interface Shape {


public void printInfo();
}

public class Circle implements Shape{


int circum;
Color c;
Circle(int circum,Color c){
this.circum = circum; this.c=c;
}
@Override
public void printInfo() {
c.drawColor("Circle");
System.out.print("and with "+circum+" units. of Circumference");
}
}

public class Square implements Shape{


int peri;
Color c;
Square(int peri,Color c){
this.peri = peri; this.c=c;
}
@Override
public void printInfo() {
c.drawColor("Square");
System.out.print("and with "+peri+" units. of Perimeter");
}
}

public class Client {


public static void main(String[] args){
Shape s1 = new Circle(50,new Green());
Shape s2 = new Square(60,new Gold());
s1.printInfo();s2.printInfo();
}
}
14)Decorator
“Decorator is a structural design pa ern that lets you a ach new behaviors to objects by placing
these objects inside special wrapper objects that contain the behaviors.”

public interface Dosa {


public String makeDosa();
}

public class OnionDosa implements Dosa{


@Override
public String makeDosa() {
return "Plain Dosa Ready";
}
}
public class RavaDosa implements Dosa{
@Override
public String makeDosa() {
return "Rava Dosa Ready";
}
}

public class PlainRava extends RavaDosa{


RavaDosa r;
PlainRava(RavaDosa r){
this.r = r;
}
@Override
public String makeDosa() {
return r.makeDosa();
}
}

public class OnionRava extends RavaDosa{


RavaDosa r;
OnionRava(RavaDosa r){
this.r = r;
}
@Override
public String makeDosa() {
return r.makeDosa()+" with Onion";
}
}

public class GheeRava extends RavaDosa{


RavaDosa r;
GheeRava(RavaDosa r){
this.r = r;
}
@Override
public String makeDosa() {
return r.makeDosa()+" with Ghee";
}
}

public class Customer {


public static void main(String args[]){
OnionDosa d1 = new OnionDosa();
System.out.println(d1.makeDosa());
RavaDosa d2 = new GheeRava(new RavaDosa());
System.out.println(d2.makeDosa());
}
}

OUTPUT: (On running Customer)


15) h ps://www.tutorialspoint.com/design_pa ern/proxy_pa ern.htm

You might also like