Benefits of design patterns

Source: Internet
Author: User

Benefits of design patterns
Flyweight)
Use the sharing technology to effectively support a large number of fine-grained objects.


Do you still remember the five games you 've played on the text? Wuziqi is a pure strategy-based chess game between two players. It originated from one of the traditional black and white games in ancient China. It not only enhances the thinking ability, but also improves the intelligence, and is rich in philosophy and helps cultivate self-cultivation.


What should we do if we want to develop a five-game program? Let's take a look at the pawns in the wuziqi game, that is, the two types of pawns: "Sunday" and "white son". If we create a new object instance each time, is it too much to consume system memory? Here we will solve this problem for you.


Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. collections; namespace: Meta mode {// chess subclass public abstract class AbstractChessman {// chess coordinate protected int x; protected int y; // chess pieces category (black | white) protected string chess; public AbstractChessman (string chess) {this. chess = chess;} // set public abstract void point (int x, int y) by coordinate; // display the pu Blic void show () {Console. writeLine (this. chess + "(" + this. x + "," + this. y + ")") ;}}// implement public class BlackChessman: AbstractChessman {public BlackChessman (): base ("●") {Console. writeLine ("-- BlackChessman Construction Exec !!! ");} Public override void point (int x, int y) {this. x = x; this. y = y; this. show () ;}// implement public class WhiteChessman: AbstractChessman {public WhiteChessman (): base ("○") {Console. writeLine ("-- WhiteChessman Construction Exec !!! ");} Public override void point (int x, int y) {this. x = x; this. y = y; this. show () ;}/// create the chess pieces factory public class FiveChessmanFactory {// Singleton mode factory private static FiveChessmanFactory fiveChessmanFactory = new FiveChessmanFactory (); // cache storage shared object private Hashtable cache = new Hashtable (); // private constructor private FiveChessmanFactory () {}// obtain the singleton factory public static FiveChessmanFactory getInstance () {return fiveChessm AnFactory;} public AbstractChessman getChessmanObject (string c) {// obtain the pawn object instance from the cache. AbstractChessman abstractChessman = (AbstractChessman) this. cache [c]; if (abstractChessman = null) {// no information about the pawn object instance in the cache, create a pawn object instance and place it in the cache switch (c) {case "B": abstractChessman = new BlackChessman (); break; case "W": abstractChessman = new WhiteChessman (); break; default: break ;} // return null if (abstractChessman! = Null) {cache. add (c, abstractChessman) ;}return abstractChessman ;}} class Program {static void Main (string [] args) {// create FiveChessmanFactory fiveChessmanFactory = FiveChessmanFactory. getInstance (); // Random number, which is used to generate the random Random = new Random (); int radom = 0; AbstractChessman abstractChessman = null; for (int I = 0; I <10; I ++) {radom = random. next (2); switch (radom) {case 0: abstract Chessman = fiveChessmanFactory. getChessmanObject ("B"); break; case 1: abstractChessman = fiveChessmanFactory. getChessmanObject ("W"); break;} if (abstractChessman! = Null) {// set the position information of the pawnpiece. abstractChessman. point (I, random. Next (15 ));}}}}}

Shares = "http://www.2cto.com/uploadfile/Collfiles/20140822/20140822084208274.png" alt = "" http://www.bkjia.com/ OS/"target =" _ blank "class =" keylink "> systems share as appropriate.




Main advantages:
1. The number of objects in the memory can be greatly reduced, so that only one copy of the same or similar objects can be saved in the memory, thus saving system resources and improving system performance.
2. The external status of the shared object mode is relatively independent and does not affect its internal status, so that the shared object can be shared in different environments.


Main disadvantages:
1. the metadata mode makes the system complex and requires separation of internal and external states, which complicate the logic of the program.
2. In order for objects to be shared, part of the state of the object to be shared must be externalized in the metadata mode, and reading the external State will extend the running time.


Applicable scenarios:
1. When there are many instances of an object type in the system.
2. Most of the object states can be externalized and can be passed into the object.





Related Design Patterns
1. Combination: You can use the leaf nodes in the shared element combination to improve the processing efficiency of the system.
2. Singleton: Generally, the Yuan-sharing factory is set as a singleton to reduce the space used by the system. The Singleton itself is a kind of Yuan! A single instance has only one object instance, which is shared by other objects.









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.