工厂模式

一、简单工厂模式

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

代码:

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 American

Hello,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 Japanese

Hello,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@e5146

Hello,I'm Japanese.Clothes:com.example.helloword.HeFu@a4323 Language:com.example.helloword.JapanLanguage@e5146
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值