中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站建設(shè)公司專業(yè)網(wǎng)站開發(fā)需求seo服務(wù)外包客服

網(wǎng)站建設(shè)公司專業(yè)網(wǎng)站開發(fā)需求,seo服務(wù)外包客服,個人網(wǎng)頁設(shè)計硬件需求,河北建設(shè)廳網(wǎng)站電話文章目錄 前言1、抽取公用方法2、抽工具類3、反射4、泛型5、繼承與多態(tài)6、使用設(shè)計模式7、自定義注解(或者說AOP面向切面)8、函數(shù)式接口和Lambda表達式 前言 日常開發(fā)中,我們經(jīng)常會遇到一些重復(fù)代碼。大家都知道重復(fù)代碼不好,它主要有這些缺點&#xff…

文章目錄

  • 前言
  • 1、抽取公用方法
  • 2、抽工具類
  • 3、反射
  • 4、泛型
  • 5、繼承與多態(tài)
  • 6、使用設(shè)計模式
  • 7、自定義注解(或者說AOP面向切面)
  • 8、函數(shù)式接口和Lambda表達式


前言

日常開發(fā)中,我們經(jīng)常會遇到一些重復(fù)代碼。大家都知道重復(fù)代碼不好,它主要有這些缺點:可維護性差、可讀性差、增加錯誤風(fēng)險等等。這里給大家講講優(yōu)化重復(fù)代碼的幾種方式。


1、抽取公用方法

抽取公用方法,是最常用的代碼去重方法~

比如這個例子,分別遍歷names列表,然后各自轉(zhuǎn)化為大寫和小寫打印出來:

public class TianLuoExample {public static void main(String[] args) {List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "TianLuo");System.out.println("Uppercase Names:");for (String name : names) {String uppercaseName = name.toUpperCase();System.out.println(uppercaseName);}System.out.println("Lowercase Names:");for (String name : names) {String lowercaseName = name.toLowerCase();System.out.println(lowercaseName);}}
}

顯然,都是遍歷names過程,代碼是重復(fù)的,只不過轉(zhuǎn)化大小寫不一樣。我們可以抽個公用方法processNames,優(yōu)化成這樣:

public class TianLuoExample {public static void processNames(List<String> names, Function<String, String> nameProcessor, String processType) {System.out.println(processType + " Names:");for (String name : names) {String processedName = nameProcessor.apply(name);System.out.println(processedName);}}public static void main(String[] args) {List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "TianLuo");processNames(names, String::toUpperCase, "Uppercase");processNames(names, String::toLowerCase, "Lowercase");}
}

2、抽工具類

我們優(yōu)化重復(fù)代碼,抽一個公用方法后,如果發(fā)現(xiàn)這個方法有更多共性,就可以把公用方法升級為一個工具類。比如這樣的業(yè)務(wù)場景:我們注冊的時候,修改郵箱,重置密碼等,都需要校驗郵箱。

實現(xiàn)注冊功能時,用戶會填郵箱,需要驗證郵箱格式

