关于工厂模式的总结(和体会)

设计模式之工厂模式

  1.简单工厂模式

    通过传递一个参数,是工厂决定返回那个对象的实例。

    

人类接口
1 package juntai.Factory.simply;
2 
3 public interface Human {
4     public void toSay();
5 
6     public void toColor();
7 }

 

人类接口的实现(黑人)
 1 package juntai.Factory.simply;
 2 
 3 public class BlackHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("黑人的皮肤是黑色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("黑人不用英語和普通話");
11     }
12 
13 }
人类接口的实现(白人)
 1 package juntai.Factory.simply;
 2 
 3 public class WithHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("白人的皮肤是白色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("白人用英语说话");
11     }
12 
13 }

 

人类工厂
 1 package juntai.Factory.simply;
 2 
 3 public class HumanFactory {
 4     public Human createHumary(String str) {
 5         Human man = null;
 6         if ("with".equals(str)) {
 7             man = new WithHuman();
 8         } else if ("black".equals(str)) {
 9             man = new BlackHuman();
10         }
11         return man;
12     }
13 }

 

测试
 1 package juntai.Factory.simply;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         HumanFactory factory = new HumanFactory();
 6         Human black = factory.createHumary("black");
 7         black.toColor();
 8         black.toSay();
 9     }
10 }

    如果现在要添加一个新的人种(黄种人),那么就需要改(人类工厂中的代码,添加一个逻辑分支),违背了设计模式的 开放封闭原则。那怎么解决这个问题引入(工厂方法模式)

 

  2. 工厂方法模式

    定义工厂方法模式:定义一个创建类的接口,用子类来决定实例化那个那类。是一个类的实例化延迟到其子类。

 

人类接口
1 package juntai.Factory.simply;
2 
3 public interface Human {
4     public void toSay();
5 
6     public void toColor();
7 }
人类接口的实现(黑人)
 1 package juntai.Factory.simply;
 2 
 3 public class BlackHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("黑人的皮肤是黑色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("黑人不用英語和普通話");
11     }
12 
13 }
人类接口的实现(白人)
 1 package juntai.Factory.simply;
 2 
 3 public class WithHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("白人的皮肤是白色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("白人用英语说话");
11     }
12 
13 }
人类接口的实现(黄种人)
 1 package juntai.Factory.Function;
 2 
 3 public class YellowHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("黄种人的皮肤是黄色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("黄种人用普通话说话");
11     }
12 }

 

工厂接口
1 package juntai.Factory.Function;
2 
3 public interface HumanFactory {
4     public Human createHumary();
5 }
生产黑人工厂
 1 package juntai.Factory.Function;
 2 
 3 public class BlackFactoryImpl implements HumanFactory {
 4 
 5     public Human createHumary() {
 6         Human man = new BlackHuman();
 7         return man;
 8     }
 9 
10 }
生产白人工厂
 1 package juntai.Factory.Function;
 2 
 3 public class WithFactoryImpl implements HumanFactory {
 4 
 5     public Human createHumary() {
 6         Human man = new WithHuman();
 7         return man;
 8     }
 9 
10 }
生产黄种人工厂
 1 package juntai.Factory.Function;
 2 
 3 public class YellowFactoryImpl implements HumanFactory {
 4 
 5     public Human createHumary() {
 6         Human man = new YellowHuman();
 7         return man;
 8     }
 9 
10 }
测试
 1 package juntai.Factory.Function;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         HumanFactory factory = new YellowFactoryImpl();
 6         Human yellow = factory.createHumary();
 7         yellow.toColor();
 8         yellow.toSay();
 9         
10     }
11 }

    这样可以解决,简单工厂的问题。现在我要给人类添加一个性别方法。那么这个模式就要违背 (开放封闭原则了),引入 抽象工厂。

 

  3.抽象工厂模式

    

