Design Pattern C ++ study note 14th (Iterator pattern)

Source: Internet
Author: User
14. 1. Explanation

Concept: provides a way to access each element in an aggregate object sequentially without exposing the internal representation of the object.

Main (), customer

IProject, product Interface

CProject, product type

IIterator, iterator Interface

IProjectIterator, product iterator Interface

CProjectIterator, product iterator implementation class

Note: The CProject product class can return the pointer of an iterator. This iterator encapsulates an array in the product class. Therefore, when running the Next function, the iterator can traverse all elements of this array.

To put it simply, use code to implement vector <int>: iterator or vector <int>: const_iterator.

Let's look at the Code:

// IProject. h

# Pragma once
# Include "IProjectIterator. h"
# Include <iostream>
Using std: string;
Class IProject
{
Public:
IProject (void)
{
}
Virtual ~ IProject (void)
{
}
Virtual void Add (string name, int num, int cost) = 0;
Virtual string GetProjectInfo () = 0;
Virtual IProjectIterator * GetIterator () = 0;
Virtual void Erase () = 0;
};

// Project. h

# Pragma once
# Include "iproject. h"
# Include "IProjectIterator. h"
# Include <iostream>
# Include <vector>
Using std: string;
Using std: vector;
Class CProject:
Public IProject
{
Public:
CProject (void );
CProject (string name, int num, int cost );
~ CProject (void );
String GetProjectInfo ();
Void Add (string name, int num, int cost );
IProjectIterator * GetIterator ();
Void Erase ();
Private:
String m_name;
Int m_num;
Int m_cost;
Vector <IProject *> m_projectList;
};

// Project. cpp

# Include "StdAfx. h"
# Include "Project. h"
# Include ".. \ CommonDeclare \ Convert. h"
# Include "ProjectIterator. h"
# Include <iostream>
# Include <vector>
Using std: string;
Using std: vector;
CProject: CProject (void)
{
M_name = "";
M_num = 0;
M_cost = 0;
}
CProject: CProject (string name, int num, int cost): m_name (name), m_num (num), m_cost (cost)
{
}
CProject ::~ CProject (void)
{
}
String CProject: GetProjectInfo ()
{
String info = "";
Info. append ("Project name :");
Info. append (this-> m_name );
Info. append ("\ t project count :");
Info. append (CConvert: ToString (m_num ));
Info. append ("\ t project fee :");
Info. append (CConvert: ToString (m_cost ));
Return info;
}
Void CProject: Add (string name, int num, int cost)
{
This-> m_projectList.push_back (new CProject (name, num, cost ));
}
IProjectIterator * CProject: GetIterator ()
{
Return new CProjectIterator (this-> m_projectList );
}
Void CProject: Erase ()
{
Vector <IProject *>: reverse_iterator projectDelIt = m_projectList.rbegin ();
For (; projectDelIt! = M_projectList.rend (); projectDelIt ++)
{
Delete (* projectDelIt );
(* ProjectDelIt) = NULL;
}
M_projectList.clear ();
}

// IIterator. h

# Pragma once
Class IProject;
Class IIterator
{
Public:
IIterator (void)
{
}
Virtual ~ IIterator (void)
{
}
Virtual bool HasNext () = 0;
Virtual IProject * Next () = 0;
};

// IProjectIterator. h

# Pragma once
# Include "iiterator. h"
Class IProject;
Class IProjectIterator:
Public IIterator
{
Public:
IProjectIterator (void)
{
}
Virtual ~ IProjectIterator (void)
{
}
Virtual bool HasNext () = 0;
Virtual IProject * Next () = 0;
};

// ProjectIterator. h

# Pragma once
# Include "iprojectiterator. h"
# Include "IProject. h"
# Include <vector>
Using std: vector;
Class CProjectIterator:
Public IProjectIterator
{
Public:
CProjectIterator (vector <IProject *> pl );
~ CProjectIterator (void );
Bool HasNext ();
IProject * Next ();
Private:
Vector <IProject *> m_projectList;
Size_t m_currentItem;
};

// ProjectIterator. cpp

# Include "StdAfx. h"
# Include "ProjectIterator. h"
CProjectIterator: CProjectIterator (vector <IProject *> pl): m_projectList (pl)
{
M_currentItem = 0;
}
CProjectIterator ::~ CProjectIterator (void)
{
}
Bool CProjectIterator: HasNext ()
{
Bool B = true;
If (m_currentItem> = m_projectList.size ())
B = false;
Return B;
}
IProject * CProjectIterator: Next ()
{
IProject * pp = m_projectList.at (m_currentItem ++ );
Return pp;
}

// Iterator. cpp
# Include "stdafx. h"
# Include "IProject. h"
# Include "Project. h"
# Include ".. \ CommonDeclare \ Convert. h"
# Include "ProjectIterator. h"
# Include <iostream>
# Include <vector>
Using std: vector;
Using std: cout;
Using std: endl;
Void DoIt ()
{
Cout <"---------- no iteration mode ----------" <endl;
Vector <IProject *> projectList;

ProjectList. push_back (new CProject ("Star Wars project", 10,100 000 ));
ProjectList. push_back (new CProject ("reverse spatiotemporal Project", 100,100 ));
ProjectList. push_back (new CProject ("Superman Transformation Project", 10000,100 0000000 ));

For (int I = 4; I <6; I ++)
{
String name = "";
Name. append ("th ");
Name. append (CConvert: ToString (I ));
Name. append ("Projects ");
ProjectList. push_back (new CProject (name, I * 5, I * 1000000 ));
}

Vector <IProject *>: const_iterator projectIt = projectList. begin ();
For (; projectIt! = ProjectList. end (); projectIt ++)
Cout <(* projectIt)-> GetProjectInfo (). c_str () <endl;

Vector <IProject *>: reverse_iterator projectDelIt = projectList. rbegin ();
For (; projectDelIt! = ProjectList. rend (); projectDelIt ++)
{
Delete (* projectDelIt );
(* ProjectDelIt) = NULL;
}
ProjectList. clear ();
}
Void DoNew ()
{
Cout <"---------- use iteration mode ----------" <endl;
IProject * pproject = new CProject ();
Pproject-> Add ("Star Wars project", 10,100 000 );
Pproject-> Add ("reverse spatiotemporal Project", 100,100 );
Pproject-> Add ("Superman Transformation Project", 10000,100 0000000 );

For (int I = 4; I <6; I ++)
{
String name = "";
Name. append ("th ");
Name. append (CConvert: ToString (I ));
Name. append ("Projects ");
Pproject-> Add (name, I * 5, I * 1000000 );
}

IProjectIterator * pprojectIt = pproject-> GetIterator ();
While (pprojectIt-> HasNext ())
{
IProject * p = dynamic_cast <IProject *> (pprojectIt-> Next ());
Cout <p-> GetProjectInfo (). c_str () <endl;
}
Delete pprojectIt;
PprojectIt = NULL;
Pproject-> Erase ();
Delete pproject;
Pproject = NULL;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Before using the Iterator Mode
DoIt ();

// Use Iterator
DoNew ();

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();

Return 0;
}

This model may be a bit difficult and requires careful consideration. I originally hoped to summarize the mode of work in the simplest and easy-to-understand language, but in fact this conclusion is more difficult. A simple model is easy to understand and summarize, and complicated. I plan to summarize this sentence in the process of learning the data structure later. Looking forward to learning the data structure, I find that I love learning more and more.

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.