See Design Patterns (IV. Simple factory pattern) from the king of glory

Look design pattern (a simple factory pattern) from the king of glory

I. Introduction

Before the start of the game, players can choose to play the hero of heroes from the free pool

II. Simple factory pattern

Simple factory pattern (Simple Factory Pattern) belong to the class of innovative models, also known as static factory method pattern (Static FactoryMethod Pattern), is defined by a specialized class is responsible for creating instances of other classes, instances are created usually have in common father.

  • Among these design principles are:
  1. There is a special class to be responsible for the process of creating the instance. Specifically, the product is a series looking at the collection of classes, these classes is an abstract class or interface of a derived object tree. Factory class and for generating a suitable object to meet customer requirements.
  2. Simple factory pattern contained in the respective roles and responsibilities are as follows:

    Plant role (Creator): This is a simple factory pattern of the core, which is responsible for all internal logic to create the class. Of course, the factory class must be able to be called outside world, to create products required for the object.
    Abstract (Product) Product character: the parent class of all objects created by a simple factory pattern, note that this parent can be an interface can also be a abstract class, which is responsible for describing the public interface common to all instances.
    Specific products (Concrete Product) Role: Specific examples of simple objects created by the factory, these specific products tend to have a common parent class.

  • The advantages of simple factory mode
    the user can factory class to create the required instance in the use, without the need to understand how these objects are created and how it is organized. Help to optimize the software architecture of the whole
  • Drawback simple factory pattern
  1. Since all logic in one factory class, resulting in a not very high cohesion, but also contrary to the "open closed principle"
  2. Simple factory pattern method are generally static, and static factory method is unable to make a subclass inherits, therefore, a simple factory pattern can not be formed based on inheritance tree of the base class.

III. Structure Figure

Structure chart

IV. FIG design class

Class Diagram

V. code implementation

HeroFactory (Simple factory class) to create an instance of the corresponding class in accordance with the players selected

package com.game.Factory;
/*
 * 简单工厂类(提供创建接口)
 */
import com.game.domain.Hero;
import com.game.domain.HouYi;
import com.game.domain.LuBanQiHao;

public class HeroFactory {
    public static Hero createHero(String HeroName) throws Exception {
        //传入HouYi,工厂创建并返回HouYi实例
        if(HeroName.equalsIgnoreCase("HouYi")) {
            return new HouYi();
        }else if(HeroName.equalsIgnoreCase("LuBanQiHao")){
        //传入LuBanQiHao,工厂创建并返回LuBanQiHao实例
            return new LuBanQiHao();
        }else {
            throw new Exception();//抛出异常
        }
    }
}

HeroFactoryTest test class

package com.game.test;
/*
 * 英雄工厂测试类
 */

import com.game.Factory.HeroFactory;
import com.game.domain.Hero;

public class HeroFactoryTest {
    public static void main(String [] args) {
        //使用工厂的创建工厂方法,传入英雄的名称,由工厂创建英雄
        try {
            //1.选择英雄后羿
            System.out.println("=======choose1=======");
            System.out.println("请选择你的英雄:");
            //向工厂传入英雄的名称
            Hero hero1 = HeroFactory.createHero("HouYi");
            System.out.println("您选择的英雄是【"+hero1.getHeroName()+"】");
            hero1.display();
            //2.选择英雄鲁班七号
            System.out.println("=======choose2========");
            System.out.println("请选择你的英雄:");
            //向工厂传入英雄的名称
            Hero hero2 = HeroFactory.createHero("LuBanQiHao");
            System.out.println("您选择的英雄是【"+hero2.getHeroName()+"】");
            hero2.display();
        }catch(Exception e) {//捕捉异常
            System.out.println("不存在此英雄!");
        }
    }
}

operation result
result

Guess you like

Origin www.cnblogs.com/miaowulj/p/11441577.html