public class RegisterServiceImpl implements RegisterService{private static final String EMAIL_REGEX ="^[A-Za-z0-9+_.-]+@(.+)$";public boolean registerUser(UserInfoReq userInfo) {String email = userInfo.getEmail();Pattern pattern = Pattern.compile(EMAIL_REGEX);Matcher emailMatcher = pattern.matcher(email);if (!emailMatcher.matches()) {System.out.println("Invalid email address.");return false;}// 進行其他用戶注冊邏輯,比如保存用戶信息到數(shù)據(jù)庫等// 返回注冊結(jié)果return true;}
}

密碼重置流程中,通常會向用戶提供一個鏈接或驗證碼,并且需要發(fā)送到用戶的電子郵件地址。在這種情況下,也需要驗證郵箱格式合法性

public class PasswordServiceImpl implements PasswordService{private static final String EMAIL_REGEX ="^[A-Za-z0-9+_.-]+@(.+)$";public void resetPassword(PasswordInfo passwordInfo) {Pattern pattern = Pattern.compile(EMAIL_REGEX);Matcher emailMatcher = pattern.matcher(passwordInfo.getEmail());if (!emailMatcher.matches()) {System.out.println("Invalid email address.");return false;}//發(fā)送通知修改密碼sendReSetPasswordNotify();}
}

我們可以抽取個校驗郵箱的方法出來,又因為校驗郵箱的功能在不同的類中,因此,我們可以抽個校驗郵箱的工具類

public class EmailValidatorUtil {private static final String EMAIL_REGEX ="^[A-Za-z0-9+_.-]+@(.+)$";private static final Pattern pattern = Pattern.compile(EMAIL_REGEX);public static boolean isValid(String email) {Matcher matcher = pattern.matcher(email);return matcher.matches();}
}//注冊的代碼可以簡化為這樣啦
public class RegisterServiceImpl implements RegisterService{public boolean registerUser(UserInfoReq userInfo) {if (!EmailValidatorUtil.isValid(userInfo.getEmail())) {System.out.println("Invalid email address.");return false;}// 進行其他用戶注冊邏輯,比如保存用戶信息到數(shù)據(jù)庫等// 返回注冊結(jié)果return true;}
}

3、反射

我們?nèi)粘i_發(fā)中,經(jīng)常需要進行PO、DTO和VO的轉(zhuǎn)化。所以大家經(jīng)??吹筋愃频拇a:

    //DTO 轉(zhuǎn)VOpublic UserInfoVO convert(UserInfoDTO userInfoDTO) {UserInfoVO userInfoVO = new UserInfoVO();userInfoVO.setUserName(userInfoDTO.getUserName());userInfoVO.setAge(userInfoDTO.getAge());return userInfoVO;}//PO 轉(zhuǎn)DTOpublic UserInfoDTO convert(UserInfoPO userInfoPO) {UserInfoDTO userInfoDTO = new UserInfoDTO();userInfoDTO.setUserName(userInfoPO.getUserName());userInfoDTO.setAge(userInfoPO.getAge());return userInfoDTO;}

我們可以使用BeanUtils.copyProperties() 去除重復(fù)代碼BeanUtils.copyProperties()底層就是使用了反射

    public UserInfoVO convert(UserInfoDTO userInfoDTO) {UserInfoVO userInfoVO = new UserInfoVO();BeanUtils.copyProperties(userInfoDTO, userInfoVO);return userInfoVO;}public UserInfoDTO convert(UserInfoPO userInfoPO) {UserInfoDTO userInfoDTO = new UserInfoDTO();BeanUtils.copyProperties(userInfoPO,userInfoDTO);return userInfoDTO;}

4、泛型

泛型是如何去除重復(fù)代碼的呢?給大家看個例子,我有個轉(zhuǎn)賬明細和轉(zhuǎn)賬余額對比的業(yè)務(wù)需求,有兩個類似這樣的方法:

private void getAndUpdateBalanceResultMap(String key, Map<String, List<TransferBalanceDTO>> compareResultListMap,
List<TransferBalanceDTO> balanceDTOs) {List<TransferBalanceDTO> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());tempList.addAll(balanceDTOs);compareResultListMap.put(key, tempList);
}private void getAndUpdateDetailResultMap(String key, Map<String, List<TransferDetailDTO>> compareResultListMap,List<TransferDetailDTO> detailDTOS) {List<TransferDetailDTO> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());tempList.addAll(detailDTOS);compareResultListMap.put(key, tempList);
}

這兩塊代碼,流程功能看著很像,但是就是不能直接合并抽取一個公用方法,因為類型不一致。單純類型不一樣的話,我們可以結(jié)合泛型處理,因為泛型的本質(zhì)就是參數(shù)化類型.優(yōu)化為這樣:

private <T> void getAndUpdateResultMap(String key, Map<String, List<T>> compareResultListMap, List<T> accountingDTOS) {
List<T> tempList = compareResultListMap.getOrDefault(key, new ArrayList<>());
tempList.addAll(accountingDTOS);
compareResultListMap.put(key, tempList);
}

