Design Mode: java
I. Creation Mode
Mode name |
Definition |
Usage Frequency |
Learning Difficulty |
Simple factory Mode (Simple Factory Pattern) |
Defines a factory class. It can return instances of different classes according to different parameters. The created instances usually share the same parent class. |
★★☆☆☆ |
★★★☆☆ |
Factory method mode (Factory Method Pattern) |
Define an interface for creating objects, but let the subclass decide which class to instantiate. The factory method mode delays the instantiation of a class to its subclass. |
★★☆☆☆ |
★★★★★ |
Abstract Factory Model (Abstract Factory Pattern) |
Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. |
★★★★☆ |
★★★★★ |
Builder Mode (Builder Pattern) |
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. |
★★★★☆ |
★★☆☆☆ |
Prototype (Prototype Pattern) |
Use the prototype instance to specify the type of the object to be created, and copy the prototype to create a new object. |
★★★☆☆ |
★★★☆☆ |
Singleton Mode (Singleton Pattern) |
Make sure that a class has only one instance and provides a global access point to access this unique instance. |
★☆☆☆☆ |
★★★★☆ |
2. Structure and Implementation of the simple factory Model
(1) Structure of the simple factory Model
Customers who want to buy a milkshake pizza or a bacon pizza will use the factory to buy it. If this function is implemented first.
1) create a form first
2) Step 2: create a product parent class
// The product's parent class // The keyword used for the abstract method is abstract public abstract class Pizza {public abstract string say ();}
3) Step 3: Create a subclass of the milkshake pizza
Note: The parent class must be inherited.
Public class chees: Pizza {public override string say () {return "I'm a milkshake Pizza ";}}
4) Step 4: create a sub-class of bacon
Public class pg: Pizza {public override string say () {return "I'm a bacon Pizza ";}}
5) Step 5: Create a factory
// Abstract class factory public abstract class Fack {// static parent class public static Pizza st (string Type) {Pizza pizza = null; switch (Type) {case "milkshake Pizza ": pizza = new chees (); break; case "bacon pizza": pizza = new pg (); break; default: break;} return pizza ;}}
6) Click the order button of the main form to write the following code:
Private void btOk_Click (object sender, EventArgs e) {int num1 = Convert. toInt32 (txtLfet. text); string Signature = cb. text; int num2 = Convert. toInt32 (txtRight. text); // 04. call the static method of the Factory, input the type, and obtain the return value Operator part = Factory. cu (partial); part. numLeft = num1; part. numRight = num2; int result = 0; // 05. call the Calc () corresponding to the parent variable to complete the calculation and receive the return value try {result = part. calc ();} catch (Exception ex) {MessageBox. show (ex. message);} // 06. label1.Text = result is displayed in the Label. toString ();}
Iii. Singleton Mode