工厂模式

来源:互联网 发布:程序员三年薪资2017 编辑:程序博客网 时间:2024/06/17 00:48

一、简单工厂模式

定义:一个工厂类根据不同的参数创建不同的实体对象。

代码:

IPerson.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public interface IPerson {    public void sayHello();}

Japanese.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Japanese implements IPerson{    @Override    public void sayHello() {        System.out.println("Hello,I'm Jspanese");    }}

Canadian.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Canadian implements IPerson{    @Override    public void sayHello() {        System.out.println("Hello,I'm Canadian");    }}

American.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class American implements IPerson {    @Override    public void sayHello() {        System.out.println("Hello,I'm American");    }}

Chinese.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Chinese implements IPerson {    @Override    public void sayHello() {        System.out.println("Hello,I'm Chinese");    }}

PersonFactory.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class PersonFactory {    public static IPerson creatPerson(String type){        IPerson person=null;        if (type.equals("American")){            person=new American();        }else if (type.equals("Chinese")){            person=new Chinese();        }        return person;    }}

Test.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Test {    public static void main(String[] args){        IPerson person=null;        person=PersonFactory.creatPerson("American");        person.sayHello();        person=PersonFactory.creatPerson("Chinese");        person.sayHello();    }}

输出

Hello,I'm AmericanHello,I'm Chinese

二、工厂方法模式

定义:一个抽象工厂类定义创建的方法,具体工厂类实现创建的功能

代码:

IPersonFactory.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public interface IPersonFactory {    public IPerson creatPerson(String type);}

AsiaPersonFactory.java

package com.example.helloworld;/**亚洲人工厂类 * Created by Administrator on 2016/8/18. */public class AsiaPersonFactory implements IPersonFactory{    @Override    public IPerson creatPerson(String type) {        IPerson person=null;        if (type.equals("Chinese")){            person=new Chinese();        }else if (type.equals("Japanese")){            person=new Japanese();        }        return person;    }}

NorthAmericaPersonFactory.java

package com.example.helloworld;/**北美洲人工厂类 * Created by Administrator on 2016/8/18. */public class NorthAmericaPersonFactory implements IPersonFactory{    @Override    public IPerson creatPerson(String type) {        IPerson person=null;        if (type.equals("American")){            person=new American();        }else if(type.equals("Canadian")){            person=new Canadian();        }        return person;    }}

Test.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Test {    public static void main(String[] args){        IPerson person=null;        AsiaPersonFactory asiaPersonFactory=new AsiaPersonFactory();        person=asiaPersonFactory.creatPerson("Japanese");        person.sayHello();        NorthAmericaPersonFactory northAmericaPersonFactory=new NorthAmericaPersonFactory();        person=northAmericaPersonFactory.creatPerson("Canadian");        person.sayHello();    }}

输出:

Hello,I'm JapaneseHello,I'm Canadian

三、抽象工厂模式

定义:提供一个接口,使在不指定特定产品的情况下,创建多个产品族中的产品对象。

需求:为每个person类提供一种语言,一件服饰。

IOther.java (定义一个工厂接口,定义创建对象的方法)

package com.example.helloworld;/** * Created by Administrator on 2016/8/20. */public interface Iother {    public IClothes creatClothes();//提供服饰    public ILanguage creatLanguage();//提供语言}

ChinaOther.java (创建中国人的语言,服饰)

package com.example.helloworld;/** * Created by Administrator on 2016/8/20. */public class ChinaOther implements Iother{    @Override    public IClothes creatClothes() {        return new HanFu();    }    @Override    public ILanguage creatLanguage() {        return new ChinaLanguage();    }}

JapanOther.java(创建日本人的语言,服饰)

package com.example.helloworld;/** * Created by Administrator on 2016/8/20. */public class JapanOther implements Iother{    @Override    public IClothes creatClothes() {        return new HeFu();    }    @Override    public ILanguage creatLanguage() {        return new JapanLanguage();    }}

Chinese.java(构造方法里面,传入创建的工厂类)

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Chinese implements IPerson {    private Clothes clothes;    private Language language;    public Chinese(Iother iother){        clothes=iother.creatClothes();        language=iother.creatLanguage();    }    @Override    public void sayHello() {        System.out.println("Hello,I'm Chinese.Clothes:"+clothes+"Language:"+language);    }}

Japanese.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Japanese implements IPerson{    private Clothes clothes;    private Language language;    public Japanese(Iother iother){        clothes=iother.creatClothes();        language=iother.creatLanguage();    }    @Override    public void sayHello() {        System.out.println("Hello,I'm Jspanese.Clothes:\"+clothes+\"Language:\"+language");    }}

Test.java

package com.example.helloworld;/** * Created by Administrator on 2016/8/18. */public class Test {    public static void main(String[] args){        IPerson person=null;        Iother other=null;        other=new ChinaOther();        person=new Chinese(other);        person.sayHello();        other=new JapanOther();        person=new Japanese(other);        person.sayHello();    }}

输出:

Hello,I'm Chinese.Clothes:com.example.helloword.HanFu@a4323 Language:com.example.helloword.ChinaLanguage@e5146Hello,I'm Japanese.Clothes:com.example.helloword.HeFu@a4323 Language:com.example.helloword.JapanLanguage@e5146
0 0
原创粉丝点击