Java基礎 簡單原廠模式

來源:互聯網
上載者:User

標籤:思考   ges   package   感恩   項目開發   equal   wow   2-2   pack   

禮悟:
           好好學習多思考,尊師重道存感恩。葉見尋根三返一,活水清源藏於零。
           虛懷若穀良心主,皓月當空自在王。願給最苦行無悔,誠勸且行且珍惜。



    os :windows7 x64
    jdk:jdk-8u131-windows-x64
    ide:Eclipse Oxygen Release (4.7.0)



package blog.jizuiku8;/** * 能發出聲音 *  * @author jizuiku * @version V17.09.27 */public interface Toy {void talk();}

 

package blog.jizuiku8;/** * 玩具小貓 *  * @author jizuiku * @version V17.09.27 */public class Cat implements Toy {@Overridepublic void talk() {// TODO Auto-generated method stubSystem.out.println("Meow");}}

 

package blog.jizuiku8;/** * 玩具-小狗 *  * @author jizuiku * @version V17.09.27 */public class Dog implements Toy {@Overridepublic void talk() {// TODO Auto-generated method stubSystem.out.println("Wow");}}

 

package blog.jizuiku8;/** * 玩具工廠 *  * @author jizuiku * @version V17.09.27 */public class ToyFactory {/** * @param type a string.For example "Dog" and "Cat". *             * @return Get object of the type */public Toy getToy(String type) {// Write your code hereif ("Dog".equals(type)) {return new Dog();} else if ("Cat".equals(type)) {return new Cat();} else {return null;}}}

 

package blog.jizuiku8;/** *  *  * @author jizuiku * @version V17.09.27 */public class Demo {public static void main(String[] args) {ToyFactory factory = new ToyFactory();Toy dog = factory.getToy("Dog");if (dog != null) {dog.talk();} else {System.out.println("建立對象失敗");}Toy cat = factory.getToy("Cat");if (cat != null) {cat.talk();} else {System.out.println("建立對象失敗");}}}

 

結果



Java優秀,值得學習。
設計模式中蘊含著極高的智慧,對項目開發有大協助。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。

Java基礎 簡單原廠模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.