亚洲一级免费看,特黄特色大片免费观看播放器,777毛片,久久久久国产一区二区三区四区,欧美三级一区二区,国产精品一区二区久久久久,人人澡人人草

java語(yǔ)言

java工廠的方法是什么

時(shí)間:2025-04-29 15:17:53 java語(yǔ)言 我要投稿

java工廠的方法是什么

  創(chuàng)建新對(duì)象最簡(jiǎn)單的辦法是使用new關(guān)鍵字和具體類(lèi)。只有在某些場(chǎng)合下,創(chuàng)建和維護(hù)對(duì)象工廠所帶來(lái)的額外復(fù)雜性才是物有所值。以下是小編為大家搜索整理java工廠的方法是什么,希望能給大家?guī)?lái)幫助!更多精彩內(nèi)容請(qǐng)及時(shí)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

java工廠的方法是什么

  工廠方法概述

  工廠方法模式中抽象工廠類(lèi)負(fù)責(zé)定義創(chuàng)建對(duì)象的接口,具體對(duì)象的創(chuàng)建工作由繼承抽象工廠的具體類(lèi)實(shí)現(xiàn)。

  優(yōu)點(diǎn)

  客戶(hù)端不需要在負(fù)責(zé)對(duì)象的創(chuàng)建,從而明確了各個(gè)類(lèi)的職責(zé),如果有新的對(duì)象增加,只需要增加一個(gè)具體的類(lèi)和具體的工廠類(lèi)即可,不影響已有的代碼,后期維護(hù)容易,增強(qiáng)了系統(tǒng)的擴(kuò)展性

  缺點(diǎn)

  需要額外的編寫(xiě)代碼,增加子工作量

  class IntegerDemo { public static void main(String[] args) { Factory factory = new DogFactory(); Animal animal = factory.createAnimal(); animal.eat(); factory = new CatFactory(); animal = factory.createAnimal(); animal.eat(); }}abstract class Animal {// 抽象類(lèi) public abstract void eat();}class Dog extends Animal {// 狗 public void eat() { System.out.println("a dog is eatting."); }}class Cat extends Animal {// 貓 public void eat() { System.out.println("a cat is eatting."); }}interface Factory {// 接口 public abstract Animal createAnimal();}class DogFactory implements Factory {// 實(shí)現(xiàn)接口 public Animal createAnimal() { return new Dog(); }}class CatFactory implements Factory {// 實(shí)現(xiàn)接口 public Animal createAnimal() { return new Cat(); }}

  工廠方法概述

  工廠方法模式中抽象工廠類(lèi)負(fù)責(zé)定義創(chuàng)建對(duì)象的接口,具體對(duì)象的創(chuàng)建工作由繼承抽象工廠的具體類(lèi)實(shí)現(xiàn)。

  優(yōu)點(diǎn)

  客戶(hù)端不需要在負(fù)責(zé)對(duì)象的創(chuàng)建,從而明確了各個(gè)類(lèi)的職責(zé),如果有新的對(duì)象增加,只需要增加一個(gè)具體的類(lèi)和具體的工廠類(lèi)即可,不影響已有的代碼,后期維護(hù)容易,增強(qiáng)了系統(tǒng)的擴(kuò)展性

  缺點(diǎn)

  需要額外的編寫(xiě)代碼,增加子工作量

  public class IntegerDemo { public static void main(String[] args) { Factory factory = new DogFactory(); Animal animal = factory.createAnimal(); animal.eat(); factory = new CatFactory(); animal = factory.createAnimal(); animal.eat(); }}abstract class Animal {// 抽象類(lèi) public abstract void eat();}class Dog extends Animal {// 狗 public void eat() { System.out.println("a dog is eatting."); }}class Cat extends Animal {// 貓 public void eat() { System.out.println("a cat is eatting."); }}interface Factory {// 接口 public abstract Animal createAnimal();}class DogFactory implements Factory {// 實(shí)現(xiàn)接口 public Animal createAnimal() { return new Dog(); }}class CatFactory implements Factory {// 實(shí)現(xiàn)接口 public Animal createAnimal() { return new Cat(); }}

【java工廠的方法是什么】相關(guān)文章:

關(guān)于java工廠的方法是什么09-01

java垃圾回收的方法是什么07-31

java通過(guò)值傳遞參數(shù)的方法是什么11-06

Java是什么07-03

java虛方法09-21

java入門(mén)方法10-13

java調(diào)用的方法09-04

java方法重寫(xiě)的方法分析09-04

java繼承是什么06-11