Structural Pattern

Flyweigt Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 28, 2019
*/

public interface Shape {

    public void draw();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 28, 2019
*/

public class Circle implements Shape {
    private String name = null;
    private int xcoor = 0;
    private int ycoor = 0;

    public Circle(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getXcoor() {
        return xcoor;
    }

    public void setXcoor(int xcoor) {
        this.xcoor = xcoor;
    }

    public int getYcoor() {
        return ycoor;
    }

    public void setYCoor(int ycoor) {
        this.ycoor = ycoor;
    }

    public void draw() {
        System.out.println("Drawing a Circle having " + this.name + " Color at X: " + this.xcoor + " Y :" + this.ycoor
                + " Coordinates");
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.HashMap;

/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 28, 2019
*/

public class ShapeFactory {

    private static final HashMap<String, Shape> shapeMap = new HashMap<>();

    public static Shape getShape(String color) {

        Circle circle = (Circle) shapeMap.get(color);

        if (circle == null) {
            circle = new Circle(color);
            shapeMap.put(color, circle);
            System.out.println("Creating Circle of Color : " + color);
        }
        return circle;

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.Random;

/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 28, 2019
*/

public class FlyweightPatternDemo {

    public static final String color[] = { "Red", "Green", "Blue", "White", "Black" };
    static Random rand = new Random();

    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {

            Circle circle = (Circle) ShapeFactory.getShape(getColor());
            circle.setXcoor(getRandX());
            circle.setYCoor(getRandY());
            circle.draw();
        }

    }

    private static String getColor() {

        return color[rand.nextInt(color.length)];
    }

    private static int getRandX() {

        return rand.nextInt(100);
    }

    private static int getRandY() {

        return rand.nextInt(100);
    }

}

Flyweigt Pattern Example


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 29, 2019

*/

package LabTask;

import java.util.HashMap;

public class AlphabetGenerator {

    private static final HashMap<Character, Character> charmap = new HashMap<>();

    public static char getChar(char ch) {

        Character retch = charmap.get(ch);

        if (retch == null) {

            retch = ch;
            charmap.put(ch, ch);

            System.out.println("Char '" + ch + "' is added to the Hashmap");
        } else 
            System.out.println(retch);

        return retch;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Mar 29, 2019
* Task Description: Consider a Word text editor where a user can type alphabet and takes one byte in momory . 
                    Implemet flyweight patter to use memory efficeiently for storage of characters. 
                    Alphabets are A,B,C,D,E
*/

package LabTask;

import java.util.Random;

public class FlyweightDemo {

    private static char alpha[] = { 'A', 'B', 'C', 'D', 'E' };

    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) 
            AlphabetGenerator.getChar( getRandomAlpha() );


    }

    private static char getRandomAlpha() {

        Random rand = new Random();
        return alpha[rand.nextInt(5)];

    }

}

Facade Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package facadePattern;

public class HomeTheater {

    private Amplifier amp;
    private DVDPlayer dvdPlayer;
    private Light light;
    private Popcorn popcorn;
    private Projecter projecter;

    public HomeTheater() {

        this.amp = new Amplifier();
        this.dvdPlayer = new DVDPlayer();
        this.light = new Light();
        this.popcorn = new Popcorn();
        this.projecter = new Projecter();

    }

    public void watchMovie() {

        popcorn.load();
        popcorn.on();

        projecter.lower();

        amp.on();
        amp.medium();
        amp.inputDVD();

        dvdPlayer.on();
        dvdPlayer.load();
        dvdPlayer.sorround();

        projecter.on();
        projecter.inputDVD();
        projecter.wideScreen();

        light.dim();

        dvdPlayer.start();

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package facadePattern;

public class Amplifier {

    public void on() {
        // TODO Auto-generated method stub

    }



    public void inputDVD() {
        // TODO Auto-generated method stub

    }

    public void medium() {
        // TODO Auto-generated method stub

    }



}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package facadePattern;

public class DVDPlayer {


    public void load() {
        // TODO Auto-generated method stub

    }

    public void sorround() {
        // TODO Auto-generated method stub

    }

    public void start() {
        // TODO Auto-generated method stub

    }

    public void on() {
        // TODO Auto-generated method stub

    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package facadePattern;

public class Light {

    public void dim() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package facadePattern;

public class Popcorn {

    public void load() {
        System.out.println("Popcorn machine is loading.");

    }

    public void on() {
        System.out.println("");

    }



}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package facadePattern;

public class Projecter {

    public void lower() {
        // TODO Auto-generated method stub

    }

    public void on() {
        // TODO Auto-generated method stub

    }

    public void load() {
        // TODO Auto-generated method stub

    }

    public void inputDVD() {
        // TODO Auto-generated method stub

    }

    public void wideScreen() {
        // TODO Auto-generated method stub

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package facadePattern;

public class FacadeDemo {

    public static void main(String[] args) {

        HomeTheater theater = new HomeTheater();
        theater.watchMovie();   
    }

}

Decorator Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public abstract class Beverage {

    String description = "Unknown Description";

    public String getDescription() {

        return description;

    }

    public abstract double cost();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public abstract class CondimentDecorator extends Beverage {

    public abstract String getDescription();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class DarkRoast extends Beverage {

    public DarkRoast() {
        description = this.getClass().getName() +  " Coffe";
    }

    @Override
    public double cost() {

        return 3.0;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class Decaf extends Beverage{

    public Decaf() {
        description = this.getClass().getName() +  " Coffe";
    }

    @Override
    public double cost() {

        return 4.0;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class Espresso extends Beverage{

    public Espresso() {
        description = this.getClass().getName() +  " Coffe";
    }

    @Override
    public double cost() {

        return 6.0;
    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class HouseBlend extends Beverage {

    public HouseBlend() {
        description = this.getClass().getName() +  " Coffe";
    }

    @Override
    public double cost() {

        return 2.0;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class Mocha extends CondimentDecorator {

    Beverage bev;
    public Mocha(Beverage bev) {
        this.bev = bev;

    }


    @Override
    public String getDescription() {

        return bev.getDescription() + " + " + this.getClass().getName() ;
    }

    @Override
    public double cost() {

        return 0.2 + bev.cost() ;
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class Soy extends CondimentDecorator{


    Beverage bev;
    public Soy(Beverage bev) {
        this.bev = bev;

    }


    @Override
    public String getDescription() {

        return bev.getDescription() + " + " + this.getClass().getName() ;
    }

    @Override
    public double cost() {

        return 0.4 + bev.cost() ;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public abstract class Beverage {

    String description = "Unknown Description";

    public String getDescription() {

        return description;

    }

    public abstract double cost();

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class SteamedMilk extends CondimentDecorator {

    Beverage bev;

    public SteamedMilk(Beverage bev) {
        this.bev = bev;

    }

    @Override
    public String getDescription() {

        return bev.getDescription() + " + " + this.getClass().getName();
    }

    @Override
    public double cost() {

        return 0.6 + bev.cost();
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class WhippedMilk extends CondimentDecorator{

    Beverage bev;

    public WhippedMilk(Beverage bev) {
        this.bev = bev;

    }

    @Override
    public String getDescription() {

        return bev.getDescription() + " + " + this.getClass().getName();
    }

    @Override
    public double cost() {

        return 0.9 + bev.cost();
    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 8, 2019
*/

public class MainStarBuzz {
    public static void main(String[] args) {

        HouseBlend obj = new HouseBlend();
        Mocha moc = new Mocha(obj);

        System.out.println(obj.getDescription());
        System.out.println(obj.cost());

        System.out.println(moc.getDescription());
        System.out.println(moc.cost());

        Beverage coffe = new DarkRoast();
        coffe = new Mocha(coffe);
        coffe = new Soy(coffe);

        System.out.println(coffe.getDescription());
        System.out.println(coffe.cost());






    }
}

Composite Pattern


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 24, 2019
*/

public abstract class Employee {

    // Composite Method Specifically

    public void add(Employee employee) {
        throw new UnsupportedOperationException();

    }

    public void remove(Employee employee) {
        throw new UnsupportedOperationException();

    }

    public Employee getChild(int i) {
        throw new UnsupportedOperationException();
    }

    // Composite + leafItems Mehthods

    public int getId() {
        throw new UnsupportedOperationException();
    }

    public String getName() {
        throw new UnsupportedOperationException();
    }

    public double getSalary() {

        throw new UnsupportedOperationException();
    }

    public void print() {

        throw new UnsupportedOperationException();
    }



}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 24, 2019
*/

public class BankManager extends Employee {
    int id;
    String name;
    double salary;

    public BankManager(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    List<Employee> employeelist = new ArrayList<>();

    @Override
    public void add(Employee employee) {

        employeelist.add(employee);
    }

    @Override
    public void remove(Employee employee) {

        employeelist.remove(employee);
    }

    @Override
    public Employee getChild(int i) {

        return employeelist.get(i);
    }

    @Override
    public int getId() {

        return this.id;
    }

    @Override
    public String getName() {

        return this.name;
    }

    @Override
    public double getSalary() {

        return this.salary;
    }

    @Override
    public void print() {

        System.out.println("======================================");
        System.out.println("Id = " + getId());
        System.out.println("Name = " + getName());
        System.out.println("Salaray = " + getSalary());
        System.out.println("======================================");

        Iterator<Employee> it = employeelist.iterator();
        while(it.hasNext()){
            Employee emp = (Employee) it.next();
            emp.print();
        }

    }

}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 24, 2019
*/

public class Cashier extends Employee {
    int id;
    String name;
    double salary;

    public Cashier(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int getId() {

        return this.id;
    }

    @Override
    public String getName() {

        return this.name;
    }

    @Override
    public double getSalary() {

        return this.salary;
    }

    @Override
    public void print() {

        System.out.println("======================================");
        System.out.println("Id = " + getId());
        System.out.println("Name = " + getName());
        System.out.println("Salaray = " + getSalary());
        System.out.println("======================================");

    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 24, 2019
*/

public class Accountant extends Employee {
    int id;
    String name;
    double salary;

    public Accountant(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @Override
    public int getId() {

        return this.id;
    }

    @Override
    public String getName() {

        return this.name;
    }

    @Override
    public double getSalary() {

        return this.salary;
    }

    @Override
    public void print() {

        System.out.println("======================================");
        System.out.println("Id = " + getId());
        System.out.println("Name = " + getName());
        System.out.println("Salaray = " + getSalary());
        System.out.println("======================================");

    }


}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
*
* @author:          Muhammad Mohsin Mahmood
* email:            mohsinmahmoodmdl@gmail.com
* Registration#:    SP16-BSE-060
* Date:             Apr 24, 2019
*/

public class CompositePatternDemo {
    public static void main(String[] args) {

        Employee manager = new BankManager(101, "BankManager", 80000.00);
        Employee cash1 = new Cashier(202, "Cashier1", 20000.00);
        Employee cash2 = new Cashier(203, "Cashier2", 20000.00);
        Employee accountant = new Accountant(204, "Accountant", 42000.00);

        manager.add(cash1);
        manager.add(cash2);
        manager.add(accountant);

        manager.print();

    }
}