人类接口
1 package juntai.Factory.abstract1;
2 
3 public interface Human {
4     public void toSay();
5 
6     public void toColor();
7     
8     public void getSex();
9 }
黑人抽象类
 1 package juntai.Factory.abstract1;
 2 
 3 public abstract class BlackHuman implements Human {
 4 
 5     public void toColor() {
 6         System.out.println("黑人的皮肤是黑色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("黑人不用英語和普通話");
11     }
12 
13 }
白人抽象类
 1 package juntai.Factory.abstract1;
 2 
 3 public abstract class WithHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("白人的皮肤是白色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("白人用英语说话");
11     }
12 
13 }
黄种人抽象类
 1 package juntai.Factory.abstract1;
 2 
 3 public abstract class YellowHuman implements Human{
 4 
 5     public void toColor() {
 6         System.out.println("黄种人的皮肤是黄色的");
 7     }
 8 
 9     public void toSay() {
10         System.out.println("黄种人用普通话说话");
11     }
12 
13 }
黑人男性类
1 package juntai.Factory.abstract1;
2 
3 public class BlackManHuman extends BlackHuman{
4 
5     public void getSex() {
6         System.out.println("男性黑人");
7     }
8 
9 }
黑人女性类
1 package juntai.Factory.abstract1;
2 
3 public class BlackWomanHuman extends BlackHuman{
4 
5     public void getSex() {
6         System.out.println("女性黑人");
7     }
8 
9 }
白人男性
1 package juntai.Factory.abstract1;
2 
3 public class WithManHuman extends WithHuman {
4 
5     public void getSex() {
6         System.out.println("男性白人");
7     }
8 
9 }
白人女性类
1 package juntai.Factory.abstract1;
2 
3 public class WithWomanHuman extends WithHuman {
4 
5     public void getSex() {
6         System.out.println("女性白人");
7     }
8 
9 }
黄种人男性类
1 package juntai.Factory.abstract1;
2 
3 public class YellowManHuman extends YellowHuman {
4 
5     public void getSex() {
6         System.out.println("男性黄种人");
7     }
8 
9 }
黄种人女性类
1 package juntai.Factory.abstract1;
2 
3 public class YellowWomanHuman extends YellowHuman {
4 
5     public void getSex() {
6         System.out.println("女性黄种人");
7     }
8 
9 }

 

工厂接口
1 package juntai.Factory.abstract1;
2 
3 public interface HumanFactory {
4     public Human createYellowHuman();
5 
6     public Human createBlackHuman();
7 
8     public Human createWithHuman();
9 }
人类男性工厂
 1 package juntai.Factory.abstract1;
 2 
 3 public class HumanManFactory implements HumanFactory {
 4 
 5     public Human createBlackHuman() {
 6         Human human = new BlackManHuman();
 7         return human;
 8     }
 9 
10     public Human createWithHuman() {
11         Human human = new WithManHuman();
12         return human;
13     }
14 
15     public Human createYellowHuman() {
16         Human human = new YellowManHuman();
17         return human;
18     }
19 
20 }
人类女性工厂
 1 package juntai.Factory.abstract1;
 2 
 3 public class HumanWomanFactory implements HumanFactory {
 4     public Human createBlackHuman() {
 5         Human human = new BlackWomanHuman();
 6         return human;
 7     }
 8 
 9     public Human createWithHuman() {
10         Human human = new WithWomanHuman();
11         return human;
12     }
13 
14     public Human createYellowHuman() {
15         Human human = new YellowWomanHuman();
16         return human;
17     }
18 
19 }

 

 

测试类
 1 package juntai.Factory.abstract1;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         HumanFactory factory = new HumanManFactory();
 6         Human yellow = factory.createYellowHuman();
 7 
 8         yellow.toColor();
 9         yellow.toSay();
10         yellow.getSex();
11     }
12 }

 

 

posted @ 2012-04-25 13:37  嘎空间  阅读(172)  评论(0编辑  收藏  举报