I want to fly higher
programming Explorer
posts - 114,comments - 263,trackbacks - 0
     工厂模式主要是为创建对象提供了接口。主要包括三类:简单工厂模式,工厂方法模式和抽象工厂模式。

       1.简单工厂模式:模式很简单,使用在业务较简单的情况。由三种角色构成,分别是工厂类角色抽象产品角色以及具体产品角色。其中工厂类是核心,包含一定的逻辑判断,而抽象产品角色是由一个抽象类或接口实现,而具体产品则是对抽象产品的实现。
    下面写了一个程序实现简单工厂模式:其中抽象产品角色为一个接口Industry.java(行业),具体产品角色为两个ITIndustry.javaIT行业)和FinanceIndustry.java(金融行业),工厂类为IndustryFactory.java,最后是一个测试类SimpleFactoryTest.java
Industry.java:

 1package com.landon.trainjava.oop;
 2/**
 3 * 一个普通的接口,行业
 4 * @author landon
 5 *
 6 */

 7public interface Industry {
 8 void getIndustry();
 9
10}

11
12


ITIndustry.java

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 简单工厂模式中的具体产品-IT行业
 7 * @author landon
 8 *
 9 */

10public class ITIndustry implements Industry{
11 public void getIndustry()
12 {
13  System.out.println("My industry is IT!");
14 }

15
16}

17
18


FinanceIndustry.java

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 简单工厂模式中的具体产品-金融行业
 7 * @author landon
 8 *
 9 */

10public class FinanceIndustry implements Industry{
11 public void getIndustry()
12 {
13  System.out.println("My industry is Finance!");
14 }

15}

16
17


IndustryFactory.java

 1package com.landon.trainjava.designpattern;
 2
 3import com.xpec.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 简单工厂模式中的工厂类-行业管理工厂
 7 * 简单工厂模式的弊端在于每增加一个行业,都需要在工厂类中添加相应的逻辑,所以需要引入工厂方法模式
 8 * @author landon
 9 *
10 */

11public class IndustryFactory {
12 public Industry createIndustry(String type)
13 {
14  if(type.equals("IT"))
15  {
16   return (new ITIndustry());
17  }

18  else if(type.equals("Finance"))
19  {
20   return (new FinanceIndustry());
21  }

22  else
23  {
24   return null;
25  }

26 }

27
28}

29
30


SimpleFactoryTest.java

 1package com.landon.trainjava.designpattern;
 2/**
 3 * 简单工厂模式的测试
 4 * @author landon
 5 *
 6 */

 7public class SimpleFactoryTest {
 8 public static void main(String[] args) {
 9  IndustryFactory factory = new IndustryFactory();
10  
11  factory.createIndustry("IT").getIndustry();
12  factory.createIndustry("Finance").getIndustry();
13 }

14
15}

16
17

 

    2工厂方法模式简单工厂模式的弊端在于每增加一个行业,都需要在工厂类中增加相应的逻辑,所以需要引入工厂方法模式。工厂方法模式与简单工厂模式不同的是将原有的工厂类划分为抽象工厂和具体工厂。其中抽象工厂提供了一个接口,具体工厂负责实现对应的具体的产品。    
    
下面写了一个程序实现抽象方法模式:其中抽象工厂为IndustryFactoryInterface.java,包括两个具体工厂ITIndustryFactory.javaFinanceIndusryFactory.java,具体的产品还是ITIndustry.javaFinanceIndustry.java另外还有一个测试类FactoryMethodTest.java

IndustryFactoryInterface.java

    

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/*
 6 * 工厂方法模式中的抽象工厂角色
 7 */

 8public interface IndustryFactoryInterface {
 9 public Industry createIndustry();
10
11}

12
13


ITIndustryFactory.java:

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 工厂方法模式中的具体工厂角色-IT行业工厂
 7 * @author lvwenyong
 8 *
 9 */

10public class ITIndustryFactory implements IndustryFactoryInterface{
11 public Industry createIndustry ()
12 {
13  return new ITIndustry();
14 }

15
16}

17
18


FinanceIndustry.java:

    

 

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 工厂方法模式中的具体工厂角色-金融行业工厂
 7 * @author landon
 8 *
 9 */

10public class FinanceIndustryFactory implements IndustryFactoryInterface{
11 
12 public Industry createIndustry() 
13 {
14  return (new FinanceIndustry());
15 }

16
17}

18
19


    具体的产品类同上简单工厂模式。
FactoryMethodTest.java:

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 工厂方法模式测试
 7 * @author landon
 8 *
 9 */

