Pandit Deendayal Energy University School of Technology: 21BCP430D Pranjal M. Patel
Pandit Deendayal Energy University School of Technology: 21BCP430D Pranjal M. Patel
Patel
Pranjal M. Patel
21BCP430D
G2 batch
21BCP430D Pranjal M. Patel
1. Adapter Pattern:
Introduction:
Adapter pattern works as a bridge between two incompatible interfaces. This
type of design pattern comes under structural pattern as this pattern combines
the capability of two independent interfaces.
This pattern involves a single class which is responsible to join functionalities
of independent or incompatible interfaces. A real life example could be a case
of card reader which acts as an adapter between memory card and a laptop.
You plugin the memory card into card reader and card reader into the laptop
so that memory card can be read via laptop.
Diagram:
21BCP430D Pranjal M. Patel
EXAMPLE-1:
Code:
mediaplayer.java
package Adapter1;
AdvancedMediaPlayer.java
package Adapter1;
VlcPlayer.java
package Adapter1;
@Override
public void playMp4(String fileName)
{
//do nothing
}
}
21BCP430D Pranjal M. Patel
Mp4Player.java
package Adapter1;
@Override
public void playVlc(String fileName)
{
//do nothing
}
@Override
public void playMp4(String fileName)
{
System.out.println("Playing mp4 file. Name: "+ fileName);
}
}
MediaAdapter.java
package Adapter1;
AdvancedMediaPlayer advancedMusicPlayer;
if(audioType.equalsIgnoreCase("vlc") ){
advancedMusicPlayer = new VlcPlayer();
}else if (audioType.equalsIgnoreCase("mp4")){
advancedMusicPlayer = new Mp4Player();
}
}
@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
}
else if(audioType.equalsIgnoreCase("mp4")){
21BCP430D Pranjal M. Patel
advancedMusicPlayer.playMp4(fileName);
}
}
}
AudioPlayer.java
package Adapter1;
MediaAdapter mediaAdapter;
@Override
if(audioType.equalsIgnoreCase("mp3")){
else if(audioType.equalsIgnoreCase("vlc") ||
audioType.equalsIgnoreCase("mp4")){
mediaAdapter.play(audioType, fileName);
else{
21BCP430D Pranjal M. Patel
AdapterPatternDemo.java
package Adapter1;
audioPlayer.play("mp4", "HeartLess.mp4");
audioPlayer.play("vlc", "Disaster.vlc");
audioPlayer.play("avi", "Lonely.avi");
Output:
21BCP430D Pranjal M. Patel
EXAMPLE-2:
Code:
ImageViewer.java
package Adapter2;
AdvancedImageViewer.java
package Adapter2;
PngShower.java
package Adapter2;
@Override
public void showJpg(String fileName)
{
//do nothing in jpeg format
}
}
21BCP430D Pranjal M. Patel
JpgShower.java
package Adapter2;
@Override
public void showPng(String fileName)
{
//do nothing in png format
}
@Override
public void showJpg(String fileName)
{
System.out.println("It is showing 'jpg' file. File Name: "+ fileName);
}
}
ImageAdapter.java
package Adapter2;
AdvancedImageViewer advancedImageViewer;
if(imageFormat.equalsIgnoreCase("png") )
{
advancedImageViewer = new PngShower();
}
else if (imageFormat.equalsIgnoreCase("jpg"))
{
advancedImageViewer = new JpgShower();
}
}
@Override
public void show(String imageFormat, String fileName)
21BCP430D Pranjal M. Patel
if(imageFormat.equalsIgnoreCase("png"))
{
advancedImageViewer.showPng(fileName);
}
else if(imageFormat.equalsIgnoreCase("jpg"))
{
advancedImageViewer.showJpg(fileName);
}
}
}
GalleryApp.java
package Adapter2;
ImageAdapter imageAdapter;
@Override
public void show(String imageFormat, String fileName)
{
if(imageFormat.equalsIgnoreCase("jpeg"))
{
System.out.println("Showing jpeg file. Name: " + fileName);
}
//imageAdapter is providing support to show other file formats
else if(imageFormat.equalsIgnoreCase("png") ||
imageFormat.equalsIgnoreCase("jpg"))
{
imageAdapter = new ImageAdapter(imageFormat);
imageAdapter.show(imageFormat, fileName);
}
else
{
System.out.println("Invalid image format. " + imageFormat + "
format is not supported");
}
}
21BCP430D Pranjal M. Patel
AdapterPatternDemo.java
package Adapter2;
gallery.show("jpeg", "Yagni.jpeg");
gallery.show("png", "Kashish.png");
gallery.show("jpeg", "Pranjal.jpeg");
gallery.show("tiff", "Vrushi.tiff");
}
}
Output:
21BCP430D Pranjal M. Patel
2. Composite Pattern:
Introduction:
Composite pattern is used where we need to treat a group of objects in similar
way as a single object. Composite pattern composes objects in term of a tree
structure to represent part as well as whole hierarchy. This type of design
pattern comes under structural pattern as this pattern creates a tree structure of
group of objects.
This pattern creates a class that contains group of its own objects. This class
provides ways to modify its group of same objects.
Diagram:
21BCP430D Pranjal M. Patel
EXAMPLE-1:
Code:
ComputerPart.java
package sdp1;
//import java.util.List;
import java.util.*;
interface component
{
void showprice();
}
class leaf implements component
{
int price;
String name;
@Override
public void showprice() {
System.out.println(name);
System.out.println(price);
}
}
CompositeTest.java
package sdp1;
mb.addcomponent(cpu);
mb.addcomponent(ram);
ph.addcomponent(mouse);
21BCP430D Pranjal M. Patel
ph.addcomponent(monitor);
cabinet.addcomponent(hd);
cabinet.addcomponent(mb);
computer.addcomponent(ph);
computer.addcomponent(cabinet);
ram.showprice();
mb.showprice();
computer.showprice();
Output:
21BCP430D Pranjal M. Patel
EXAMPLE-2:
Code:
Employee.java
package sdp2;
import java.util.ArrayList;
import java.util.List;
// constructor
public Employee(String name,String dept, int sal) {
this.name = name;
this.dept = dept;
this.salary = sal;
subordinates = new ArrayList<Employee>();
}
return ("Employee :[ Name : " + name + ", dept : " + dept + ",
salary :" + salary+" ]");
}
}
CompositePatternDemo.java
package sdp2;
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
21BCP430D Pranjal M. Patel
Output:
21BCP430D Pranjal M. Patel
3. Decorator Pattern:
Introduction:
Decorator pattern allows a user to add new functionality to an existing object
without altering its structure. This type of design pattern comes under structural
pattern as this pattern acts as a wrapper to existing class.
This pattern creates a decorator class which wraps the original class and
provides additional functionality keeping class methods signature intact.
Diagram:
21BCP430D Pranjal M. Patel
EXAMPLE-1:
Code:
Shape.java
package Decorator1;
Rctangle.java
package Decorator1;
@Override
public void draw()
{
System.out.println("Shape: Rectangle");
}
}
Circle.java
package Decorator1;
@Override
public void draw()
{
System.out.println("Shape: Circle");
}
}
21BCP430D Pranjal M. Patel
ShapeDecorator.java
package Decorator1;
RedShapeDecorator.java
package Decorator1;
@Override
public void draw()
{
decoratedShape.draw();
setRedBorder(decoratedShape);
}
{
System.out.println("Border Color: Red");
}
}
DecoratorPatternDemo.java
package Decorator1;
Output:
21BCP430D Pranjal M. Patel
EXAMPLE-2:
Code:
Sandwich.java
package Decorator2;
WhiteBreadSandWich.java
package Decorator2;
import java.math.BigDecimal;
@Override
public BigDecimal price()
{
21BCP430D Pranjal M. Patel
SandWichDecorator.java
package Decorator2;
import java.math.BigDecimal;
CheeseDecorator.java
package Decorator2;
import java.math.BigDecimal;
@Override
public String getDescription()
{
return currentSandwich.getDescription() + ", Cheese";
}
@Override
public BigDecimal price()
{
21BCP430D Pranjal M. Patel
return currentSandwich.price().add(new
BigDecimal("1.50"));
}
}
SandwichMaker.java
package Decorator2;
Output:
21BCP430D Pranjal M. Patel
4. Facade Pattern:
Introduction:
Facade pattern hides the complexities of the system and provides an interface
to the client using which the client can access the system. This type of design
pattern comes under structural pattern as this pattern adds an interface to
existing system to hide its complexities.
This pattern involves a single class which provides simplified methods
required by client and delegates calls to methods of existing system classes.
Diagram:
21BCP430D Pranjal M. Patel
EXAMPLE-1:
Code:
Shape.java
package facade1;
Rectangle.java
package facade1;
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
Square.java
package facade1;
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
21BCP430D Pranjal M. Patel
Circle.java
package facade1;
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
ShapeMaker.java
package facade1;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
FacadePatternDemo.java
package facade1;
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
Output:
21BCP430D Pranjal M. Patel
EXAMPLE-2:
Code:
Food.java
package facade;
Pizza.java
package facade;
public class Pizza implements Food{
public String prepareItem;
public void prepareFood(String itemRequired)
{
prepareItem = "This pizza contains: " + itemRequired;
}
public String deliverFood() {
return prepareItem;
}
}
21BCP430D Pranjal M. Patel
Pasta.java
package facade;
public class Pasta implements Food
{
public String prepareItem;
public void prepareFood(String itemRequired)
{
prepareItem = "This pasta contains: " + itemRequired;
}
public String deliverFood()
{
return prepareItem;
}
}
Waiter.java
package facade;
public class Waiter {
public static String deliverFood (String foodType){
Ingredients ingredients = new Ingredients();
switch(foodType)
{
case "pizza":
Food pizza = new Pizza();
21BCP430D Pranjal M. Patel
FoodType.java
package facade;
Customer.java
package facade;
class Customer
{
public static void main(String args[])
{
Ingredients ingredients = new Ingredients();
//without facade
System.out.println("------Without Facade-------");
Food pizza = new Pizza();
pizza.prepareFood(ingredients.getPizzaItem());
System.out.println(pizza.deliverFood());
System.out.println("------With Facade-------");
System.out.println(Waiter.deliverFood(FoodType.PASTA));
System.out.println(Waiter.deliverFood(FoodType.PIZZA));
}
}
Output:
21BCP430D Pranjal M. Patel
5. Flyweight Pattern:
Introduction:
Flyweight pattern is primarily used to reduce the number of objects created and
to decrease memory footprint and increase performance. This type of design
pattern comes under structural pattern as this pattern provides ways to decrease
object count thus improving the object structure of application.
Flyweight pattern tries to reuse already existing similar kind objects by storing
them and creates new object when no matching object is found.
Diagram:
21BCP430D Pranjal M. Patel
EXAMPLE-1:
Code:
Shape.java
package Fp1;
Circle.java
package Fp1;
@Override
public void draw() {
System.out.println("Circle: Draw() [Color : " + color + ", x :
" + x + ", y :" + y + ", radius :" + radius);
}
}
ShapeFactory.java
package Fp1;
import java.util.HashMap;
if(circle == null)
{
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("Creating circle of color : " + color);
}
return circle;
}
}
21BCP430D Pranjal M. Patel
FlyweightPatternDemo.java
package Fp1;
Output:
21BCP430D Pranjal M. Patel
EXAMPLE-2:
Code:
Pen.java
package Fp2;
BrushSize.java
package Fp2;
ThickPen.java
package Fp2;
@Override
public void draw(String content) {
21BCP430D Pranjal M. Patel
ThinPen.java
package Fp2;
@Override
public void draw(String content) {
System.out.println("Drawing THIN content in color : " +
color);
}
}
MediumPen.java
package Fp2;
@Override
public void draw(String content) {
21BCP430D Pranjal M. Patel
PenFactory.java
package Fp2;
import java.util.HashMap;
if(pen != null) {
return pen;
} else {
pen = new ThickPen();
pen.setColor(color);
pensMap.put(key, pen);
}
return pen;
}
if(pen != null) {
return pen;
} else {
pen = new ThinPen();
pen.setColor(color);
pensMap.put(key, pen);
}
return pen;
}
if(pen != null) {
return pen;
} else {
pen = new MediumPen();
pen.setColor(color);
pensMap.put(key, pen);
}
return pen;
}
}
PaintBrushClient.java
package Fp2;
//System.out.println(yellowThinPen1.hashCode());
//System.out.println(yellowThinPen2.hashCode());
//System.out.println(blueThinPen.hashCode());
}
}
Output: