public static void TestComposite() { Console.WriteLine("-- TEST COMPOSITE --"); ICompositeCompenent main = new CompositeComponent("TOP"); ICompositeCompenent componentA = new CompositeComponent("ComponentA"); ICompositeCompenent componentB = new CompositeComponent("ComponentB"); ICompositeCompenent componentC = new CompositeComponent("ComponentC"); ICompositeCompenent leaf = new Leaf("Leaf"); ICompositeCompenent leafA1 = new Leaf("LeafA1"); ICompositeCompenent leafA2 = new Leaf("LeafA2"); ICompositeCompenent leafB1 = new Leaf("LeafB1"); ICompositeCompenent leafC1 = new Leaf("LeafC1"); componentA.Add(leafA1); componentA.Add(leafA2); componentB.Add(leafB1); componentB.Add(componentC); componentC.Add(leafC1); main.Add(leaf); main.Add(componentA); main.Add(componentB); main.Display(0); }
static void Main(string[] args) { var composite = new CompositeComponent(); composite.Add(new Leaf()); composite.Add(new SecondTypeOfLeaf()); composite.Add(new AThirdLeafType()); component = composite; component.Something(); }
static async void Composite(string path) { WebCrawler crawler = WebCrawler.GetInstance(); HtmlStoreProxy proxy = new HtmlStoreProxy(); MyHtmlModel model = await proxy.GetMyHtmlModel(path); HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(model.HTML); HtmlNode html = crawler.GetFirstNode("html", htmlDoc); var bodyNode = html.SelectSingleNode("//body"); var navigationNode = crawler.GetNode ( "class", "navbar navbar-expand-lg navbar-light fixed-top", htmlDoc ); var headerNode = crawler.GetNode ( "class", "masthead", htmlDoc ); var aboutSectionNode = crawler.GetNode ( "id", "about", htmlDoc ); var signupSection = crawler.GetNode ( "id", "signup", htmlDoc ); var contentOfsignupSection = crawler.GetFirstNode("body/section[3]/div[1]", htmlDoc); signupSection.RemoveAllChildren(); html.RemoveChild(bodyNode);//html tag is without body tag Console.WriteLine(html.OuterHtml); ////////////////Composite realization Composite.Component htmlComposite = new CompositeComponent ( new MyHtmlModel() { HTML = html.OuterHtml, Name = "WithoutBody" } ); htmlComposite.Node = html; bodyNode.RemoveAllChildren();//body tag is empty //composite components Composite.Component EmptyBody = new CompositeComponent ( new MyHtmlModel() { HTML = bodyNode.OuterHtml, Name = "EmptyBody" } ); EmptyBody.Node = bodyNode; //leaf HtmlElem navigation = new HtmlElem(new MyHtmlModel() { HTML = navigationNode.OuterHtml, Name = "NavBar" }); navigation.Node = navigationNode; //leaf HtmlElem header = new HtmlElem(new MyHtmlModel() { HTML = headerNode.OuterHtml, Name = "Header" }); header.Node = headerNode; //leaf HtmlElem AboutSection = new HtmlElem(new MyHtmlModel() { HTML = aboutSectionNode.OuterHtml, Name = "NavBar" }); AboutSection.Node = aboutSectionNode; //append children to body EmptyBody.Add(navigation); EmptyBody.Node.AppendChild(navigation.Node); EmptyBody.Add(header); EmptyBody.Node.AppendChild(header.Node); EmptyBody.Add(AboutSection); EmptyBody.Node.AppendChild(AboutSection.Node); htmlComposite.Add(EmptyBody); htmlComposite.Node.AppendChild(EmptyBody.Node); htmlComposite.Display();//displaying tree Console.WriteLine("--------------------htmlComposite html----------------"); Console.WriteLine(htmlComposite.Node.OuterHtml); //displaying node html EmptyBody.Remove(AboutSection); //remove child EmptyBody.Node.RemoveChild(AboutSection.Node); //remove node Console.WriteLine("--------------------htmlComposite html after remove----------------"); Console.WriteLine(htmlComposite.Node.OuterHtml);//displaying body without child EmptyBody.Add(AboutSection); EmptyBody.Node.AppendChild(AboutSection.Node); //add one more composite element Composite.Component signUpSectionComposite = new CompositeComponent (new MyHtmlModel() { HTML = signupSection.OuterHtml, Name = "EmptySignUpSection" }); signUpSectionComposite.Node = signupSection; //leaf HtmlElem signUpSectionContainer = new HtmlElem ( new MyHtmlModel() { HTML = contentOfsignupSection.OuterHtml, Name = "SignUpSectionContainer" } ); signUpSectionContainer.Node = contentOfsignupSection; signUpSectionComposite.Add(signUpSectionContainer); signUpSectionComposite.Node.AppendChild(signUpSectionContainer.Node); EmptyBody.Add(signUpSectionComposite); EmptyBody.Node.AppendChild(signUpSectionComposite.Node); htmlComposite.Display(); Console.WriteLine("--------------------htmlComposite html----------------"); Console.WriteLine(htmlComposite.Node.OuterHtml); htmlComposite.MyHtmlmodel.HTML = htmlComposite.Node.OuterHtml;//saving all html MyHtmlModel final = new MyHtmlModel() { HTML = htmlComposite.MyHtmlmodel.HTML, Name = "ResultOfComposition" }; try { GenericSerializer serializer = new GenericSerializer("res", @"E:\studying\education\6сем\трпз\lab5\Saves\"); serializer.BinarySerializing(FileMode.Create, final); serializer.DataContractSerialization(FileMode.Create, typeof(MyHtmlModel), final); } catch (Exception e) { } }