By GokiSoft.com| 13:36 15/04/2020|
Java Basic

Share Code - Thiết kế Singleton Design Pattern


/*
 * 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 lession11;

/**
 *
 * @author Diep.Tran
 */
public class Category {
    int id;
    String catName;

    public Category() {
    }

    public Category(int id, String catName) {
        this.id = id;
        this.catName = catName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }
    
    
}



/*
 * 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 lession11;

import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class Product {
    int id, idCat;
    String title;
    float price;

    public Product() {
    }

    public Product(int id, int idCat, String title, float price) {
        this.id = id;
        this.idCat = idCat;
        this.title = title;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getIdCat() {
        return idCat;
    }

    public void setIdCat(int idCat) {
        this.idCat = idCat;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
    
    public void input() {
        List<Category> categorys = DataMgr.getInstance().getCategorys();
    }
}



/*
 * 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 lession11;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Diep.Tran
 */
public class DataMgr {
    List<Category> categorys;
    List<Product> products;
    
    private static DataMgr instance = null;
    
    private DataMgr() {
        categorys = new ArrayList<>();
        products = new ArrayList<>();
        System.out.println("Init object from class object DataMgr");
    }
    
    public static DataMgr getInstance() {
        if(instance == null) {
            instance = new DataMgr();
        }
        return instance;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }
    
    
}



/*
 * 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 lession11;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        DataMgr dataMgr1 = DataMgr.getInstance();
        DataMgr dataMgr2 = DataMgr.getInstance();
        DataMgr dataMgr3 = DataMgr.getInstance();
        DataMgr dataMgr4 = DataMgr.getInstance();
        DataMgr dataMgr5 = DataMgr.getInstance();
        //Dang tao ra dc 5 doi tuong => tao nhieu doi tuong tu DataMgr
        //code cac chuc nang khac
        //them danh muc san pham
        //them san pham
        //sua, xoa, tim kiem san pham
        //sua, xoa, tim kiem danh muc san pham
        //luu file, sort, ...
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)