c_cpp 复合模式问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 复合模式问题相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <string>
#include "Drawing.hpp"
#include "Shape.hpp"
#include "Circle.hpp"
#include "Group.hpp"

using namespace std;

int main() {
  Circle tete(0, 0, 200, Circle::Color::BLACK);
  Circle c1(0, 0, 100, Circle::Color::BLACK);
  Circle c2(0, 0, 70, Circle::Color::PINK);
  Group oreille;
  oreille.add(c1);
  oreille.add(c2);
  oreille.translate(-210, 210);
  Group oreille2(oreille);
  oreille2.translate(420, 0);
  Circle nez(0, 0, 20, Circle::Color::RED);
  Drawing d;
  d.add(tete);
  d.add(oreille);
  d.add(oreille2);
  d.add(nez);
  d.draw();

  return 0;
}


// Commande de compilation car pas cool de la retrouver dans l'historique console à chaque fois...
// g++ -std=c++11 source/main.cpp source/Circle.cpp source/Shape.cpp source/Group.cpp  source/Drawing.cpp -framework SFML-graphics -framework SFML -framework SFML-window -framework SFML-system
#ifndef SHAPE_H
#define SHAPE_H

#include "Drawing.hpp"

class Drawing;

class Shape {
public:
    virtual void draw(sf::RenderWindow &target);
    virtual void translate(const float x, const float y);
private:

};

#endif
#include "Shape.hpp"

void Shape::draw(sf::RenderWindow &target) {};
void Shape::translate(const float x, const float y) {};
#ifndef GROUP_H
#define GROUP_H

#include <vector>
#include "Shape.hpp"

class Group : public Shape::Shape {
public:
  Group();
  Group(const std::vector<Shape::Shape> &children);
  void add(Shape::Shape &s);
  std::vector<Shape::Shape> &getChildren();
  void draw(sf::RenderWindow &target);
  void translate(const float x, const float y);
private:
  std::vector<Shape::Shape> myChildren;
};

#endif
#include "Group.hpp"

using namespace std;

Group::Group() {
  myChildren = vector<Shape::Shape>();
};

Group::Group(const vector<Shape::Shape> &children){
  myChildren = children;
};

void Group::add (Shape::Shape &s) {
  myChildren.push_back(s);
};

std::vector<Shape::Shape> &Group::getChildren() {
  return myChildren;
};

void Group::draw(sf::RenderWindow &target) {
  for (Shape::Shape s : myChildren) {
    s.draw(target);
  }
};

void Group::translate(const float x, const float y) {
  for (Shape::Shape s : myChildren) {
    s.translate(x, y);
  }
};
#ifndef DRAWING_H
#define DRAWING_H

#include <SFML/Graphics.hpp>
#include "Shape.hpp"
#include <string>

class Shape;

class Drawing {
public:
  Drawing();
  void draw();
  void add(Shape &shape);
private:
  std::vector<Shape> myShapes;
};

#endif
#include "Drawing.hpp"
#include <iostream>

using namespace std;

Drawing::Drawing () {
  myShapes = vector<Shape::Shape>();
};

void Drawing::draw() {
  sf::RenderWindow window(sf::VideoMode(800, 600), "myTitle"); // TODO enlever le hardcodage des dimensions et du titre
  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
      if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)) {
        window.close();
      }
      window.clear(sf::Color::White);

      for (Shape::Shape s : myShapes) {
        s.draw(window);
      }

      window.display();
    }
  }
};

void Drawing::add(Shape::Shape &shape) {
  myShapes.push_back(shape);
};
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.hpp"

class Circle : public Shape::Shape {
public:
  enum  class Color {
    BLACK,
    RED,
    PINK
  };
  Circle (float x, float y, float r, Color color);
  Circle (float x, float y, float r);
  void draw(sf::RenderWindow &target);
  void translate(const float x, const float y);
private:
  float myX;
  float myY;
  float myR;
  Color myColor;
};

#endif
#include "Circle.hpp"
#include <iostream>
#include <string>

using namespace std;

Circle::Circle(float x, float y, float r, Circle::Color color) {
  myX = x;
  myY = y;
  myR = r;
  myColor = color;
};

Circle::Circle(float x, float y, float r) {
  Circle::Circle(x, y, r, Circle::Color::BLACK);
};

void Circle::draw(sf::RenderWindow &target) {
  cout << "Drawing a shape..." << endl;
  sf::CircleShape circle;
	circle.setRadius(myR);
	circle.setPosition(myX, myY);
  circle.setFillColor(sf::Color(100, 250, 50)); // On verra les différentes couleures ensuite...
  target.draw(circle);
};

void Circle::translate(const float x, const float y) {
  myX += x;
  myY += y;
};

以上是关于c_cpp 复合模式问题的主要内容,如果未能解决你的问题,请参考以下文章

将复合模式转换为具有附加行为的新模式

MVC:两步和复合视图模式之间的差异

访问复合模式 Java 的叶子

《Head First 设计模式》学习笔记——复合模式

C#、温莎城堡和复合设计模式

c_cpp myKaarma模式问题