設計模式 工廠模式

2021-10-25 06:37:08 字數 1509 閱讀 8426

工廠模式屬於建立型模式

它提供了一種基於"輸入"的建立物件方式。

應用場景

超類存在多個子類時建立物件,

建立過程比較複雜的物件,適合使用工廠模式,

過程簡單,不需要使用工廠模式。

package com.superv.supervresource.design.factory;

/** * @author yangwei

* @describition 動物

* * @time 2023年10月26日-下午7:10:32

*/public

inte***ce

animal

package com.superv.supervresource.design.factory;

/** * @author yangwei

* @describition 羊

* * @time 2023年10月26日-下午7:13:45

*/public

class

sheep

implements

animal

}

package com.superv.supervresource.design.factory;

/** * @author yangwei

* @describition 貓

* * @time 2023年10月26日-下午7:12:45

*/public

class

catimplements

animal

}

package com.superv.supervresource.design.factory;

/** * @author yangwei

* @describition 動物工廠

* * @time 2023年10月26日-下午7:14:13

*/public

class

animalfactoryif(

"cat"

.equals

(animaltype)

)return null;

}}

package com.superv.supervresource.design.factory;

/** * @author yangwei

* @describition 工廠測試類

* * @time 2023年10月26日-下午7:16:34

不當之處,請予指正。

設計模式 工廠模式 抽象工廠模式

建立物件時不會對客戶暴露建立邏輯,並且通過使用乙個共同的介面來指向建立的物件。sept1 建立乙個公共介面,將要對外開放的方法在這裡定義。sept2 建立實現介面的類,用即實現對外開放的類的方法 sept3 建立工廠,提供乙個get方法,這個方法提供返回實現類的物件 建立選擇 sept4 使用,建立...

設計模式 工廠設計模式

用於建立物件的介面,交給子類去實現 我們舉乙個生產nokia的例子 public abstract class nokiaphone先試定義了乙個抽象類,抽象出方法poweronphone 模擬手機開機的動作 public class nokia5200 extends nokiaphone pub...

設計模式 工廠設計模式

工廠模式分為工廠方法模式和抽象工廠模式 工廠方法模式分為 普通工廠模式,就是建立乙個工廠類,對實現了同一介面的一些類進行例項的建立。多個工廠方法模式,是對普通工廠方法模式的改進,在普通工廠方法模式中,如果傳遞的字串出錯,則不能正確建立物件,而多個工廠方法模式是提供多個工廠方法,分別建立物件。靜態工廠...