DEV Community

Mustafa  Çam
Mustafa Çam

Posted on

Örnek ile converter java

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CustomerConverter {

public static Customer toCustomer(CustomerSaveRequest request) {
    String hashedPassword = HashUtil.generate(request.getPassword());

    Customer customer = new Customer(request.getName(), request.getSurname(), request.getEmail(), hashedPassword);
    customer.setAccountType(AccountType.STANDARD);
    customer.setIsActive(Boolean.TRUE);
    return customer;
}
Enter fullscreen mode Exit fullscreen mode

}
burda ne yapılmış ?

Bu kod parçasında, bir CustomerConverter adında bir sınıf oluşturulmuş ve bu sınıf içinde bir statik metot tanımlanmış. Bu metot, bir CustomerSaveRequest nesnesini alarak bir Customer nesnesine dönüştürmek için kullanılıyor. İşlemlerin ayrıntılarına bakalım:

Anotasyon ve Sınıf Tanımı

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CustomerConverter {
Enter fullscreen mode Exit fullscreen mode
  • @NoArgsConstructor(access = AccessLevel.PRIVATE): Bu anotasyon, Lombok kütüphanesi tarafından sağlanır ve sınıfın özel bir parametresiz (no-args) kurucu oluşturur. Böylece bu sınıftan nesne oluşturulması engellenir. Bu, tüm metotların statik olacağını ve bu sınıfın yalnızca bir yardımcı (utility) sınıf olarak kullanılacağını gösterir.

Statik Metot

public static Customer toCustomer(CustomerSaveRequest request) {
Enter fullscreen mode Exit fullscreen mode
  • public static Customer toCustomer(CustomerSaveRequest request): Bu metot, CustomerSaveRequest nesnesini alarak bir Customer nesnesine dönüştürür. Metot statik olduğu için CustomerConverter sınıfının bir örneğini oluşturmadan doğrudan çağrılabilir.

Parolanın Hashlenmesi

String hashedPassword = HashUtil.generate(request.getPassword());
Enter fullscreen mode Exit fullscreen mode
  • String hashedPassword = HashUtil.generate(request.getPassword()): request nesnesinden alınan parola (password), HashUtil sınıfındaki generate metodu kullanılarak hashlenir. Bu, parolanın güvenli bir şekilde saklanmasını sağlar.

Customer Nesnesinin Oluşturulması

Customer customer = new Customer(request.getName(), request.getSurname(), request.getEmail(), hashedPassword);
Enter fullscreen mode Exit fullscreen mode
  • Customer customer = new Customer(...): CustomerSaveRequest nesnesinden alınan isim (name), soyisim (surname), e-posta (email) ve hashlenmiş parola (hashedPassword) kullanılarak yeni bir Customer nesnesi oluşturulur.

Ek Ayarların Yapılması

customer.setAccountType(AccountType.STANDARD);
customer.setIsActive(Boolean.TRUE);
Enter fullscreen mode Exit fullscreen mode
  • customer.setAccountType(AccountType.STANDARD): Müşterinin hesap türü STANDARD olarak ayarlanır.
  • customer.setIsActive(Boolean.TRUE): Müşteri aktif olarak işaretlenir.

Sonuç

return customer;
Enter fullscreen mode Exit fullscreen mode
  • return customer: Oluşturulan ve ayarları yapılan Customer nesnesi geri döndürülür.

Özet

Bu kod parçası, bir CustomerSaveRequest nesnesinden bir Customer nesnesi oluşturan bir yardımcı sınıf (utility class) tanımlar. CustomerConverter sınıfı, özel bir kurucuya sahip olduğu için doğrudan örneklenemez. Statik toCustomer metodu, CustomerSaveRequest'ten gelen verileri kullanarak yeni bir Customer nesnesi oluşturur, parolayı hashler, hesap türünü ve aktif durumunu ayarlar ve son olarak bu nesneyi geri döndürür.

Image of Wix Studio

2025: Your year to build apps that sell

Dive into hands-on resources and actionable strategies designed to help you build and sell apps on the Wix App Market.

Get started

Top comments (0)

Image of Bright Data

Ensure Data Quality Across Sources – Manage and normalize data effortlessly.

Maintain high-quality, consistent data across multiple sources with our efficient data management tools.

Manage Data

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay