Dinamik CMS bileşenleri için Desenler (olay güdümlü?)

1 Cevap php

Benim başlık büyük değil Üzgünüz, bu ben hatırlıyorum daha fazla yıl boyunca usul oldum gibi OO% 100 hareketli benim ilk gerçek kumar oynamak olduğunu. Ben ne yapmaya çalışıyorum mümkün olup olmadığını anlamak zor buluyorum. 2 aşağıdaki noktaları üzerinde insanların düşüncelerini bağlı olarak, ben bu yolu aşağı gidersiniz.

Ben kuruyorum CMS alıntı küçük, ancak farklı içerik türleri üzerinde çok duruluyor. Ben kolayca ben çok rahatım hangi Drupal kullanabilirsiniz, ama ben kendimi tasarım kalıpları / OO-PHP kendimi içine taşımak için çok iyi nedenler vermek istiyorum

1) I have created a base 'content' class which I wish to be able to extend to handle different types of content. The base class, for example, handles HTML content, and extensions might handle XML or PDF output instead. On the other hand, at some point I may wish to extend the base class for a given project completely. I.e. if class 'content-v2' extended class 'content' for that site, any calls to that class should actually call 'content-v2' instead. Is that possible?

Kod türü 'içerik' bir nesne örneğini ise -, ben aslında devralma kullanarak bunu nasıl görebiliyorum ... bu tip 'içerik-v2' birini örneğini istiyorum, ama açıkça sınıfa atıfta dahil görünüyor Ben dinamik yerine kullanmak istediğiniz sınıfı nasıl bağlantı göremiyorum.

2) Secondly, the way I'm building this at the moment is horrible, I'm not happy with it. It feels very linear indeed - i.e. get session details > get content > build navigation > theme page > publish. To do this all the objects are called 1-by-1 which is all very static. I'd like it to be more dynamic so that I can add to it at a later date (very closely related to first question).

Bir yolu var mı bunun yerine benim orkestraci sınıf yerine diğer sınıfların her zaman geçerli olarak, belirli olaylar için 'dinlemek', sonra sonunda her şeyi kadar bina, tüm diğer sınıfları 1-by-1 çağrı nokta onların BUT ve mutlaka atlamak? Bu şekilde orkestraci sınıfın diğer sınıflar istendi bilmek gerekir, ve 1-by-1 demezdim.

Ben var ise özür dilerim hepsi benim kafamda bükülmüş. Bu yüzden gerçekten esnek Bunu oluşturmaya çalışıyorum.

1 Cevap

Content ve Content-v2 ile çalışma hakkında sorunuz için (ki korkunç isimler btw vardır) ...

  1. encapsulation ve polymorphism hakkında daha fazla okumak gitmek
  2. Seni devralmasını tarafından başka herhangi bir şekilde tüketilen asla soyut bir sınıf olan bir BaseContent sınıf gerekir düşünüyorum.
  3. Lütfen BaseContent sınıftan sonra bir HtmlContent, PdfContent, MSWordContent, vb sınıfları olmalıdır. Ve eğer isterseniz, hatta böyle bir HtmlReportContent (uzanır HtmlContent), MSWord2010Content, vb gibi, bu uzatabilirsiniz
  4. Bu şekilde yaparak, beyan ve bunları örneğini zaman mağaza değişkenleri tipi BaseContent tüm ama, sizin belirli bir tip olarak bunları başlatamazsınız. Bunu yaptığınızda, varsa bile sizin BaseContent sınıfta uygulanan) yöntem, çocukların sınıfları) yöntemi (baz Render kullanmayı tercih ya da geçersiz kılmak ve kendi uygulamasını sağlamak, hatta onu geçersiz, biraz sağlayabilir (Render özel uygulama, ve daha sonra da bu uygulamasından faydalanmak için temel uygulama diyoruz.

Eğer uygulama veya davranışı paylaşılan varsa o zaman, (örneğin, bir Chihuahua ve GermanShepard sınıfını hem Bark() işlevi olurdu ama onlar farklı uygulamaya konacağını) Bir temel sınıf uygulama ya da davranış paylaştı soyut. Sen mutlaka taban sınıftaki bir uygulama sağlamak zorunda değilsiniz - bu soyut bir sınıf yüzden bu - o zaman Bark() sizin {tanımlanmış olacaktır (bunu uygulamak için çocuğunuzun sınıfları zorlar [(4)] } class ancak uygulanmadı).

As for the flow of your system... For your Object-Oriented design, think of designing a state machine and your objects are essentially filling out that state machine's purpose and every change from one state to another state. Yeah, there is probably one way to walk through your state-flow diagram that covers a good 50% of scenarios, but we all know that users will deviate from that. So you need to define the ways that a user can deviate from it and allow those but restrict to those. You may or may not actually draw out a flow diagram, depending on how complex your system is. But sometimes it helps to visualize some of the possibilities by drawing out part of it. Typically, though, this diagram would be way too large for most OO systems to actually create. If you were doing a system for NASA, you would probably have it, though. :-P

İşte bu bazı şeyleri göstermek amacıyla (ve görüşleri bazı soruların ele) bazı koddur. Ancak, ben size C # örnekler vereceğim yüzden ben bir PHP adam değilim ama kolayca yeterli PHP çevirmek gerekir ki yeteri kadar basit tutmak gerekir.

public interface IAnimal
{
    string GetName();
    string Talk();
}

public abstract class AnimalBase : IAnimal
{
    private string _name;

    // Constructor #1
    protected AnimalBase(string name)
    {
        _name = name;
    }

    // Constructor #2
    protected AnimalBase(string name, bool isCutsey)
    {
        if (isCutsey)
        {
            // Change "Fluffy" into "Fluffy-poo"
            _name = name + "-poo";
        }
    }

    // GetName implemention from IAnimal.
    // In C#, "virtual" means "Let the child class override this if it wants to but is not required to"
    public virtual string GetName()
    {
        return _name;
    }

    // Talk "implementation" from IAnimal.
    // In C#, "abstract" means "require our child classes to override this and provide the implementation".
    // Since our base class forces child classes to provide the implementation, this takes care of the IAnimal implementation requirement.
    abstract public string Talk();
}

public class Dog : AnimalBase
{
    // This constructor simply passes on the name parameter to the base class's constructor.
    public Dog(string name)
        : base(name)
    {
    }

    // This constructor passes on both parameters to the base class's constructor.
    public Dog(string name, bool isCutsey)
        : base(name, isCutsey)
    {
    }

    // Override the base class's Talk() function here, and this satisfy's AnimalBase's requirement to provide this implementation for IAnimal.
    public override string Talk()
    {
        return "Woof! Woof!";
    }
}

public class SmallDog : Dog
{
    private bool _isPurseDog;

    // This constructor is unique from all of the other constructors.
    // Rather than the second boolean representing the "isCutsey" property, it's entirely different.
    // It's entirely a coincidence that they're the same datatype - this is not important.
    // Notice that we're saying ALL SmallDogs are cutsey by passing a hardcoded true into the base class's (Dog) second parameter of the constructor.
    public SmallDog(string name, bool isPurseDog)
        : base(name, true)
    {
        _isPurseDog = isPurseDog;
    }

    // This tells us if the dog fits in a purse.
    public bool DoesThisDogFitInAPurse()
    {
        return _isPurseDog;
    }

    // Rather than using Dog's Talk() implementation, we're changing this because this special type of dog is different.
    public override string Talk()
    {
        return "Yip! Yip!";
    }
}

public class Chihuahua : SmallDog
{
    private int _hatSize;

    // We say that Chihuahua's always fit in a purse. Nothing else different about them, though.
    public Chihuahua(string name, int hatSize)
        : base(name, true)
    {
        _hatSize = hatSize;
    }

    // Of course all chihuahuas wear Mexican hats, so let's make sure we know its hat size!
    public int GetHatSize()
    {
        return _hatSize;
    }
}

public class Cat : AnimalBase
{
    // This constructor simply passes on the name parameter to the base class's constructor.
    public Cat(string name)
        : base(name)
    {
    }

    // This constructor passes on both parameters to the base class's constructor.
    public Cat(string name, bool isCutsey)
        : base(name, isCutsey)
    {
    }

    // Override the base class's Talk() function here, and this satisfy's AnimalBase's requirement to provide this implementation for IAnimal.
    public override string Talk()
    {
        return "Meoooowwww...";
    }
}

public class Lion : Cat
{
    public Lion(string name)
        : base(name)
    {
    }

    // Rather than using Cat's Talk() implementation, we're changing this because this special type of cat is different.
    public override string Talk()
    {
        return "ROAR!!!!!!!!";
    }
}

public class ThisIsNotAGoodExampleOfObjectOrientedCoding
{
    public string DoStuff()
    {
        // To keep the C#-to-PHP translation easy, think of this as an array of IAnimal objects.
        List<IAnimal> myAnimals = new List<IAnimal>();

        IAnimal strayCat = new Cat("Garfield", false);
        Cat myPet = new Cat("Katrina");
        IAnimal myMothersDog = new Dog("Harley");
        Dog myMothersOtherDog = new Dog("Cotton");
        IAnimal myNeighborsDog = new SmallDog("Roxy", false);
        Dog movieStarsDog = new SmallDog("Princess", true);
        Dog tacoBellDog = new Chihuahua("Larry", 7);
        Lion lionKing = new Lion("Simba");

        myAnimals.Add(strayCat);
        myAnimals.Add(myPet);
        myAnimals.Add(myMothersDog);
        myAnimals.Add(myMothersOtherDog);
        myAnimals.Add(myNeighborsDog);
        myAnimals.Add(movieStarsDog);
        myAnimals.Add(tacoBellDog);
        myAnimals.Add(lionKing);

        string allAnimalsTalking = "";

        // Create a string to return.
        // Garfield says "Meow". Fido says "Woof! Woof!" etc...
        for (int i = 0; i < myAnimals.Count; i++)
        {
            allAnimalsTalking = allAnimalsTalking + myAnimals[i].GetName() + " says \"" + myAnimals[i].Talk() + "\" ";

            if (myAnimals[i] is SmallDog)
            {
                // Cast the IAnimal into a SmallDog object.
                SmallDog yippyDog = myAnimals[i] as SmallDog;

                if (yippyDog.DoesThisDogFitInAPurse())
                {
                    allAnimalsTalking = allAnimalsTalking + " from a purse.";
                }
            }
        }

        return allAnimalsTalking;
    }
}

Bu bazı yardımcı olur umarım.