Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the java Abstract Factory pattern

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to implement the java Abstract Factory pattern". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The abstraction factory implements only one method of createHuman, which is designed to simplify the code workload of the implementation class, as you will say when talking about code. A new feature of Jdk 1.5, the Enum type, is also used here. In fact, this can be realized by static variables of the class, but I think since you are learning, you should learn something. Even if you know this pattern very well, you may not have used the Enum type, which is a different knowledge point. I hope to explain to you that every time a new technical point is put forward, it will be enough for everyone to gain a little bit. Then when you use it in a specific project, you know that there is this technical point, and then you can solve the problem by going to the baidu dog.

Let's look at the program implementation:

Package com.cbf4life;/*** defines a general term for human beings. The problem is that I forgot to define gender when I just defined it. * this important issue must be modified, otherwise there are too many things in the world that do not exist * / public interface Human {/ / first define what is human / / people are happy, will laugh, originally intended to use smile to express, think about laugh is more appropriate, for a long time did not laugh Public void laugh (); / / Human still cries, representing pain public void cry (); / / Human can speak public void talk (); / / defines gender public void sex ();}

The human interface is defined, and then three abstract classes, that is, three product levels, are created according to the interface, and the three methods laugh (), cry () and talk () are implemented. Take AbstractYellowHuman as an example:

Why would package com.cbf4life.yellowHuman;import com.cbf4life.Human;/*** be changed to an abstract class? To define gender * / public abstract class AbstractYellowHuman implements Human {public void cry () {System.out.println ("yellow people cry");} public void laugh () {System.out.println ("yellow people laugh, happy!") ;} public void talk () {System.out.println ("Yellow people can talk, usually double bytes");}}

The other two abstract classes, AbstractWhiteHuman and AbstractgBlackHuman, have similar transaction methods that no longer copy the code throughout.

All three abstract classes have been implemented, and then there are some implementation classes. Actually, do you think there is any point in putting abstract classes here? You are not allowed to new an abstract object, which can be completely replaced by a non-abstract class. Hehe, each has its own way of killing pigs and tails, but since you enter the Java door, you have to abide by the rule of Java. Let's look at the implementation class:

The realization class of the female yellow race:

Package com.cbf4life.yellowHuman;/*** female yellow race * / public class YellowFemaleHuman extends AbstractYellowHuman {public void sex () {System.out.println ("the sex of the yellow race is female");}}

The implementation class of the male yellow race:

Package com.cbf4life.yellowHuman;/*** male yellow race * / public class YellowMaleHuman extends AbstractYellowHuman {public void sex () {System.out.println ("the sex of the yellow race is male");}}

By the same token, female Caucasian, male Caucasian, female black, male black are all the realization of gender.

The product level and product family in the abstract factory mode have been completed, that is, human beings and the generated human beings have been defined, and the next step is waiting for the factory to be built, so let's look at the factory class. Before we look at the factory class, let's look at that enumerated type, which is very interesting.

Package com.cbf4life / * make a list of the types of people in the world * JDK 1.5 began to introduce the enum type for the purpose. Attract C programmers to turn around * / public enum HumanEnum {/ / define all types of people in the world YelloMaleHuman ("com.cbf4life.yellowHuman.YellowMaleHuman"), YelloFemaleHuman ("com.cbf4life.yellowHuman.YellowFemaleHuman"), WhiteFemaleHuman ("com.cbf4life.whiteHuman.WhiteFemaleHuman"), WhiteMaleHuman ("com.cbf4life.whiteHuman.WhiteMaleHuman"), BlackFemaleHuman ("com.cbf4life.blackHuman.BlackFemaleHuman"), BlackMaleHuman ("com.cbf4life.blackHuman.BlackMaleHuman") Private String value = ""; / / defines the constructor to match private HumanEnum (String value) of type Data (value) {this.value = value;} public String getValue () {return this.value;}}

Then, let's look at our factory class and look at the interface first:

Package com.cbf4life;/*** this time set an interface, should be to create people of different genders, need different production lines * then this gossip stove must be able to make men and women * / public interface HumanFactory {/ / make a yellow race public Human createYellowHuman (); / make a white race public Human createWhiteHuman (); / make a black race public Human createBlackHuman () } then look at the abstract class: package com.cbf4life.humanFactory;import com.cbf4life.Human;import com.cbf4life.HumanEnum;import com.cbf4life.HumanFactory;public abstract class AbstractHumanFactory implements HumanFactory {/ * * given a gender race, the technical term for creating a human is to generate a product grade * / protected Human createHuman (HumanEnum humanEnum) {Human human = null / / if the passed in is not a specific Element in an Enum, if (! humanEnum.getValue (). Equals (")) {try {/ / directly generate an instance human = (Human) Class.forName (humanEnum.getValue ()) .newInstance () } catch (Exception e) {/ / because enum is used, this exception will not occur unless there is something wrong with your enum E.printStackTrace ();}} / / if ends return human;}}

See, this is the advantage of introducing enum. The createHuman (HumanEnum humanEnum) method defines that the input parameter must be of type HumanEnum, and then you can directly use the humanEnum.getValue () method to get the specific value passed in.

If you look at the program, you can see that it is not very difficult. The purpose of this abstract class is to reduce the code amount of the following implementation class. Let's look at the implementation class:

Men's factory, create only men:

Package com.cbf4life.humanFactory;import com.cbf4life.Human;import com.cbf4life.HumanEnum;/*** male creation Factory * / public class MaleHumanFactory extends AbstractHumanFactory {/ / create a male black public Human createBlackHuman () {return super.createHuman (HumanEnum.BlackMaleHuman) } / / create a male Caucasian public Human createWhiteHuman () {return super.createHuman (HumanEnum.WhiteMaleHuman);} / / create a male yellow public Human createYellowHuman () {return super.createHuman (HumanEnum.YelloMaleHuman);}}

Women's factories, create only women:

Package com.cbf4life.humanFactory;import com.cbf4life.Human;import com.cbf4life.HumanEnum;/*** female creation Factory * / public class FemaleHumanFactory extends AbstractHumanFactory {/ / create a female black public Human createBlackHuman () {return super.createHuman (HumanEnum.BlackFemaleHuman);} / / create a female Caucasian public Human createWhiteHuman () {return super.createHuman (HumanEnum.WhiteFemaleHuman) } / / create a female yellow public Human createYellowHuman () {return super.createHuman (HumanEnum.YelloFemaleHuman);}}

The product is defined, the factory is defined, and everything is ready but Dongfeng, so let's start building. Oh, no, Nu WA began to make people.

Public class NvWa {public static void main (String [] args) {/ / the first production line, the male production line HumanFactory maleHumanFactory = new MaleHumanFactory (); / / the second production line, the female production line HumanFactory femaleHumanFactory = new FemaleHumanFactory (); / / the production line has been established and the production begins: Human maleYellowHuman = maleHumanFactory.createYellowHuman (); Human femaleYellowHuman = femaleHumanFactory.createYellowHuman () MaleYellowHuman.cry (); maleYellowHuman.laugh (); femaleYellowHuman.sex (); / *. * you can continue later * /}} "how to implement the java Abstract Factory pattern". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report