【しばらく編集不可モードで運営します】 編集(管理者用) | 差分 | 新規作成 | 一覧 | RSS | FrontPage | 検索 | 更新履歴

C++言語版FactoryMethod - I have written.

I have written.


# include <iostream>
# include <string>
# include <vector>
using namespace std;

class Product {
public:
    virtual ~ Product () {}
    virtual void use () = 0;
}

class Factory {
public:
    virtual ~ Factory () {}
    Product * create (string owner) {
        Product * p = createProduct (owner);
        registerProduct (p);
        return p;
    }
protected:
    virtual Product * createProduct (string owner) = 0;
    virtual void registerProduct (Product * product) = 0;
}

class IDCard: public Product {
private:
    string owner;
public:
    IDCard (string owner) {
        cout <<owner << "create a card. \ n";
        this-> owner = owner;
    }
    ~ IDCard () {}
    void use () {
        cout <<owner << "use the card. \ n";
    }
    string getOwner () {
        return owner;
    }
}

class IDCardFactory: public Factory {
private:
    vector <string> owners;
protected:
    Product * createProduct (string owner) {
        return new IDCard (owner);
    }
    void registerProduct (Product * product) {
        owners.push_back (((IDCard *) product) -> getOwner ());
    }
public:
    vector <string> getOwners () {
        return owners;
    }
}

void main () {
    Factory * factory = new IDCardFactory ();
    Product * card1 = factory-> create ( "Hiroshi Yuki");
    Product * card2 = factory-> create ( "uneven and");
    Product * card3 = factory-> create ( "Sato Hanako");
    card1-> use ();
    card2-> use ();
    card3-> use ();
    delete card1;
    delete card2;
    delete card3;
    delete factory;
}

SoraSora


Question Box

card1, card2, card3 the new and delete, but I do not care which is not very "Well, C + + language is difficult to delete it What is the timing of it.

new things that are, well in that class but I want to serve in that function. "In this case, main () in the auto_ptr Tteiu may want to use a hand. (Broad bean)

I guess the above code written by anyone. Zehi sign. --- Yuuki "Oh, I forgot. I wrote. --- Broad bean


License and

Hiroshi Yuki's Java language version http://www.hyuki.com/dp/dpsrc_2001-06-22.zip The broad bean was based on the C + + language Translate. Java version of the same conditions http://www.hyuki.com/dp/

But I want to YukiWiki in Well they can modify the source file. To do.

"Well, I will not have to worry too much (^_^) --- Yuuki

Thank you. In that, please feel free to use (^ ^)


Request

Or anyone, GUI sample using the Mediator, State, Command and Why do not C + + and Na to Translate. --- Broad bean


Please let the questions about the above code.

IDCardFactory # createProduct () in, new object address as Of course you return to? I like this move is certainly What is new in the function object, do you also missing the function valid? In other words, the caller of this function to get the address of this object Is not even through the block that generated the object, Is it that memory is valid?

http://custom-paper-writing.com/dissertation-services

As you may have a rudimentary question, I would appreciate answers. Thank you. --- Passing C + + beginners