Home > Java > Java Tutorial > body text

Introduction and use of factory pattern design patterns

零下一度
Release: 2017-07-03 11:31:18
Original
1532 people have browsed it

Simple factory pattern

The simple factory pattern is a class creation pattern, also called the static factory method pattern. The simple factory pattern uses a factory object to determine which product class instances to produce.

Why use the simple factory pattern

The reason is simple: decoupling.

LOL scene analysis:

There are currently more than 100 heroes in LOL, and the skills of each character are all different. The code implementation of specific heroes must be Different;

But the skills of each hero are the four basic skills of Q, W, E, R, and the summoner skills D and F;

Although the selected heroes are different, The rest of the game should be exactly the same, it's impossible to completely change the logic of other common parts depending on which hero we choose!

How to implement such an application scenario

Summoner skill constant class

public class SummonerSkillName {  
  public static final String FLASH = "Flash";//闪现  public static final String HEAL = "Heal";//治疗  public static final String IGNITE = "Ignite";//引燃  public static final String REVIVE = "Revive";//惩戒  public static final String TELEPORT = "Teleport";//传送  public static final String EXHAUST = "ExhaustSS";//虚弱
  
}
Copy after login

Summoner Skill Interface

public interface SummonerSkill {  
  void release();//释放技能
  
}
Copy after login

Summoner Skill Implementation Class 1: Flash

public class FlashSS implements SummonerSkill {  
  public static final String NAME = "闪现";
  
  @Override  public void release() {
    System.out.println("闪现");
  }
  
}
Copy after login

Summoner Skill Implementation Category 2: Ignite

public class IgniteSS implements SummonerSkill {  public static final String NAME = "引燃";

  @Override  public void releaseSS() {
    System.out.println("引燃");
  }

}
Copy after login

Summoner Skill Factory

public class SummonerSkillFactory {  
  public static SummonerSkill getSkillSS(String ssName) throws Exception {
    
    SummonerSkill ss;if (ssName.equals(SummonerSkillName.FLASH)) {
      ss = new FlashSS();
    } else if (ssName.equals(SummonerSkillName.TELEPORT)) {
      ss = new TeleportSS();
    } else if (ssName.equals(SummonerSkillName.HEAL)) {
      ss = new HealSS();
    } else if (ssName.equals(SummonerSkillName.IGNITE)) {
      ss = new IgniteSS();
    } else if (ssName.equals(SummonerSkillName.EXHAUST)) {
      ss = new ExhaustSS();
    } else {
      ss = new ReviveSS();
    }    return ss;
  }
  
}
Copy after login

Improved factory, using reflection:

public class SummonerSkillFactory {  
  private static final String CLASS_NAME_SUFFIX = "SS";  
  public static SummonerSkill getSkillSS(String ssName) throws Exception {

    String className = ssName + classNameSuffix;
    String packageName = SummonerSkill.class.getPackage().getName();
    SummonerSkill ss = (SummonerSkill) Class.forName(packageName + "." + className).newInstance();    return ss;
  }
  
}
Copy after login

Imagine how beautiful the picture would be if more than 100 heroes also used if else~

The advantage of this is not only that the amount of code writing is much smaller, but also assuming that a new summoner is added In terms of technology, the factory code does not need to be changed, and the opening and closing principle is followed.

public class LeagueClient {
  
  @Test  public void selectHero() throws Exception {
    SummonerSkill flash = SummonerSkillFactory.getSkillSS(SummonerSkillName.FLASH);
    SummonerSkill ignite = SummonerSkillFactory.getSkillSS(SummonerSkillName.IGNITE); 
}
Copy after login

The focus of the simple factory pattern or factory pattern is not on how to produce the required classes in the factory, It's about separating the creation of products from the consumption of products.

I have used if...else if...else and reflection before. In addition to these methods, there are other ways to create products, such as passing in the identification of a specific product and going to the database based on this identification. Inquire inside.

Advantages and Disadvantages of Factory Pattern

Advantages:

1. Simply optimize the software architecture and clarify their respective functions Responsibilities and rights of the module

2. Through the factory class, the outside world does not need to directly create specific product objects. It only needs to be responsible for consumption and does not need to care about how to create objects internally

Disadvantages:

1. All the creation logic of the simple factory pattern before improvement is concentrated in one factory class. The classes that can be created can only be considered. If you need to add a new class, you must change the factory class

2. The simple factory model before improvement. As the number of specific products continues to increase, there may be a need to create different instances of the common class according to different conditions. This judgment of conditions and the judgment of specific product types are intertwined, making it difficult to Avoid the spread of functional modules, which is detrimental to system maintenance and expansion

3. The improved simple factory mode mainly uses reflection and the efficiency will be lower

The above is the detailed content of Introduction and use of factory pattern design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!