This is the 14th day of my participation in the August Text Challenge.More challenges in August

Amount of fly

Fly pattern: Use fly pattern if one instance of a class can be used to provide many “virtual instances.”

In a house design platform, there are trees around it. The tree has an XY coordinate position, and it can dynamically draw itself according to the age of the tree. If we create many trees, there will be many instance objects of trees. Using a tree instance and a customer object to maintain the state of “all” trees is the fly count pattern.

Design Class diagram:

Implementation code:

① Create a class that stores tree state

1 public class TreeState 2 { 3 public int X { get; set; } 4 public int Y { get; set; } 5 public int Age { get; set; 6}}Copy the code

② Create a tree object with no state

1 public class Tree 2 {3 public void Display(int x, int y, int age) {4 console. WriteLine($" coordinates: {x},{y} Tree age: {age}"); 6 5}}Copy the code

③ Create managed objects

1 public class TreeManager 2 { 3 List<TreeState> treeArray; 4 private Tree treeObj; 5 public TreeManager(List<TreeState> trees) { 6 treeArray = trees; 7 treeObj = new Tree(); 8 } 9 10 public void DispalyTress() { 11 foreach (var tree in treeArray) 12 { 13 treeObj.Display(tree.X, tree.Y, tree.Age); 14} 15} 16}Copy the code

Advantages:

1. Reduce the number of runtime object instances to save memory.

2. Centrally manage the state of many “virtual” objects.

Usage and disadvantages:

1. When a class has many instances and these instances can be controlled by a unified method, we can use fly count patterns.

2. The disadvantage of fly count is that once you implement it, a single instance cannot have independent and distinct behavior.

The interpreter

Interpreter mode: Creates an interpreter for the language

Given a language, define its grammatical representation and define an interpreter that uses the identifier to interpret sentences in the language. Each syntactic rule is represented by a class. Note that classes map directly to grammars.

It is not always necessary to copy the patterns described in the book when we use them, but it is reasonable to have more interpreter classes.

Class diagram:

 

Sample code:

(1) an abstract class

1 Public interface Expression 2 {3 // <summary> 4 // </summary> 6 // <param name="input"></param> 7 // <returns></returns> 8 public Object interpret(String input); 9}Copy the code

② Terminator expression class

1 public class TerminalExpression : Expression 2 {3 public object interpret(string input) 4 {5 return null; 8 7}}Copy the code

③ Non-terminal expression classes

1 public class NonterminalExpression : Expression 2 { 3 private Expression exp1; 4 private Expression exp2; 5 Public Object interpret(string input) 6 {7 // non-terminal expression parsing 8 return null; 10 9}}Copy the code

④ Context class

1 public class Context 2 { 3 private Expression exp; 9 public void operation(string input) 9 {10 public void operation(string input) 9 {10 public void operation(string input) 11} 12}Copy the code

Advantages:

1. Each grammar rule is represented as a class to facilitate the implementation of the language.

2. Because syntax is represented by many classes, the language can easily be changed or extended.

3. By adding new methods to the class structure, you can add new behavior at the same time as interpretation, such as print formatting or complex program validation.

Usage and disadvantages:

1. Use the interpreter when you need to implement a simple language.

2, can handle scripting languages and programming languages.

3. This pattern becomes cumbersome when the number of grammars is too large and complex.