分隔策略模式返回的类型

时间:2018-07-19 15:54:11

标签: design-patterns covariance

我对某些代码有疑问。为了弄清楚这一点,我做了一个尽可能简单的类比-请原谅任何语法错误,因为在编程时我很生疏,所以我很快就将它们组合在一起。

这个想法是我有一些实现策略的类。我需要管理它们的对象以某种方式识别操作返回的类型以便处理它(可能不进行向下转换)-别问,这不在我的控制之下。

我考虑过使用协变返回类型,以避免必须显式引用具体的实现(即使那时我必须编写一个处理每种类型的函数,但是对我来说似乎还可以,因为所有内容都保持在一起并且对于一个新的类型,您只需添加一个新功能即可。

    public abstract class ArtPiece
{
}

public class Painting extends ArtPiece
{
    public Painting()
    {
    }

    // paintings stuff here
}

public class Song extends ArtPiece
{
    public Song()
    {
    }

    // songs stuff here
}

public class Book extends ArtPiece
{
    public Book()
    {
    }

    // books stuff here
}

///////////////////////////////////////////////////

public abstract class Artist
{
    public abstract ArtPiece create();
}

public class Painter extends Artist
{
    public Painter()
    {
    }

    public Painting create()
    {
        return new Painting();
    }
}

public class Musician extends Artist
{
    public Musician()
    {
    }

    public Song create()
    {
        return new Song();
    }
}

public class Writer extends Artist
{
    public Writer()
    {
    }

    public Book create()
    {
        return new Book();
    }
}

//////////////////////////////////////////////////

public class ArtCollector
{
    private Artist[] artists;
    private Painting[] paintings;
    private Song[] songs;
    private Book[] books;

    private int followedArtists;
    private int ownedPaintings;
    private int ownedSongs;
    private int ownedBooks;

    public ArtCollector()
    {
        followedArtists = 0;
        ownedPaintings = 0;
        ownedSongs = 0;
        ownedBooks = 0;

        artists = new Artist[3];
        paintings = new Painting[3];
        songs = new Song[3];
        books = new Book[3];
    }

    public void followArtist(Artist a)
    {
        if (followedArtists < artists.length)
        {
            artists[followedArtists++] = a;
            return;
        }
        System.out.println("Too many artists to follow!");
    }

    public void commissionArt(int id)
    {
        storeArtPiece(artists[id].create());
    }

    private void storeArtPiece(Painting p)
    {
        if (ownedPaintings < paintings.length)
        {
            paintings[ownedPaintings++] = p;
            return;
        }
        System.out.println("Too many paintings to store!");
    }

    private void storeArtPiece(Song s)
    {
        if (ownedSongs < songs.length)
        {
            songs[ownedSongs++] = s;
            return;
        }
        System.out.println("Too many songs to store!");
    }

    private void storeArtPiece(Book b)
    {
        if (ownedBooks < books.length)
        {
            books[ownedBooks++] = b;
            return;
        }
        System.out.println("Too many books to store!");
    }

    public void countPaintings()
    {
        System.out.println("I own " + paintings.length.toString() + " paintings.");
    }

    public void countSongs()
    {
        System.out.println("I own " + songs.length.toString() + " songs.");
    }

    public void countBooks()
    {
        System.out.println("I own " + books.length.toString() + " books.");
    }
}

//////////////////////////////////////////////////

public static void main(string[] args)
{
    ArtCollector collector = new ArtCollector();

    collector.followArtist(new Musician());
    collector.followArtist(new Painter());
    collector.followArtist(new Writer());

    collector.commissionArt(0);
    collector.commissionArt(0);
    collector.commissionArt(1);
    collector.commissionArt(2);
    collector.commissionArt(1);
    collector.commissionArt(0);
    collector.commissionArt(0);

    collector.countPaintings();
    collector.countSongs();
    collector.countBooks();
}

首先,我还没有测试这种方法。我只是想解决这个问题。

我想知道您对此的看法-我在正确的轨道上吗?

您认为有更好的方法来解决这个问题吗?

0 个答案:

没有答案