Yeni Sınıf Oluşturma (Extract Class)

Bir sınıfın yüzlerce ya da binlerce satırdan oluşmasının ana sebeplerinden birisi bu sınıfa birden fazla sorumluluğun yüklenmiş olmasıdır. Single Repsonsiblity (SRP) prensibinden de bildigimiz gibi her sınıfın sadece ve sadece bir sorumluluk alanı olmalıdır yani sınıf sadece bir iş yapmalıdır ve bu işlemi iyi yapmalıdır. Aşağıda yer alan Order sınıfı SRP ile uyumlu değildir. Yeni Sınıf Oluşturma (Extract Class) refactoring metodunu kullanarak bu sınıfı SRP’ye uygun hale getirebiliriz.

package com.kurumsaljava.refactoring.extractClass;


public class Order {

         private double price;
	
	// siparis verilen ürün
	private String productName;

	// siparis veren müsteri bilgileri
	private String customerName;
	private String customerFirstnam;
	private String customerBirthdate;
	
	// müsteri adresi
	private String customerStreet;
	private String customerHouseNumber;
	private String customerZip;
	private String customerTown;
	private String customerCountry;
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getCustomerFirstnam() {
		return customerFirstnam;
	}
	public void setCustomerFirstnam(String customerFirstnam) {
		this.customerFirstnam = customerFirstnam;
	}
	public String getCustomerBirthdate() {
		return customerBirthdate;
	}
	public void setCustomerBirthdate(String customerBirthdate) {
		this.customerBirthdate = customerBirthdate;
	}
	public String getCustomerStreet() {
		return customerStreet;
	}
	public void setCustomerStreet(String customerStreet) {
		this.customerStreet = customerStreet;
	}
	public String getCustomerHouseNumber() {
		return customerHouseNumber;
	}
	public void setCustomerHouseNumber(String customerHouseNumber) {
		this.customerHouseNumber = customerHouseNumber;
	}
	public String getCustomerZip() {
		return customerZip;
	}
	public void setCustomerZip(String customerZip) {
		this.customerZip = customerZip;
	}
	public String getCustomerTown() {
		return customerTown;
	}
	public void setCustomerTown(String customerTown) {
		this.customerTown = customerTown;
	}
	public String getCustomerCountry() {
		return customerCountry;
	}
	public void setCustomerCountry(String customerCountry) {
		this.customerCountry = customerCountry;
	}
}

Yeni Sınıf Oluşturma (Extract Class) refactoring metodunu uygulayarak ürün, müşteri ve müşteri adresi bilgilerini kendi sınıflarına taşıyabiliriz. Bu işlemin ardından Order sınıfı aşağıdaki yapıya sahip olacaktır:


package com.kurumsaljava.refactoring.extractClass;


public class OrderTemp {

		
	private Product product = new Product();

	private Customer customer = new Customer();

	private CustomerAddress address = new CustomerAddress();
	
	public double getPrice() {
		return product.getPrice();
	}
	public void setPrice(double price) {
		this.product.setPrice(price);
	}
	public String getProductName() {
		return product.getProductName();
	}
	public void setProductName(String productName) {
		this.product.setProductName(productName);
	}
	public String getCustomerName() {
		return customer.getCustomerName();
	}
	public void setCustomerName(String customerName) {
		this.customer.setCustomerName(customerName);
	}
	public String getCustomerFirstnam() {
		return customer.getCustomerFirstnam();
	}
	public void setCustomerFirstnam(String customerFirstnam) {
		this.customer.setCustomerFirstnam(customerFirstnam);
	}
	public String getCustomerBirthdate() {
		return customer.getCustomerBirthdate();
	}
	public void setCustomerBirthdate(String customerBirthdate) {
		this.customer.setCustomerBirthdate(customerBirthdate);
	}
	public String getCustomerStreet() {
		return address.getCustomerStreet();
	}
	public void setCustomerStreet(String customerStreet) {
		this.address.setCustomerStreet(customerStreet);
	}
	public String getCustomerHouseNumber() {
		return address.getCustomerHouseNumber();
	}
	public void setCustomerHouseNumber(String customerHouseNumber) {
		this.address.setCustomerHouseNumber(customerHouseNumber);
	}
	public String getCustomerZip() {
		return address.getCustomerZip();
	}
	public void setCustomerZip(String customerZip) {
		this.address.setCustomerZip(customerZip);
	}
	public String getCustomerTown() {
		return address.getCustomerTown();
	}
	public void setCustomerTown(String customerTown) {
		this.address.setCustomerTown(customerTown);
	}
	public String getCustomerCountry() {
		return address.getCustomerCountry();
	}
	public void setCustomerCountry(String customerCountry) {
		this.address.setCustomerCountry(customerCountry);
	}
}

Değişik sorumluluk alanlarını ifade eden sınıf değişkenleri yerine sorumluluk alanını kapsayan sınıf tipinde değişkenler kullandık. Böylece her bir sorumluluk alanını yeni bir sınıf ile izole etmiş olduk. Order bünyesinde yer alan metotlar delegasyon usulü ile bu yeni degişkenleri kullanabilirler. Eğer bunu istemiyorsak Order bünyesinde bulunan eski sınıf değişkenleri ile ilişkili tüm metotları kaldırarak, Order sınıfını kullanan diğer sınıfların oluşturdugumuz yeni sınıfları kullanmalarını sağlayabiliriz.


EOF (End Of Fun)
Özcan Acar

Share Button
0.00 avg. rating (0% score) - 0 votes

Bir cevap yazın