Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

Unit 20: Advanced Programming

Assignment 2: Implementation and evaluation the design


patterns

YOUTH INTERNATIONAL COLLEGE (YIC)


Hlaing Thin Nwe
Contents
Creational Design Pattern Factory Method............................................................................................2
Structural Design Pattern of Composite Method....................................................................................5
Behavioral Design Pattern of Template Method.....................................................................................9
Design patterns with relevant examples of creational, structural and behavioral pattern types......13
Appropriate Design Patterns of Happy Baby Product.........................................................................22
Evaluation for a range of design patterns..............................................................................................22
References................................................................................................................................................23
Creational Design Pattern Factory Method
Factory Method Design Pattern with Star UML tools

Code
Factory method of creation design pattern with car company

. In the factory method of creational design pattern, there has six class These six class are
CarType, Car, LuxuryCar, SmallCar, SedanCar and CarFactory. First, CarType is a enum class
that will hold the types of car such as Small, Sedan, Luxury. And it will provide these car types
to all other class. Second, Car is an abstract class and parent class of all car type. Also it will
contain the common logic applicable in car making of all types. LuxuryCar is a public class that
extends to the Car class. SmallCar and SedanCar are also public classes which also extend to
Car class. These all LuxuryCar, SmallCar and SedanCar classes are concreted the
implementation of car type such as Luxury, Sedan, Small from CarType enum class. The last
class is CarFactory that is a public class. This class is main class implemented by using factory
pattern. In this class, after determined the car type, it instantiated a car instance. CarFactory class
will print the building car type such as Building Small Car, Building Sedan Car, Building Luxury
Car.

Composite Method Design Pattern Using with Star UML tools


Code
Composite Method of Structural Design Pattern with Bank

Draw the composite design pattern with UML tools. There are five classes in composite
method of structural design pattern. The five classes are Employee (interface class),
BankManager (public class), Cashier (public class), Accountant (public class) and
CompositePatternDemo (public class). Employee interface class will be treated as a component.
It declares public function to reuse in other class. BankManager class implements Employee
interface class and it will be used as a composite. It declares private variables and public
function, and it also include while condition statement. In print function of this class, it will print
the data by calling function such as getId, getName, getSalary. Cashier class will be treated as a
leaf and it implements to Employee (interface class). It also declares private variables and public
function, and it will print the data as BankManager class. Accountant class will implement to
Employee class and it will be used as a leaf. It also declares as Cashier class and will print the
data as BankManager class. CompositePatternDemo class will be treated as a client and it will
also use Employee class.

Behavioral Design Pattern of Template Method


Template Design Pattern using with UML tools
Code
Template Method of Behavioral Design Pattern with Construction house
The behavioral design pattern of template method has four classes, they are House,
ConcreteWallHouse, GlassWallHouse and Demo. House is an abstract class that will define the
template method called buildHouse (). This abstract class contains the template method
buildHose and implement the same for all types of houses. Template method needs to use final
function which cannot re-implement in other class. In this abstract class House, create five
abstract function such as DecorateHouse, PaintHouse, ConstructDoors, ConstructWindows,
ConstructWalls these all require to re-implement. And create two final function such as
ConstructBase and ConstructRoof that do not need to re-implement. The second class is
ConcreteWallHouse that extends House abstract class. In this class, the functions are re-
implement the abstract function from House abstract class. Each function will print the data such
as Decorating Concrete Wall House, Painting Concrete Wall House, Constructing Doors for
Concrete Wall House, Constructing Windows for Concreate Wall House and Constructing
Concrete Wall for my House. The third class is GlassWallHouse which extends House. This
class also re-implement the function from House. It also will print as ConcreteWallHouse. The
last class is Demo that builds. In main function of Demo class, create the ConcreteWallHouse
object which will print “Concrete Wall House constructed successfully”. And also create
GlassWallHouse object in this Demo class that also will print “Glass Wall House constructed
successfully”.

Design patterns with relevant examples of creational, structural and