5、繼承與多態(tài)

假設(shè)你正在開發(fā)一個電子商務(wù)平臺,需要處理不同類型的訂單,例如普通訂單和折扣訂單。每種訂單都有一些共同的屬性(如訂單號、購買商品列表)和方法(如計算總價、生成訂單報告),但折扣訂單還有特定的屬性和方法

在沒有使用繼承和多態(tài)的話,會寫出類似這樣的代碼:

//普通訂單
public class Order {private String orderNumber;private List<Product> products;public Order(String orderNumber, List<Product> products) {this.orderNumber = orderNumber;this.products = products;}public double calculateTotalPrice() {double total = 0;for (Product product : products) {total += product.getPrice();}return total;}public String generateOrderReport() {return "Order Report for " + orderNumber + ": Total Price = $" + calculateTotalPrice();}
}//折扣訂單
public class DiscountOrder {private String orderNumber;private List<Product> products;private double discountPercentage;public DiscountOrder(String orderNumber, List<Product> products, double discountPercentage) {this.orderNumber = orderNumber;this.products = products;this.discountPercentage = discountPercentage;}public double calculateTotalPrice() {double total = 0;for (Product product : products) {total += product.getPrice();}return total - (total * discountPercentage / 100);}public String generateOrderReport() {return "Order Report for " + orderNumber + ": Total Price = $" + calculateTotalPrice();}
}

顯然,看到在OrderDiscountOrder類中,generateOrderReport() 方法的代碼是完全相同的。calculateTotalPrice()則是有一點點區(qū)別,但也大相徑庭。

我們可以使用繼承和多態(tài)去除重復(fù)代碼,讓DiscountOrder去繼承Order,代碼如下:

public class Order {private String orderNumber;private List<Product> products;public Order(String orderNumber, List<Product> products) {this.orderNumber = orderNumber;this.products = products;}public double calculateTotalPrice() {double total = 0;for (Product product : products) {total += product.getPrice();}return total;}public String generateOrderReport() {return "Order Report for " + orderNumber + ": Total Price = $" + calculateTotalPrice();}
}public class DiscountOrder extends Order {private double discountPercentage;public DiscountOrder(String orderNumber, List<Product> products, double discountPercentage) {super(orderNumber, products);this.discountPercentage = discountPercentage;}@Overridepublic double calculateTotalPrice() {double total = super.calculateTotalPrice();return total - (total * discountPercentage / 100);}
}

6、使用設(shè)計模式

很多設(shè)計模式可以減少重復(fù)代碼、提高代碼的可讀性、可擴展性.比如:

  • 工廠模式: 通過工廠模式,你可以將對象的創(chuàng)建和使用分開,從而減少重復(fù)的創(chuàng)建代碼。
  • 策略模式: 策略模式定義了一族算法,將它們封裝成獨立的類,并使它們可以互相替換。通過使用策略模式,你可以減少在代碼中重復(fù)使用相同的邏輯。
  • 模板方法模式:模板方法模式定義了一個算法的骨架,將一些步驟延遲到子類中實現(xiàn)。這有助于避免在不同類中重復(fù)編寫相似的代碼。

舉個例子,模板方法是如何去除重復(fù)代碼的吧,業(yè)務(wù)場景:

假設(shè)你正在開發(fā)一個咖啡和茶的制作流程,制作過程中的熱水和添加物質(zhì)的步驟是相同的,但是具體的飲品制作步驟是不同的。

如果沒有使用模板方法模式,實現(xiàn)是這樣的:

public class Coffee {public void prepareCoffee() {boilWater();brewCoffeeGrinds();pourInCup();addCondiments();}private void boilWater() {System.out.println("Boiling water");}private void brewCoffeeGrinds() {System.out.println("Brewing coffee grinds");}private void pourInCup() {System.out.println("Pouring into cup");}private void addCondiments() {System.out.println("Adding sugar and milk");}
}public class Tea {public void prepareTea() {boilWater();steepTeaBag();pourInCup();addLemon();}private void boilWater() {System.out.println("Boiling water");}private void steepTeaBag() {System.out.println("Steeping the tea bag");}private void pourInCup() {System.out.println("Pouring into cup");}private void addLemon() {System.out.println("Adding lemon");}
}

