av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

java語言

java工廠的方法是什么

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

java工廠的方法是什么

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

java工廠的方法是什么

  工廠方法概述

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

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

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

  缺點(diǎn)

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

  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 {// 抽象類 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(); }}

  工廠方法概述

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

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

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

  缺點(diǎn)

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

  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 {// 抽象類 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通過值傳遞參數(shù)的方法是什么11-06

Java是什么07-03

java虛方法09-21

java入門方法10-13

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

java方法重寫的方法分析09-04

java繼承是什么06-11