Structural mode (vi) Enjoy meta-mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.