這個代碼例子,我們可以發(fā)現(xiàn),燒水和倒入杯子的步驟代碼,在CoffeeTea類中是重復(fù)的。

使用模板方法模式,代碼可以優(yōu)化成這樣:

abstract class Beverage {public final void prepareBeverage() {boilWater();brew();pourInCup();addCondiments();}private void boilWater() {System.out.println("Boiling water");}abstract void brew();private void pourInCup() {System.out.println("Pouring into cup");}abstract void addCondiments();
}class Coffee extends Beverage {@Overridevoid brew() {System.out.println("Brewing coffee grinds");}@Overridevoid addCondiments() {System.out.println("Adding sugar and milk");}
}class Tea extends Beverage {@Overridevoid brew() {System.out.println("Steeping the tea bag");}@Overridevoid addCondiments() {System.out.println("Adding lemon");}
}

在這個例子中,我們創(chuàng)建了一個抽象類Beverage,其中定義了制作飲品的模板方法 prepareBeverage()。這個方法包含了燒水、倒入杯子等共同的步驟,而將制作過程中的特定步驟 brew()addCondiments()延遲到子類中實現(xiàn)。這樣,我們避免了在每個具體的飲品類中重復(fù)編寫相同的燒水和倒入杯子的代碼,提高了代碼的可維護性和重用性。

7、自定義注解(或者說AOP面向切面)

使用 AOP框架可以在不同地方插入通用的邏輯,從而減少代碼重復(fù)。

業(yè)務(wù)場景:

假設(shè)你正在開發(fā)一個Web應(yīng)用程序,需要對不同的Controller方法進行權(quán)限檢查。每個Controller方法都需要進行類似的權(quán)限驗證,但是重復(fù)的代碼會導(dǎo)致代碼的冗余和維護困難。

public class MyController {public void viewData() {if (!User.hasPermission("read")) {throw new SecurityException("Insufficient permission to access this resource.");}// Method implementation}public void modifyData() {if (!User.hasPermission("write")) {throw new SecurityException("Insufficient permission to access this resource.");}// Method implementation}
}

你可以看到在每個需要權(quán)限校驗的方法中都需要重復(fù)編寫相同的權(quán)限校驗邏輯,即出現(xiàn)了重復(fù)代碼.我們使用自定義注解的方式能夠?qū)?quán)限校驗邏輯集中管理,通過切面來處理,消除重復(fù)代碼.如下:

