---Design Pattern Factory :-Thuộc nhóm Nhóm khởi tạo
-Tạo lớp khởi tạo thuộc cùng 1 nhóm
- Khởi tạo đối tượng linh hoạt
1.tạo lớp Interface
package InterFace;
/**
*
* @author Xuanlinh
*/
public interface IPersonal {
public void In();
public void Nhap();
public void Tinh();
}
2.Tạo lớp thực thi
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterFace;
import java.util.Scanner;
public class Personal implements IPersonal{
protected String ten;
public String getTen() {
return ten;
}
public void setTen(String ten) {
this.ten = ten;
}
public String getTuoi() {
return tuoi;
}
public void setTuoi(String tuoi) {
this.tuoi = tuoi;
}
public String getDiachi() {
return diachi;
}
public void setDiachi(String diachi) {
this.diachi = diachi;
}
protected String tuoi;
protected String diachi;
@Override
public void In() {
System.out.print("\n---------------------------------------");
System.out.print("\nNhap vao ten"+ten);
System.out.print("\nNhap vao tuoi"+tuoi);
System.out.print("\nNhap vao diachi"+diachi);
}
@Override
public void Nhap() {
Scanner input = new Scanner(System.in);
System.out.print("\nNhap vao ten");
ten=input.nextLine();
System.out.print("\nNhap vao tuoi");
tuoi=input.nextLine();
System.out.print("\nNhap vao diachi");
diachi=input.nextLine();
}
@Override
public void Tinh() {
}
}
----------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterFace;
import java.util.Scanner;
/**
*
* @author Xuanlinh
*/
public class Manager extends Personal implements IPersonal{
String capbac;
@Override
public void In() {
System.out.print("\n---------------------------------------");
super.In();
System.out.print("\nNhap vao cap bac"+capbac);
}
public String getCapbac() {
return capbac;
}
public void setCapbac(String capbac) {
this.capbac = capbac;
}
@Override
public void Nhap() {
Scanner input = new Scanner(System.in);
super.Nhap();
System.out.print("\nNhap vao capbac");
capbac=input.nextLine();
}
@Override
public void Tinh() {
System.out.print("\nLuong là 50 triệu");
}
}
-------------------------------------------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterFace;
import java.util.Scanner;
/**
*
* @author Xuanlinh
*/
public class teacher extends Personal implements IPersonal{
String chucvu;
public String getChucvu() {
return chucvu;
}
public void setChucvu(String chucvu) {
this.chucvu = chucvu;
}
@Override
public void In() {
System.out.print("\n---------------------------------------");
super.In();
System.out.print("\n chucvu"+chucvu);
}
@Override
public void Nhap() {
Scanner input = new Scanner(System.in);
super.Nhap();
System.out.print("\nNhap vao chucvu");
chucvu=input.nextLine();
}
@Override
public void Tinh() {
System.out.print("\nLuong là 10 triệu");
}
}
-------------------------------
3.Tạo 1 lớp Factory quản lý các lớp thư thi của Interface personal
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterFace;
/**
*
* @author Xuanlinh
*/
public class FactoryIPersonal {
private IPersonal p;
public FactoryIPersonal (){}
;
public IPersonal Employee(String name)
{
switch(name)
{
case "teacher" :{
p=new teacher();
return p;
}
case "manager" :{
p=new Manager();
return p;
}
}
return null;
}
}
4.gọi lớp khởi tạo factory
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterFace;
public class Main {
public static void main(String[] args) {
IPersonal pa=new FactoryIPersonal().Employee("teacher");
pa.Nhap();
pa.In();
pa.Tinh();
pa=new FactoryIPersonal().Employee("manager");
pa.Nhap();
pa.In();
pa.Tinh();
Employee E=new Employee(new teacher());
E.nhap();
E.in();
E.tinh();
}
}
Không có nhận xét nào:
Đăng nhận xét