Abstract Factory Pattern merupakan salah satu jenis creational pattern yang menangani mekanisme penciptaan object secara adiptif. Pattern ini menyediakan sebuah mekanisme untuk menciptakan keluarga terkait atau object yang terkandung tanpa menentukan concrete class mereka.
Abstract factory memberikan sekumpulan abstract method untuk menciptakan jenis product tertentu yang harus diimplementasikan oleh concrete factory. Abstract product memberikan abstraksi pada keluarga product yang berbeda motif dimana tentunya memiliki cara pengimplementasian yang berbeda pula. Istilah “motif” diartikan sebagai suatu theme atau corak khusus yang melekat pada suatu product .
Berikut langkah-langkah membuat Abstract Factory Pattern
1. Buat class abstract Employee.java
package belajar.design.pattern; public abstract class Employee { private String name; public abstract float getSalary(); public void setName(String name){ this.name = name; } public String getName(){ return this.name; } }
2. Buat class CommissionedEmployee.java dan meng-extends Employee.java
package belajar.design.pattern; public class CommissionedEmployee extends Employee{ private float salesAmount; private float comission; public CommissionedEmployee(String name, float sales, float comission){ setName(name); this.salesAmount = sales; this.comission = comission; } public float getSalesAmount(){ return this.salesAmount; } public float getComission(){ return this.comission; } @Override public float getSalary() { return (getSalesAmount() * getComission()); } }
3. Buat class SalariedEmployee.java dan meng-extends Employee.java
package belajar.design.pattern; public class SalariedEmployee extends Employee{ private float basic; private float allowance; public SalariedEmployee(String name, float basic, float allowance){ setName(name); this.basic = basic; this.allowance = allowance; } public float getBasic(){ return this.basic; } public float getAllowance(){ return this.allowance; } @Override public float getSalary() { return (getBasic()+(getBasic()*getAllowance())); } }
4. Buat class EmployeeFactory.java
package belajar.design.pattern; public class EmployeeFactory { public Employee createEmployee(String name, String type, float bs, float ac){ if(type.equals("sal")){ return new SalariedEmployee(name,bs,ac); }else if(type.equals("comm")){ return new CommissionedEmployee(name,bs,ac); }else{ return null; } } }
5. Buat class TestAbstractFactoryPattern.java
package belajar.design.pattern; public class TestAbstractFactoryPattern { public static void main (String[] args){ EmployeeFactory factory = new EmployeeFactory(); Employee e1 = factory.createEmployee("Andrew", "sal", 5000.0f, 6.7f ); Employee e5 = factory.createEmployee("Joe", "sal", 360000.0f, 0.18f ); System.out.println(e1.getName()+"received : $"+e1.getSalary()); System.out.println(e5.getName()+"received : $"+e5.getSalary()); } }
6. Verifikasi Output:
Andrewreceived : $38500.0 Joereceived : $424800.0
Baca Juga :
Java Builder Pattern
Entri Sebelumnya :
Materi Tutorial Java Design Pattern
No comments:
Post a Comment