@Aspect
@Component
public class PermissionAspect {@Before("@annotation(requiresPermission)")public void checkPermission(RequiresPermission requiresPermission) {String permission = requiresPermission.value();if (!User.hasPermission(permission)) {throw new SecurityException("Insufficient permission to access this resource.");}}
}public class MyController {@RequiresPermission("read")public void viewData() {// Method implementation}@RequiresPermission("write")public void modifyData() {// Method implementation}
}

就這樣,不管多少個Controller方法需要進行權(quán)限檢查,你只需在方法上添加相應(yīng)的注解即可。權(quán)限檢查的邏輯在切面中集中管理,避免了在每個Controller方法中重復(fù)編寫相同的權(quán)限驗證代碼。這大大提高了代碼的可讀性、可維護性,并避免了代碼冗余。

8、函數(shù)式接口和Lambda表達式

業(yè)務(wù)場景:

假設(shè)你正在開發(fā)一個應(yīng)用程序,需要根據(jù)不同的條件來過濾一組數(shù)據(jù)。每次過濾的邏輯都可能會有些微的不同,但基本的流程是相似的。

沒有使用函數(shù)式接口和Lambda表達式的情況:

public class DataFilter {public List<Integer> filterPositiveNumbers(List<Integer> numbers) {List<Integer> result = new ArrayList<>();for (Integer number : numbers) {if (number > 0) {result.add(number);}}return result;}public List<Integer> filterEvenNumbers(List<Integer> numbers) {List<Integer> result = new ArrayList<>();for (Integer number : numbers) {if (number % 2 == 0) {result.add(number);}}return result;}
}

在這個例子中,我們有兩個不同的方法來過濾一組數(shù)據(jù),但是基本的循環(huán)和條件判斷邏輯是重復(fù)的,我們可以使用使用函數(shù)式接口和Lambda表達式,去除重復(fù)代碼,如下:

public class DataFilter {public List<Integer> filterNumbers(List<Integer> numbers, Predicate<Integer> predicate) {List<Integer> result = new ArrayList<>();for (Integer number : numbers) {if (predicate.test(number)) {result.add(number);}}return result;}
}

我們將過濾的核心邏輯抽象出來。該方法接受一個 Predicate函數(shù)式接口作為參數(shù),以便根據(jù)不同的條件來過濾數(shù)據(jù)。然后,我們可以使用Lambda表達式來傳遞具體的條件,這樣最終也達到去除重復(fù)代碼的效果啦.

http://www.risenshineclean.com/news/4500.html

相關(guān)文章:

  • 織夢裝修網(wǎng)站模板有域名有服務(wù)器怎么做網(wǎng)站
  • wordpress在哪兒打開企業(yè)網(wǎng)站seo優(yōu)化外包
  • 網(wǎng)站域名空間費發(fā)票廣告詞
  • 烏魯木齊哪里可以建設(shè)網(wǎng)站關(guān)鍵詞語有哪些
  • 用python做網(wǎng)站開發(fā)的課程嘉興seo
  • 網(wǎng)站點擊率如何做百度一下百度
  • wordpress文章顯示小時分鐘天津seo推廣服務(wù)
  • 東莞網(wǎng)站如何制作google play官網(wǎng)入口
  • 網(wǎng)站會員注冊系統(tǒng)下載在線看seo網(wǎng)站
  • 做網(wǎng)站都需要用到什么百度網(wǎng)址大全怎么設(shè)為主頁
  • 九江做網(wǎng)站開發(fā)需要多少錢班級優(yōu)化大師網(wǎng)頁版登錄
  • 外貿(mào)網(wǎng)站用什么語言百度廣告推廣平臺
  • 實體門店管理系統(tǒng)武漢seo公司排名
  • 如何查看網(wǎng)站在哪里做的會計培訓(xùn)班有用嗎
  • 獲獎設(shè)計網(wǎng)站怎么做ppt
  • 做網(wǎng)站需要材料北京專業(yè)網(wǎng)站優(yōu)化
  • 怎樣在設(shè)計網(wǎng)站做圖賺錢嗎投稿平臺
  • 青島網(wǎng)絡(luò)推廣選哪家seo推廣宣傳
  • 成都設(shè)計研究院北京seo產(chǎn)品
  • 蘇州網(wǎng)頁服務(wù)開發(fā)與網(wǎng)站建設(shè)鳳凰網(wǎng)臺灣資訊
  • 阿里云注銷網(wǎng)站什么是長尾關(guān)鍵詞舉例
  • 權(quán)威的網(wǎng)站建設(shè)網(wǎng)絡(luò)銷售是做什么的
  • 南寧網(wǎng)站建設(shè) 南寧聯(lián)達億seo優(yōu)化網(wǎng)站源碼
  • 現(xiàn)在怎么做網(wǎng)絡(luò)營銷推廣方法網(wǎng)站的優(yōu)化seo
  • 北京 做網(wǎng)站 公司微信公眾號小程序怎么做
  • 注重網(wǎng)站內(nèi)容維護百度推廣賬號怎么申請
  • 電子產(chǎn)品營銷策劃書seo優(yōu)化易下拉霸屏
  • 重慶網(wǎng)絡(luò)網(wǎng)站推廣網(wǎng)絡(luò)營銷公司排行榜
  • 開通網(wǎng)站必須做域名空間營銷方法有哪些方式
  • 怎么更改網(wǎng)站域名搜索引擎推廣實訓(xùn)