A summary of a sentence
Use shared technology to support a large number of objects. The enjoy meta-mode avoids a lot of similar overhead.
Plot
Goddess and Xiaoming are going to create a small team to do CS. They invited Xiao Gang to join, and there will be more people in the back.
Let's take a look at the differences between everyone. is gender and name. However, to create a new object for this purpose, a large number of similar classes of overhead are created.
At this point, we use shared technology to avoid the same overhead. The above plot is constructed using the enjoy meta model.
Task One: Create small teams and let them introduce their own methods
First we create an abstract class. Then we create this team of xiaoming.
Public Abstract classTeam { Public Abstract voidIntroduce (stringstrsex); } Public classSomemteam:team {Private stringname; PublicSomemteam (stringstrName) { This. Name =StrName; } Public Override voidIntroduce (stringstrsex) {Console.WriteLine ("name,"+ name +". Sex,"+strsex); } }Task two: Create a shared factory
We create a shared factory to implement the instance sharing functionality.
First of all, when our team was established, there was xiaoming and the goddess. After the establishment of the success, small just entered the team.
Public classTeamfactory { Publicdictionary<string, team> teamlist =Newdictionary<string, team>(); Publicteamfactory () {Teamlist.add ("Xiao Ming",NewSomemteam ("Xiao Ming")); Teamlist.add ("Goddess",NewSomemteam ("Goddess")); } PublicTeam Getteam (stringkey) {Team Team=NULL; if(Teamlist.containskey (key))returnTeamlist[key] asTeam; Else{Console.WriteLine ("Newcomers Join"); Team=NewSomemteam (key); returnteam; } } }
Here, we use key-value pairs to achieve sharing. Extracts a fetch method and adds a new object if there is no share.
Task Three: Test execution
We test execution to see if we can succeed.
Static voidMain (string[] args) {Console.WriteLine ("Xiao Ming and the goddess create a small team"); Teamfactory FA=Newteamfactory (); Team Teamm= FA. Getteam ("Xiao Ming"); Teamm.introduce ("male"); Team NvS= FA. Getteam ("Goddess"); Nvs.introduce ("female"); Team teamg= FA. Getteam ("Xiao Gang"); Teamg.introduce ("male"); Console.ReadLine (); }
Let's take a look at the results
Summarize
Shared mode is the use of shared technology to avoid a lot of similar overhead.
Common scenarios: some objects with the same state are reused.
Select key: whether the shared object can be extracted from an external state.
Structural mode (vi) Enjoy meta-mode