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

Introduction and Application of Factory pattern in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "introduction and application of factory pattern in Java". In the operation of actual cases, many people will encounter such a dilemma, 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!

Introduction

Intent: to provide an interface to create a series of related or interdependent objects without specifying their specific classes.

Mainly solve: mainly solve the problem of interface selection.

When to use: the product of the system has more than one product family, and the system consumes only one of the products.

How to solve it: define multiple products within a product family.

Key code: aggregate multiple similar products in a factory.

Application example: work, in order to attend some parties, there must be two or more sets of clothes, such as business clothes (sets, a series of specific products), fashion clothes (sets, a series of specific products), and even for a family, there may be business women's wear, business men's wear, fashion women's wear, fashion men's wear, these are also complete sets, that is, a series of specific products. Suppose there is a situation (it does not exist in reality, otherwise, there is no way to enter communism, but it is conducive to illustrating the abstract factory model), in your home, a certain wardrobe (specific factory) can only store one such clothes (complete sets, a series of specific products), and every time you take this complete set of clothes, you naturally have to take them out of this wardrobe. To understand with the idea of OOP, all wardrobes (concrete factories) are of wardrobe category (abstract factories), and each complete set of clothes includes specific tops (a specific product), trousers (a specific product), these concrete tops are actually tops (abstract products), and specific trousers are trousers (another abstract product).

Advantage: when multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family.

Disadvantages: product family expansion is very difficult, to add a series of products, not only to add code in the abstract Creator, but also to add code in the concrete.

Use scene: 1, QQ change skin, a whole set of change together. 2. Generate programs of different operating systems.

Note: the product family is difficult to expand and the product grade is easy to expand.

Realize

We will create Shape and Color interfaces and entity classes that implement these interfaces. The next step is to create the abstract factory class AbstractFactory. Then define the factory classes ShapeFactory and ColorFactory, both of which extend AbstractFactory. Then create a factory creator / generator class FactoryProducer.

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get the AbstractFactory object. It passes the shape information Shape (CIRCLE / RECTANGLE / SQUARE) to AbstractFactory to get the type of object it needs. It also passes the color information Color (RED / GREEN / BLUE) to AbstractFactory in order to get the type of object it needs.

Step 1

Create an interface for the shape.

Public interface Shape {void draw ();}

Step 2

Create an entity class that implements the interface.

Public class Rectangle implements Shape {@ Override public void draw () {System.out.println ("Inside Rectangle::draw () method.");}} public class Square implements Shape {@ Override public void draw () {System.out.println ("Inside Square::draw () method.");}} public class Circle implements Shape {@ Override public void draw () {System.out.println ("Inside Circle::draw () method.");}}

Step 3

Create an interface for the color.

Public interface Color {void fill ();}

Step 4

Create an entity class that implements the interface.

Public class Red implements Color {@ Override public void fill () {System.out.println ("Inside Red::fill () method.");}} public class Green implements Color {@ Override public void fill () {System.out.println ("Inside Green::fill () method.");}} public class Blue implements Color {@ Override public void fill () {System.out.println ("Inside Blue::fill () method.");}}

Step 5

Create abstract classes for the Color and Shape objects to get the factory.

Public abstract class AbstractFactory {public abstract Color getColor (String color); public abstract Shape getShape (String shape);}

Step 6

Create a factory class that extends AbstractFactory and generate objects of the entity class based on the given information.

Public class ShapeFactory extends AbstractFactory {@ Override public Shape getShape (String shapeType) {if (shapeType = = null) {return null;} if (shapeType.equalsIgnoreCase ("CIRCLE")) {return new Circle ();} else if (shapeType.equalsIgnoreCase ("RECTANGLE")) {return new Rectangle ();} else if (shapeType.equalsIgnoreCase ("SQUARE")) {return new Square () } return null;} @ Override public Color getColor (String color) {return null;}} public class ColorFactory extends AbstractFactory {@ Override public Shape getShape (String shapeType) {return null;} @ Override public Color getColor (String color) {if (color = = null) {return null } if (color.equalsIgnoreCase ("RED")) {return new Red ();} else if (color.equalsIgnoreCase ("GREEN")) {return new Green ();} else if (color.equalsIgnoreCase ("BLUE")) {return new Blue ();} return null;}}

Step 7

Create a factory creator / generator class to get the factory by passing shape or color information.

Public class FactoryProducer {public static AbstractFactory getFactory (String choice) {if (choice.equalsIgnoreCase ("SHAPE")) {return new ShapeFactory ();} else if (choice.equalsIgnoreCase ("COLOR")) {return new ColorFactory ();} return null;}}

Step 8

Use FactoryProducer to get AbstractFactory and pass type information to get objects of entity classes.

Public class AbstractFactoryPatternDemo {public static void main (String [] args) {/ / get the shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory ("SHAPE"); / / get the object Shape shape1 = shapeFactory.getShape ("CIRCLE") of shape Circle; / / call the draw method shape1.draw () of Circle; / / get the object Shape shape2 = shapeFactory.getShape ("RECTANGLE") of shape Rectangle / / call the draw method shape2.draw () of Rectangle; / / get the object Shape shape3 = shapeFactory.getShape ("SQUARE") in the shape of Square; / / call the draw method shape3.draw () of Square; / / get the color factory AbstractFactory colorFactory = FactoryProducer.getFactory ("COLOR") / / get the object with the color Red Color color1 = colorFactory.getColor ("RED"); / / call the fill method color1.fill () of Red; / / get the object Color color2 = colorFactory.getColor ("Green") with the color Green; / / call the fill method color2.fill () of Green / / get the object Color color3 = colorFactory.getColor ("BLUE") with the color Blue; / / call the fill method color3.fill () of Blue;}}

Step 9

Execute the program and output the result:

Inside Circle::draw () method.Inside Rectangle::draw () method.Inside Square::draw () method.Inside Red::fill () method.Inside Green::fill () method.Inside Blue::fill () method. This is the end of the introduction and application of factory patterns in Java. 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

Servers

Wechat

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

12
Report