Professional Documents
Culture Documents
Hlaing Thin Nwe - A2 (Advprogrammming)
Hlaing Thin Nwe - A2 (Advprogrammming)
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.
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.
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);
}
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();
}
}
}
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 {
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 {
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 {
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.
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/