Builder pattern.

0

Cześć, chciałem sobie zobaczyć jak działa builder i dla ćwiczenia napisałem prosty program:

#include <iostream>
#include  <string>

class home // Product 
{
private:
	std::string windows;
	std::string walls;
	std::string door;
	std::string homeType;
public:
	home(std::string homeType) {};
	
	void setWalls(std::string walls);
	void setWindows(std::string windows);
	void setDoor(std::string door);

	void print()
	{
		std::cout << "Home type: " << homeType << "\n";
		std::cout << "Walls: " << walls << "\n";
		std::cout << "Door: " << door << "\n";
		std::cout << "Windows: " << windows << "\n";
	}
};

class homeBuilder
{
protected:
	home *homeProduct;
public:
	virtual void createHome() {};
	virtual void buildWindows() {};
	virtual void buildWalls() {};
	virtual void buildDoor() {};

	home* getHome()
	{
		return homeProduct;
	}
};

class smallHomeBuilder : public homeBuilder
{
public:
	void createHome()
	{
		homeProduct = new home("Small home.");
	}

	void buildWindows()
	{
		homeProduct->setWindows("4");
	}

	void buildWalls()
	{
		homeProduct->setWalls("4");
	}

	void buildDoor()
	{
		homeProduct->setDoor("1");
	}
};


class bigHomeBuilder : public homeBuilder
{
public:
	void createHome()
	{
		homeProduct = new home("Big home.");
	}

	void buildWindows()
	{
		homeProduct->setWindows("20");
	}

	void buildWalls()
	{
		homeProduct->setWalls("24");
	}

	void buildDoor()
	{
		homeProduct->setDoor("4");
	}
};

class direcotr
{
	homeBuilder * builder;
public:
	home* createHome(homeBuilder *builder)
	{
		builder->buildDoor();
		builder->buildWindows();
		builder->buildWalls();

		return  builder->getHome();
	}
};

int main()
{
	direcotr myDirector;

	smallHomeBuilder smallHomeBuilder;
	bigHomeBuilder bigHomeBuilder;

	home *small = myDirector.createHome(&smallHomeBuilder);
	home *big = myDirector.createHome(&bigHomeBuilder);

	small->print();
	std::cout << "\n";
	big->print();

	delete small;
	delete big;
}

Ale nie mogę skompilować programu z powodów:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall home::setDoor(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: virtual void __thiscall bigHouseBuilder::buildDoor(void)" ([email protected]@@UAEXXZ) Builder E:\Repos\C++\Builder\Builder\main.obj 1

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall home::setWalls(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: virtual void __thiscall bigHouseBuilder::buildWalls(void)" ([email protected]@@UAEXXZ) Builder E:\Repos\C++\Builder\Builder\main.obj 1

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: void __thiscall home::setWindows(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" ([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) referenced in function "public: virtual void __thiscall bigHouseBuilder::buildWindows(void)" ([email protected]@@UAEXXZ) Builder E:\Repos\C++\Builder\Builder\main.obj 1

Nie wiedzę nigdzie błędu a on jest gdzieć na pewno, ma ktoś pomysł co jest nie tak?

1

Brakuje Ci implementacji metod z klasy home
Kompilator Ci to powiedział...

0

Faktycznie, chyba z tego upału o tym zapomniałem - tak samo jak o stworzeniu obiektu na którym pracuję. Ale już poprawiłem wszystko. Dzięki :)
Może się komuś przyda - poprawiona wersja:

#include <iostream>
#include  <string>

class home // Product 
{
private:
	std::string windows;
	std::string walls;
	std::string door;
	std::string homeType;
public:
	home(std::string homeType) {};
	
	void setWalls(std::string walls)
	{
		this->walls = walls;
	}

	void setWindows(std::string windows)
	{
		this->windows = windows;
	}
	void setDoor(std::string door)
	{
		this->door = door;
	}

	void print()
	{
		std::cout << "Home type: " << homeType << "\n";
		std::cout << "Walls: " << walls << "\n";
		std::cout << "Door: " << door << "\n";
		std::cout << "Windows: " << windows << "\n";
	}
};

class homeBuilder
{
protected:
	home *homeProduct;
public:
	virtual void createHome() {};
	virtual void buildWindows() {};
	virtual void buildWalls() {};
	virtual void buildDoor() {};

	home* getHome()
	{
		return homeProduct;
	}
};

class smallHomeBuilder : public homeBuilder
{
public:
	void createHome()
	{
		homeProduct = new home("Small home.");
	}

	void buildWindows()
	{
		homeProduct->setWindows("4");
	}

	void buildWalls()
	{
		homeProduct->setWalls("4");
	}

	void buildDoor()
	{
		homeProduct->setDoor("1");
	}
};


class bigHomeBuilder : public homeBuilder
{
public:
	void createHome()
	{
		homeProduct = new home("Big home.");
	}

	void buildWindows()
	{
		homeProduct->setWindows("20");
	}

	void buildWalls()
	{
		homeProduct->setWalls("24");
	}

	void buildDoor()
	{
		homeProduct->setDoor("4");
	}
};

class direcotr
{
	homeBuilder * builder;
public:
	home* createHome(homeBuilder *builder)
	{
		builder->createHome();
		builder->buildDoor();
		builder->buildWindows();
		builder->buildWalls();

		return  builder->getHome();
	}
};

int main()
{
	direcotr myDirector;

	smallHomeBuilder smallHomeBuilder;
	bigHomeBuilder bigHomeBuilder;

	home *small = myDirector.createHome(&smallHomeBuilder);
	home *big = myDirector.createHome(&bigHomeBuilder);

	small->print();
	std::cout << "\n";
	big->print();

	delete small;
	delete big;
}
2

Musze cię zmartwić ale te klasy z klascznymi builderami maja tylko tyle wspolnego, że też ostatecznie tworzą obiekt.
Polecam przeczytać więcej przykładów. O np tu https://cpppatterns.com/patterns/builder.html

1 użytkowników online, w tym zalogowanych: 0, gości: 1