zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    思考:工厂方法模式:http://www.cnblogs.com/maggiejyt/p/7561253.html

    工厂方法模式UML:

    问题:如果这家工厂不止要生产Ball(球),还要还有Sneakers(球鞋)等

    则UML图为

    当Product有多种类时则是抽象工厂模式

    代码(Ball的代码见简单工厂模式:http://www.cnblogs.com/maggiejyt/p/7561253.html

    Sneakers(球鞋抽象类)

    package com.maggie.FactoryMethod;
    
    public  abstract  class Sneakers {
         private String name;
    
        public Sneakers(String name) {
             super ();
             this .name = name;
        }
    
        public Sneakers() {
             super ();
        }
    
        public String getName() {
             return name;
        }
    
        public  void setName(String name) {
             this .name = name;
        }
        
        public  abstract  void show();
    
    }

    FootSneakers(足球鞋)

    package com.maggie.FactoryMethod;
    
    public  class FootSneakers extends Sneakers {
    
        @Override
        public  void show() {
            String show = "the sneakers is " + this .getName();
            System.out.println(show);
    
        }
    
        public FootSneakers() {
             super ();
        }
    
        public FootSneakers(String name) {
             super (name);
        }
    
    }

    BasketSneakers(篮球鞋)

    package com.maggie.FactoryMethod;
    
    public  class BasketSneakers extends Sneakers {
    
        @Override
        public  void show() {
            String show = "the sneakers is "+ this .getName();
            System.out.println(show);
        }
        public BasketSneakers() {
            super();
        }
    
        public BasketSneakers(String name) {
            super(name);
        }
    }

    IFactory(工厂接口)

    package com.maggie.FactoryMethod;
    
    public interface IFactory {
    public abstract Ball createBall();
    public abstract Sneakers createSneakers();
    }

    FootSneakers(足球工厂)

    package com.maggie.FactoryMethod;
    
    public class FootSneakers extends Sneakers {
    
        @Override
        public void show() {
            String show = "the sneakers is " + this.getName();
            System.out.println(show);
    
        }
    
        public FootSneakers() {
            super();
        }
    
        public FootSneakers(String name) {
            super(name);
        }
    
    }

     BasketFactory(篮球工厂)

    package com.maggie.FactoryMethod;
    
    public class BasketFactory implements IFactory {
    
        @Override
        public Ball createBall() {
            // TODO Auto-generated method stub
            return new BasketBall("oranger");
        }
    
        @Override
        public Sneakers createSneakers() {
            return new BasketSneakers("basketball sneakers");
        }
    
    }

    客户端调用

    package com.maggie.FactoryMethod;
    
    public class Client {
    
        public static void main(String[] args) {
            IFactory iFactory = null;
            
            iFactory = new FootFactory();
            Ball footBall = iFactory.createBall();
            Sneakers footSneakers = iFactory.createSneakers();
            footBall.play();
            footSneakers.show();
            
            iFactory = new BasketFactory();
            Ball basketBall = iFactory.createBall();
            Sneakers basketSneakers = iFactory.createSneakers();
            basketBall.play();
            basketSneakers.show();
            
            
        }
    
    }

    结果输出:

    the ball's color is white+black,I am FootBall
    the sneakers is football sneakers
    the ball's color is oranger,I am BasketBall
    the sneakers is basketball sneakers

    思考:

    每次增加一种产品类,就要改动各种Factory的代码。。。。。

    采用的是用简单工厂来改进抽象工厂

    代码实现

    SportsEquipmentFactory(采用简单工厂模式来改进)

    package com.maggie.FactoryMethod;
    
    public class SportsEquipmentFactory {
        private static final String STYLE = "basket";
        // private static final String STYLE="foot";只需要更改这里的代码配置
    
        public static Ball createBall() {
            Ball ball = null;
            switch (STYLE) {
            case "basket":
                ball = new BasketBall("oranger");
                break;
            case "foot":
                ball = new FootBall("white+black");
                break;
            }
            return ball;
        }
    
        public static Sneakers createSneakers() {
            Sneakers sneakers = null;
            switch (STYLE) {
            case "basket":
                sneakers = new BasketSneakers("basketball sneakers");
                break;
            case "foot":
                sneakers = new FootSneakers("football sneakers");
                break;
            }
            return sneakers;
        }
    }

    客户端调用

    package com.maggie.FactoryMethod;
    
    public class Client {
    
        public static void main(String[] args) {
            Ball ball = SportsEquipmentFactory.createBall();
            ball.play();
            Sneakers sneakers = SportsEquipmentFactory.createSneakers();
            sneakers.show();
    
        }
    
    }

    输出

    the ball's color is oranger,I am BasketBall
    the sneakers is basketball sneakers

    思考:如果业务又做大,工厂又需要生产网球类的产品,则需要在SportsEquipmentFactory的switch中增加case,这又违反了开放-封闭原则

    这时就要考虑反射。

  • 相关阅读:
    用Interface Builder自定义View
    UINavigationController
    pod install 失败
    关于@synchronized(self)的用法(转)
    杂记
    理解REST软件架构
    ios NSNotificationCenter消息注册与撤销
    UIApplication sharedApplication详细解释-IOS
    ios 数据持久化的三种方式
    '*.h' file not found
  • 原文地址:https://www.cnblogs.com/maggiejyt/p/7562507.html
Copyright © 2011-2022 走看看