Design Pattern 12-structural pattern-enjoy yuan Pattern

Source: Internet
Author: User
Tags getcolor

Definition:The Flyweight Pattern uses the sharing technology to effectively support a large number of fine-grained objects.

Type: structural mode.

Applicability:

  1. An application has a large number of objects.
  2. Most States of objects are external.
  3. If you delete the external state of an object, you can replace many groups of objects with a relatively small number of shared objects.
  4. Applications do not rely on object identifiers, that is, applications rely on abstract interfaces of objects.Overview:

    Flyweight, originally meant "lightweight players. The translator uses this pattern as a free translation, and strives to demonstrate the purpose of this pattern intuitively. Sharing. The meaning of the basic unit. Share, that is, the basic unit of sharing, that is, the use of sharing technology as stated by GoF to effectively support a large number of fine-grained objects.

    The focus of the object metadata mode is to abstract the "internal state" and "external State" of the object. The internal state is stored in the object metadata, while the external State is stored in the external state. This is the key to the Yuan-sharing model. Many articles on the Internet do not fully understand this.

    The following is an example. There is a board where go is exactly a large number of fine-grained objects. We abstract materials, shapes, and production processes into internal states, while the position of go on the board is the marker that identifies each piece, so the position of the piece is abstracted as an external state. The pawns only have two colors: White and Black. If they are stored in an external State, they are stored for many times. Therefore, white games and black games exist as two objects.

    Class Diagram:

    Participants: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPG9sIHR5cGU9 "1">

  5. Client, call the ChessBoard to obtain the specific chess piece object.
  6. The Chessboard is used to manage the generation and storage of pawns, that is, the specific position of each pawns.
    1. Weiqi is an abstract class of go, that is, a metadata object. It abstracts some internal states.
      1. WhiteWeiqi and BlackWeiqi are two derived go games of different colors to implement the color interface.

        Sample Code:

        // Flyweight class

        Public abstract class Weiqi {// internal status // ...... // private int nSize; public abstract Color GetColor ();}

        // Specific Flyweight class

        public class WhiteWeiqi : Weiqi    {        public override Color GetColor()        {            Console.WriteLine("White");            return Color.White;        }    }    public class BlackWeiqi : Weiqi    {        public override Color GetColor()        {            Console.WriteLine("Black");            return Color.Black;        }    }

        // FlyweightFactory class
         public class Blessboard    {        private List
               
                 listWhite = new List
                
                 ();        private List
                 
                   listBlack = new List
                  
                   ();        private WhiteWeiqi wWeiqi;        private BlackWeiqi bWeiqi;        public Blessboard()        {            wWeiqi = new WhiteWeiqi();            bWeiqi = new BlackWeiqi();        }        public Weiqi Produce(bool bWthite, Point pt)        {            if (bWthite)            {                listWhite.Add(pt);                return bWeiqi;            }            else            {                listBlack.Add(pt);                return bWeiqi;            }        }        public Weiqi GetProduce(Point pt)        {            foreach (Point p in listWhite)            {                if (p.Equals(pt))                {                    return wWeiqi;                }            }            foreach (Point p in listBlack)            {                if (p.Equals(pt))                {                    return bWeiqi;                }            }            return null;        }    }
                  
                 
                
               


        // Client class

        Class Program {static void Main (string [] args) {Blessboard bloard = new Blessboard (); // generate bloard. produce (true, new Point (1, 3); bloard. produce (false, new Point (2, 3); bloard. produce (true, new Point (1, 4); bloard. produce (false, new Point (2, 4); // you can call this operation to query the Weiqi weiqi = bloard. getProduce (new Point (2, 4); Color color = weiqi. getColor (); weiqi = bloard. getProduce (new Point (1, 4); color = weiqi. getColor ();}}


        Note:For C ++ abstract classes, you must note that the Destructor must be virtual functions.

        Advantages and disadvantages:

        1. Advantage: reduces memory usage.
          1. Disadvantages: it increases the complexity of the structure and requires independent management of external states.

            References:

            1. Design Patterns-reusable Object-Oriented Software basics
              1. Big talk Design Model
                1. Head First design model

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.