10public class FactoryMethodTest {
11 public static void main(String[] args) {
12  //首先得到具体的工厂,然后生产
13  IndustryFactoryInterface factoryInterface1 = new ITIndustryFactory();
14  Industry itIndustry = factoryInterface1.createIndustry();
15  
16  itIndustry.getIndustry();
17  
18  IndustryFactoryInterface factoryInterface2 = new FinanceIndustryFactory();
19  Industry financeIndustry = factoryInterface2.createIndustry();
20  
21  financeIndustry.getIndustry();
22
23 }

24
25}
     
26
27


    3抽象工厂模式抽象工厂模式是一种比工厂模式抽象程度更高的模式。在工厂方法模式中,一个具体的工厂类负责创建一个单独的产品,如果有两个不同的产品要创建,就需要两个不同的工厂类,即使这两个产品有某些必要联系,同样还是需要两个不同的工厂类。
    
所以说工厂方法模式是针对于一个产品系列的,而抽象工厂模式是针对多个产品系列的。即工厂方法模式是一个产品一个工厂类,而抽象工厂模式是多个产品系列一个工厂类。
    
下面写了一个程序实现抽象工厂模式:其中包括抽象工厂AbstractFactory.java,负责创建行业和公司。还包括两个具体工厂FactoryA.javaFactoryB.java。另外为了配合使用抽象工厂模式,又创建了一个接口Company.java,并有两个具体的MicosoftCompany.javaGoldmanCompany.java。最后还有一个测试类AbstractFactory.java
AbstractFactory.java

 1package com.landon.trainjava.designpattern;
 2
 3import com.landon.trainjava.oop.Industry;
 4
 5/**
 6 * 抽象工厂中模式中的抽象工厂
 7 * @author landon
 8 *
 9 */

10public interface AbstractFatory {
11 public Industry createIndustry();
12 public Company createCompany();
13
14}

15
16
17
18
19

 

FactoryA.java:

 1package com.landon.trainjava.designpattern;
 2import com.landon.trainjava.oop.*;
 3
 4/**
 5 * 抽象工厂模式中的具体工厂
 6 * @author landon
 7 *
 8 */

 9public class FactoryA implements AbstractFatory{
10 public Industry createIndustry()
11 {
12  return new ITIndustry();
13 }

14 
15 public Company createCompany()
16 {
17  return new MicrosoftCompany();
18 }

19}

20
21
22
23FactoryB.java:
24
25package com.landon.trainjava.designpattern;
26
27import com.landon.trainjava.oop.Industry;
28
29/**
30 * 抽象工厂模式中的具体工厂
31 * @author landon
32 *
33 */

34public class FactoryB implements AbstractFatory{
35 public Industry createIndustry()
36 {
37  return new FinanceIndustry();
38 }

39 
40 public Company createCompany()
41 {
42  return new GoldmanCompany();
43 }

44
45}

46
47


Company.java:
    

 1package com.landon.trainjava.designpattern;
 2/**
 3 * 一个接口,公司:为了配合使用抽象工厂模式
 4 * 作为抽象产品
 5 * @author landon
 6 *
 7 */

 8public interface Company {
 9 void getCompany();
10}

11


MicosoftCompany.java:

 1package com.landon.trainjava.designpattern;
 2/**
 3 * 抽象工厂模式中的一个具体产品:微软
 4 */

 5public class MicrosoftCompany implements Company{
 6 public void getCompany()
 7 {
 8  System.out.println("My company is Mircosoft!");
 9 }

10
11}

12


GoldmanCompany.java:

 1package com.landon.trainjava.designpattern;
 2/**
 3 * 抽象工厂模式中的一个具体产品:高盛
 4 */

 5public class GoldmanCompany implements Company{
 6 public void getCompany()
 7 {
 8  System.out.println("My company is Goldman Sachs!");
 9 }

10
11}

12
13


AbstractFactoryTest.java:

 1package com.landon.trainjava.designpattern;
 2/**
 3 * 抽象工厂模式测试
 4 * @author landon
 5 *
 6 */

 7public class AbstractFactoryTest {
 8
 9 public static void main(String[] args)
10 {
11  FactoryA fa = new FactoryA();
12  FactoryB fb = new FactoryB();
13  
14  fa.createIndustry().getIndustry();
15  fa.createCompany().getCompany();
16  
17  fb.createIndustry().getIndustry();
18  fb.createCompany().getCompany();
19 }

20
21}

22
23

 

    ITIndustry.java和FinaceIndustry.java同上面。

posted on 2010-07-07 23:49 landon 阅读(2394) 评论(0)  编辑  收藏 所属分类: Program

只有注册用户登录后才能发表评论。


网站导航: