Simple Factory Model Mode Simple Factory Pattern

xiaoxiao2021-03-06  59

Simple Factory Model Mode Simple Factory Pattern

2004-04-20 23:36:06 Guoyan19811021

http://www.21tx.com/school/dotnet/wz/37 wtv2ftzzfp1njchs.shtml

In an object-oriented (OO) program, what we often see is Simple Factory Pattern. Simple factory mode returns an instance of a class in a few possible classes based on the data provided to it. Usually it returns a class having a common parent class and a common method, but each method is considered different, and it is optimized according to different data. The simple factory model is actually not 23 GOF mode, but it can be guided by a Factory Method. How does the factory model work to facilitate the understanding of simple factory models, you can see the following: Factory Creator Role: This role is the core of factory method model, which contains a product in accordance with a certain business logic. The factory class creates a product object under the direct call of the client, which is often implemented by a specific class. Abstract Product Role: The class that serves this role is the parent class of the object created by the factory method mode, or the interface they share. Abstract product roles can be implemented with an interface or abstract class. Concrete Product Role: Any object created by the factory method mode is an instance of this role, and the specific product role is implemented by a specific class. In the above figure, class X is a base class, class XY, and XZ derived self-class X, XFactory class decides to return the instance of the subclass based on the value you provide, we define the getClass method, accept some value, and Returns one of the X classes based on this value. Returns which one of them is not related to programmers. Because they all contain the same method, different implementations may be a very complex function, but it is usually very simple. Class Now, we write two simple classes to implement a string into two parts, suppose we have an input form, allowing users to enter his name, but we have judged by the simple factory model NameFactory class The input firstname and lastname are spaced or comma. If we are a comma or space, we divide them into two parts. First define a simple class. Public Class NameClass Protected Lname, Frname As String Public Function getFirst () As String Return Frname End Function Public Function getLast () As String Return Lname End Function End Class prepared by the following two derived classes: FirstFirst class, lastFirst class which implements the base class NameClass, and divided the name into two parts in the constructor.

转载请注明原文地址:https://www.9cbs.com/read-119492.html

New Post(0)