public void RegisterTypeDefaultValueConversionTest() { ElementA element = new ElementA(); Assert.AreEqual(1, ElementA.Value3Property.GetMetadata(typeof(ElementA)).DefaultValue); Assert.AreEqual(1, element.GetValue(ElementA.Value3Property)); }
public static void Main(string[] args) { IVisitor visitor = new VisitorA(); IElement element; element = new ElementA(); element.Accept(visitor); element = new ElementB(); element.Accept(visitor); }
public void OverrideMetadataDefaultValueTest() { ElementA element1 = new ElementA(); ElementB element2 = new ElementB(); ElementC element3 = new ElementC(); ElementD element4 = new ElementD(); int propertyChangedCount = 0; element1.PropertyChanged += (sender, e) => propertyChangedCount++; element2.PropertyChanged += (sender, e) => propertyChangedCount++; element3.PropertyChanged += (sender, e) => propertyChangedCount++; element4.PropertyChanged += (sender, e) => propertyChangedCount++; Assert.AreEqual(1, element1.GetValue(ElementA.Value1Property)); Assert.AreEqual(2, element2.GetValue(ElementA.Value1Property)); Assert.AreEqual(3, element3.GetValue(ElementA.Value1Property)); Assert.AreEqual(4, element4.GetValue(ElementA.Value1Property)); Assert.AreEqual(1, element1.GetValue(ElementA.Value2Property)); Assert.AreEqual(2, element2.GetValue(ElementA.Value2Property)); Assert.AreEqual(3, element3.GetValue(ElementA.Value2Property)); Assert.AreEqual(4, element4.GetValue(ElementA.Value2Property)); element1.SetValue(ElementA.Value1Property, 1); element2.SetValue(ElementA.Value1Property, 2); element3.SetValue(ElementA.Value1Property, 3); element4.SetValue(ElementA.Value1Property, 4); Assert.AreEqual(0, propertyChangedCount); element1.SetValue(ElementA.Value1Property, 100); Assert.AreEqual(1, propertyChangedCount); element1.SetValue(ElementA.Value2Property, 1); element2.SetValue(ElementA.Value2Property, 2); element3.SetValue(ElementA.Value2Property, 3); element4.SetValue(ElementA.Value2Property, 4); Assert.AreEqual(1, propertyChangedCount); element1.SetValue(ElementA.Value2Property, 100); Assert.AreEqual(2, propertyChangedCount); }
public void AttachedOverrideMetadataCallbackTest() { ElementA element1 = new ElementA(); ElementB element2 = new ElementB(); ElementC element3 = new ElementC(); ElementD element4 = new ElementD(); int changeIndex = 1; int value2AChangedIndex = 0; int value2BChangedIndex = 0; int value2CChangedIndex = 0; int value2DChangedIndex = 0; ElementA.Value2ChangedA += (sender, e) => value2AChangedIndex = changeIndex++; ElementB.Value2ChangedB += (sender, e) => value2BChangedIndex = changeIndex++; ElementC.Value2ChangedC += (sender, e) => value2CChangedIndex = changeIndex++; ElementD.Value2ChangedD += (sender, e) => value2DChangedIndex = changeIndex++; element1.SetValue(ElementA.Value2Property, 100); Assert.AreEqual(1, value2AChangedIndex); Assert.AreEqual(0, value2BChangedIndex); Assert.AreEqual(0, value2CChangedIndex); Assert.AreEqual(0, value2DChangedIndex); element2.SetValue(ElementA.Value2Property, 100); Assert.AreEqual(2, value2AChangedIndex); Assert.AreEqual(3, value2BChangedIndex); Assert.AreEqual(0, value2CChangedIndex); Assert.AreEqual(0, value2DChangedIndex); element3.SetValue(ElementA.Value2Property, 100); Assert.AreEqual(4, value2AChangedIndex); Assert.AreEqual(5, value2BChangedIndex); Assert.AreEqual(6, value2CChangedIndex); Assert.AreEqual(0, value2DChangedIndex); element4.SetValue(ElementA.Value2Property, 100); Assert.AreEqual(7, value2AChangedIndex); Assert.AreEqual(5, value2BChangedIndex); Assert.AreEqual(6, value2CChangedIndex); Assert.AreEqual(8, value2DChangedIndex); }
public void VisitorsTest() { IVisitor visitorA = new VisitorA(); IVisitor visitorB = new VisitorB(); Element elementA = new ElementA(); Element elementB = new ElementB(); elementA.Accept(visitorA); Assert.Equal("Visitor A For Element A", elementA.ProcessElement()); elementA.Accept(visitorB); Assert.Equal("Visitor B For Element A", elementA.ProcessElement()); elementB.Accept(visitorA); Assert.Equal("Visitor A For Element B", elementB.ProcessElement()); elementB.Accept(visitorB); Assert.Equal("Visitor B For Element B", elementB.ProcessElement()); }
/// <summary> /// Vérifie que le lien est cohérent /// </summary> /// <returns></returns> public CResultAErreur ControleCoherenceLien() { CResultAErreur result = CResultAErreur.True; if (ProjetA != null && ProjetB != null) { if (ProjetA.IsChildOf(ProjetB)) { result.EmpileErreur(I.T("Link between @1 and @2 is invalide, because @1 is parent of @2|20072", ProjetB.Libelle, ProjetA.Libelle)); } if (result && ProjetB.IsChildOf(ProjetA)) { result.EmpileErreur(I.T("Link between @1 and @2 is invalide, because @1 is parent of @2|20072", ProjetA.Libelle, ProjetB.Libelle)); } } if (ElementA != null && ElementB != null && result) { HashSet <IElementDeProjetPlanifiable> set = new HashSet <IElementDeProjetPlanifiable>(); ElementA.FillDicPredecesseurs(set); if (set.Contains(ElementB)) { result.EmpileErreur(I.T("Link between @1 and @2 provokes a cyclic dependence|20073", ElementA.DescriptionElement, ElementB.DescriptionElement)); } if (result) { set.Clear(); ElementB.FillDicSuccesseurs(set); if (set.Contains(ElementA)) { result.EmpileErreur(I.T("Link between @1 and @2 provokes a cyclic dependence|20073", ElementA.DescriptionElement, ElementB.DescriptionElement)); } } } return(result); }
public override void VisitElementA(ElementA elementA) { elementA.OperationA(); }
public void Visit(ElementA a) { a.Print(); }
public abstract void VisitElementA(ElementA elemA);
public void VisitElementA(ElementA element) { element.OperationA(); }
static void Main(string[] args) { #region Creational Patterns //Singleton Singleton singleton = Singleton.GetInstance(); singleton.someUsefulCode(); Separate(); //Builder Item ciastko = new ItemBuilder(0).SetName("Ciastko").SetType("Food").build(); Console.WriteLine($"{ciastko.id}. {ciastko.name}, {ciastko.type}"); Separate(); //Prototype Person person = new Person(1, 15, "John"); Person shallowCopy = (Person)person.ShallowCopy(); Person deepCopy = (Person)person.DeepCopy(); Console.WriteLine(person); Console.WriteLine(shallowCopy); Console.WriteLine(deepCopy); person.name = "Max"; person.age = 20; person.id = new IdInfo(99); Console.WriteLine(person); Console.WriteLine(shallowCopy); Console.WriteLine(deepCopy); Separate(); //AbstractFactory Car miniCar = CarFactory.CarBuilder(CarType.MINI); Car lumiCar = CarFactory.CarBuilder(CarType.LUXURY); Separate(); //FactoryPattern ShapeFactory shapeFactory = new ShapeFactory(); Shape circle = shapeFactory.GetShape("circle"); circle.print(); Separate(); //ObjectPool SomeObject so1 = Pool.GetObject(); SomeObject so2 = Pool.GetObject(); Pool.ReleaseObject(so1); SomeObject so3 = Pool.GetObject(); Console.WriteLine($"so1: {so1}"); Console.WriteLine($"so2: {so2}"); Console.WriteLine($"so3: {so3}"); Separate(); #endregion #region Structural Patterns //Adapter OldClass oldClass = new OldClass(); IWrite write = new Adapter(oldClass); write.PrintMessage(); Separate(); //Bridge Thing triangle = new Ball(new Blue()); triangle.print(); Separate(); //Composite Component leaf1 = new Leaf("leaf1"); Component leaf2 = new Leaf("leaf2"); Component leaf3 = new Leaf("leaf3"); Component branch1 = new Composite("branch1"); branch1.Add(leaf1); branch1.Add(leaf2); Component root = new Composite("root"); root.Add(branch1); root.Add(leaf3); root.Display(1); Separate(); //Decorator Weapon weapon = new BaseWeapon(); weapon.shot(); Weapon modifiedWeapon = new Stock(weapon); modifiedWeapon.shot(); Separate(); //Facade Student student = new Student(); student.StartLearning(); student.EndLearning(); Separate(); //Flyweight Particle p1 = ParticleFactory.getSmallParticle("red"); //new key Particle p2 = ParticleFactory.getSmallParticle("red"); //no new one Particle p3 = ParticleFactory.getSmallParticle("blue"); //new key Separate(); //Proxy IBank proxyBank = new ProxyBank(); proxyBank.Pay(); Separate(); #endregion #region Behavioral Patterns //ChainOfResponsibility Chain chain = new Chain(); chain.Process(5); Separate(); //Command var modifyPrice = new ModifyPrice(); var product = new Product("Brick", 200); Console.WriteLine(product); modifyPrice.SetCommandAndInvoke(new ProductCommand(product, PriceAction.Increase, 50)); Console.WriteLine(product); Separate(); //Iterator CarRepository carRepository = new CarRepository(new string[] { "Renault", "BMW", "VW" }); for (IIterator i = carRepository.GetIterator(); i.hasNext();) { Console.WriteLine($"Car: {i.next()}"); } Separate(); //Mediator Mediator mediator = new RealMediator(); APerson jhon = new RealPerson(mediator, "Jhon"); APerson max = new RealPerson(mediator, "Max"); APerson emma = new RealPerson(mediator, "Emma"); mediator.AddPerson(jhon); mediator.AddPerson(max); mediator.AddPerson(emma); jhon.Send("I'm a jhon and this is my message! "); emma.Send("henlo"); Separate(); //Memento Localization localization = new Localization("NY", 123, 111); LocalizationSnapshot snapshot = localization.CreateSnapshot(); Console.WriteLine(localization); localization.City = "Berlin"; localization.X = 999; Console.WriteLine(localization); snapshot.Restore(); Console.WriteLine(localization); Separate(); //Observer Customer james = new Customer("James"); Customer william = new Customer("William"); Customer evelyn = new Customer("Evelyn"); Shop grocery = new Shop("YourFood"); Shop DIYshop = new Shop("Tooler"); grocery.AddSubscriber(james); grocery.AddSubscriber(william); grocery.AddSubscriber(evelyn); DIYshop.AddSubscriber(james); DIYshop.AddSubscriber(william); grocery.AddMerchandise(new Merchandise("pizza", 19)); DIYshop.AddMerchandise(new Merchandise("hammer", 399)); Separate(); //State AudioPlayer ap = new AudioPlayer(); ap.ClickPlay(); ap.ClickLock(); ap.ClickPlay(); ap.ClickPlay(); ap.ClickPlay(); ap.ClickLock(); ap.NextSong(); ap.PreviousSong(); Separate(); //Strategy Calculator calculator = new Calculator(new AddingStrategy()); calculator.Calculate(5, 2); calculator.ChangeStrategy(new SubtractingStrategy()); calculator.Calculate(5, 2); Separate(); //TemplateMethod Algorithm a1 = new AlgorithmWithStep2(); a1.Execute(); Algorithm a2 = new AlgorithmWithStep2and3(); a2.Execute(); Separate(); //Visitor ElementA ea = new ElementA("ElementA"); ElementA eb = new ElementA("ElementB"); Visitor1 v1 = new Visitor1(); Visitor2 v2 = new Visitor2(); ea.Accept(v1); ea.Accept(v2); eb.Accept(v1); eb.Accept(v2); #endregion }