behavioral pattern types
As the following design pattern of coding are developed by my program.

Singleton Method-Creational Design Pattern


Creation design patterns is used to make the decision for instantiation of a class. singleton
pattern has only one instance of the class that exists in the Java virtual machine.

public class Managerloginframe extends JFrame implements ActionListener {


JTextField txname;
JPasswordField txpw;
public Managerloginframe() {
setTitle("Login");
setSize(500,500);
setLocation(100,100);
setLayout(new GridLayout(4,1));

p1=new JPanel();
JLabel lb=new JLabel("<html><CENTER><H3><i>ManagerLogin</i>
<H3><CENTER></html>");
p1.add(lb);

p2=new JPanel();
JLabel lbname= new JLabel("Enter name");
txname= new JTextField();
txname.setColumns(10);
p2.add(lbname);
p2.add(txname);

p3=new JPanel();
JLabel lpwd= new JLabel("Enter Password");
txpw= new JPasswordField();
txpw.setColumns(10);
txpw.setEchoChar('$');
p3.add(lpwd);
p3.add(txpw);

p4=new JPanel();
JButton btok=new JButton("OK");
btok.addActionListener(this);
JButton btcancle=new JButton("Cancel");
btcancle.addActionListener(this);
p4.add(btok);
p4.add(btcancle);
add(p1);
add(p2);
add(p3);
add(p4);

setVisible(true);
setDefaultCloseOperation(1);
}

Composite Pattern-Structural Design Pattern


In my program, the following code is developed for structural design pattern. The
composite pattern of structural design pattern is used for treating a whole group of objects in a
similar.

if(e.getSource()==btadd)
{
System.out.println(selecttype);
if(selecttype.equals("Dress"))
{
productclass p=new Dress();
p.showproductinsertframe();
}
else if(selecttype.equals("Hats"))
{
productclass p=new Hats();
p.showproductinsertframe();
}
else if(selecttype.equals("Accessories"))
{
productclass p=new Accessories();
p.showproductinsertframe();
}
}
else if(e.getSource()==btlistall)
{
System.out.println(selecttype);
if(selecttype.equals("Dress"))
{
productclass p=new Dress();
p.showproductlistallframe();
}
else if(selecttype.equals("Hats"))
{
productclass p=new Hats();
p.showproductlistallframe();
}
else if(selecttype.equals("Accessories"))
{
productclass p=new Accessories();
p.showproductlistallframe();
}
}
else if(e.getSource()==btedit)
{
System.out.println(selecttype);
if(selecttype.equals("Dress"))
{
productclass p=new Dress();
p.producteditframe();
}
else if(selecttype.equals("Hats"))
{
productclass p=new Hats();
p.producteditframe();
}
else if(selecttype.equals("Accessories"))
{
productclass p=new Accessories();
p.producteditframe();
}
}
else if(e.getSource()==btsearch)
{
System.out.println(selecttype);
if(selecttype.equals("Dress"))
{
productclass p=new Dress();
p.productsearchframe();
}
else if(selecttype.equals("Hats"))
{
productclass p=new Hats();
p.productsearchframe();
}
else if(selecttype.equals("Accessories"))
{
productclass p=new Accessories();
p.productsearchframe();
}
}
else if(e.getSource()==btremove)
{
System.out.println(selecttype);
if(selecttype.equals("Dress"))
{
DressRemove dr=new DressRemove();
}
else if(selecttype.equals("Hats"))
{
HatsRemove hr=new HatsRemove();
}
else if(selecttype.equals("Accessories"))
{
AccessoriesRemove ar=new AccessoriesRemove();
}
}
}

Template Method-Behavioral Design Pattern


The template method of behavioral design pattern is defining an abstract class. It provides
to inherit from sub classes that follow the way defined in the abstract class.
package Assignment2;
public abstract class productclass {

abstract void setdata(int id, String name, double price, int age, String type, String
made);
abstract void showframe();
abstract void showproductinsertframe();
abstract void productsearchframe();

package Assignment2;
public class Dress extends productclass {

int did, age;


String name, type, made, gender;
double price;

void setdata(int id, String name, double price, int age, String type, String made)
{

}
void showframe()
{

}
void showproductinsertframe()
{

}
void productsearchframe()
{

}
}
package Assignment2;
public class Hats extends productclass {

int hid, age;


String name, type, made, gender;
double price;

void setdata(int id, String name, double price, int age, String type, String made)
{

}
void showframe()
{

}
void showproductinsertframe()
{

}
void productsearchframe()
{

}
}
package Assignment2;
public class Accessories extends productclass {

int aid, age;


String name, type, made, gender;
double price;

void setdata(int id, String name, double price, int age, String type, String made)
{

}
void showframe()
{

}
void showproductinsertframe()
{

}
void productsearchframe()
{
}
}
Appropriate Design Patterns of Happy Baby Product
In my program, I used many design pattern such as singleton method of creational design,
composite method of structural design pattern and template method of behavioral design pattern.
I chose template method to appropriate in happy baby product because it can implement the
function from subclass to abstract class. Template method defines an algorithm as the skeleton of
operations. It has different stages of algorithm, there are an abstract class, subclasses and base
class. In template method design pattern, it creates an abstract class and defers the
implementation steps to the subclasses. The template method includes steps that can be abstract
method. The abstract method of subclasses will implement to it. The subclasses are called
superclass template method and default implementation classes are also called base class.
subclasses intend to be overridden on base class. We can make the methods final if we don’t
want to be overridden them. It is used in frameworks and it also used to implement with different
behavior from subclasses. Template method is based on inheritance. It controls to allow
depending on the points subclasses. It only allows the specific details of the workflow to change.
It is easy to understand and it avoids the code duplication by implementing the coding. By
separating the algorithm into private methods that can simple and easy to test. By using an
abstract class, we remove the common parts of problem domain so that we can easily create
concrete implementation of algorithm. We use this pattern when our classes have connected
actions.

Evaluation for a range of design patterns


As the following two design patterns are most appropriate method and most inappropriate
method. These two design patterns are template design pattern and singleton design pattern. In
template method design pattern, this method defines the algorithm as a framework for
processing. And the Singleton method is a creative design pattern that is one of the simplest
design patterns. Make sure that the class has only one instance and provides a global access
point. When we use template method design pattern, create an abstract class using the template
method of design pattern and move the implementation steps to a subclass. The template method
includes steps that can be abstract methods. Implements the abstract method of the subclass.
Subclasses are overridden in the base class. If we don't want to overridden, we may be able to
create the method. The template approach is based on inheritance. But in singleton method
design pattern, contains a single class that creates objects while ensuring that only one object is
created. This class provides a way to access the only object that can be accessed directly without
having to instantiate an object of the class. And then, template design pattern is easy to
understand and implements coding to avoid code duplication. But, singleton design pattern is one
of the most inappropriately used patterns. It is suitable to be used when a class must have exactly
one instance. For these reason, I use the template design pattern. And in my program, we require
to reuse the coding so that I chose template design pattern to appropriate. Singleton design
pattern can’t re use the code that can happen duplicate code.

References
https://howtodoinjava.com/design-patterns/behavioral/template-method-pattern/

https://www.javatpoint.com/composite-pattern

https://www.geeksforgeeks.org/composite-design-pattern/

https://howtodoinjava.com/design-patterns/creational/implementing-factory-design-
pattern-in-java/

https://www.javatpoint.com/singleton-design-pattern-in-java

http://www.blackwasp.co.uk/Singleton.aspx

https://www.callicoder.com/java-singleton-design-pattern-example/

https://medium.com/better-programming/design-patterns-template-method-

5400dde7bb72

https://medium.com/@nanofaroque/template-method-design-pattern-in-javascript-

286384155823

https://howtodoinjava.com/design-patterns/behavioral/template-method-pattern/

You might also like