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 understand the Java simple factory model

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how to understand the Java simple factory model, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.

Overview of simple factory model

1. Definition: defines a factory class that can return instances of different classes according to different parameters, and the created instances usually have a common parent class

two。 The method used to create an instance in a simple factory pattern is usually a static method, so the simple factory pattern is called a static factory method (Static Factory Method).

3. What you need, you only need to pass in a correct parameter to get the desired object without knowing its implementation process.

4. For example, I open a pizzeria, and when a customer needs some kind of pizza and I can make it in this store, I will provide him with the pizza he needs ( for money, of course). If I don't have what I need, it's a different situation. I'll talk to you later. At this time, my pizza shop can be regarded as a Factory, and the pizza produced is called a product (Product), and the name of the pizza is called a parameter. The factory can return different products according to the parameters, which is the simple factory model.

The structure and implementation structure of the simple factory pattern:

1.Factory (factory): the core part, which is responsible for implementing the internal logic of creating all products. The factory class can be called directly by the outside world to create the desired objects.

2.Product (abstract product): the parent class of all objects created by the factory class, which encapsulates the public methods of the product object, and all concrete products are its subclass objects.

3.ConcreteProduct (concrete product): the goal of creating a simple factory pattern, where all objects created are instances of a specific class. It implements abstract methods declared in abstract products (about abstract classes)

Implement abstract class Product {public void MethName () {/ / implementation of public methods} public abstract void MethodDiff (); / declare abstract business methods} class ConcreteProductA: Product {public override void MethodDiff () {/ / implementation of business methods}} class Factory {public static Product GetProduct (string arg) {Product product = null If (arg.Equals ("A") {product = new ConcreteProductA (); / / init} else if (arg.Equals ("B")) {product = new ConcreteProductB (); / / init} else {.... / / other cases} return product }} class Program {static void Main (string [] args) {Product product; product = Factory.GetProduct ("A"); / / Factory class creation object Product.MethName (); product.MethodDiff ();}} simplification of the simple factory pattern

1. To simplify the simple factory pattern, merge the abstract product class and the factory class, and move the static factory method to the abstract product class.

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

Development

Wechat

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

12
Report